#Script Pages

<back to all web services

QueryCustomers

<?php namespace dtos;

use DateTime;
use Exception;
use DateInterval;
use JsonSerializable;
use ServiceStack\{IReturn,IReturnVoid,IGet,IPost,IPut,IDelete,IPatch,IMeta,IHasSessionId,IHasBearerToken,IHasVersion};
use ServiceStack\{ICrud,ICreateDb,IUpdateDb,IPatchDb,IDeleteDb,ISaveDb,AuditBase,QueryDb,QueryDb2,QueryData,QueryData2,QueryResponse};
use ServiceStack\{ResponseStatus,ResponseError,EmptyResponse,IdResponse,ArrayList,KeyValuePair2,StringResponse,StringsResponse,Tuple2,Tuple3,ByteArray};
use ServiceStack\{JsonConverters,Returns,TypeContext};


// @DataContract
class QueryBase implements JsonSerializable
{
    public function __construct(
        // @DataMember(Order=1)
        /** @var int|null */
        public ?int $skip=null,

        // @DataMember(Order=2)
        /** @var int|null */
        public ?int $take=null,

        // @DataMember(Order=3)
        /** @var string|null */
        public ?string $orderBy=null,

        // @DataMember(Order=4)
        /** @var string|null */
        public ?string $orderByDesc=null,

        // @DataMember(Order=5)
        /** @var string|null */
        public ?string $include=null,

        // @DataMember(Order=6)
        /** @var string|null */
        public ?string $fields=null,

        // @DataMember(Order=7)
        /** @var array<string,string>|null */
        public ?array $meta=null
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['skip'])) $this->skip = $o['skip'];
        if (isset($o['take'])) $this->take = $o['take'];
        if (isset($o['orderBy'])) $this->orderBy = $o['orderBy'];
        if (isset($o['orderByDesc'])) $this->orderByDesc = $o['orderByDesc'];
        if (isset($o['include'])) $this->include = $o['include'];
        if (isset($o['fields'])) $this->fields = $o['fields'];
        if (isset($o['meta'])) $this->meta = JsonConverters::from(JsonConverters::context('Dictionary',genericArgs:['string','string']), $o['meta']);
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->skip)) $o['skip'] = $this->skip;
        if (isset($this->take)) $o['take'] = $this->take;
        if (isset($this->orderBy)) $o['orderBy'] = $this->orderBy;
        if (isset($this->orderByDesc)) $o['orderByDesc'] = $this->orderByDesc;
        if (isset($this->include)) $o['include'] = $this->include;
        if (isset($this->fields)) $o['fields'] = $this->fields;
        if (isset($this->meta)) $o['meta'] = JsonConverters::to(JsonConverters::context('Dictionary',genericArgs:['string','string']), $this->meta);
        return empty($o) ? new class(){} : $o;
    }
}

/**
 * @template T
 */
class QueryDb extends QueryBase implements JsonSerializable
{
    public array $genericArgs = [];
    public static function create(array $genericArgs=[]): QueryDb {
        $to = new QueryDb();
        $to->genericArgs = $genericArgs;
        return $to;
    }

    /**
     * @param int|null $skip
     * @param int|null $take
     * @param string|null $orderBy
     * @param string|null $orderByDesc
     * @param string|null $include
     * @param string|null $fields
     * @param array<string,string>|null $meta
     */
    public function __construct(
        mixed $skip=null,
        mixed $take=null,
        mixed $orderBy=null,
        mixed $orderByDesc=null,
        mixed $include=null,
        mixed $fields=null,
        mixed $meta=null
    ) {
        parent::__construct($skip,$take,$orderBy,$orderByDesc,$include,$fields,$meta);
    }

    /** @throws Exception */
    public function fromMap($o): void {
        parent::fromMap($o);
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = parent::jsonSerialize();
        return empty($o) ? new class(){} : $o;
    }
}

class Order implements JsonSerializable
{
    public function __construct(
        /** @var int */
        public int $orderId=0,
        /** @var string|null */
        public ?string $customerId=null,
        /** @var DateTime */
        public DateTime $orderDate=new DateTime(),
        /** @var float */
        public float $total=0.0
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['orderId'])) $this->orderId = $o['orderId'];
        if (isset($o['customerId'])) $this->customerId = $o['customerId'];
        if (isset($o['orderDate'])) $this->orderDate = JsonConverters::from('DateTime', $o['orderDate']);
        if (isset($o['total'])) $this->total = $o['total'];
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->orderId)) $o['orderId'] = $this->orderId;
        if (isset($this->customerId)) $o['customerId'] = $this->customerId;
        if (isset($this->orderDate)) $o['orderDate'] = JsonConverters::to('DateTime', $this->orderDate);
        if (isset($this->total)) $o['total'] = $this->total;
        return empty($o) ? new class(){} : $o;
    }
}

class Customer implements JsonSerializable
{
    public function __construct(
        /** @var string|null */
        public ?string $customerId=null,
        /** @var string|null */
        public ?string $companyName=null,
        /** @var string|null */
        public ?string $address=null,
        /** @var string|null */
        public ?string $city=null,
        /** @var string|null */
        public ?string $region=null,
        /** @var string|null */
        public ?string $postalCode=null,
        /** @var string|null */
        public ?string $country=null,
        /** @var string|null */
        public ?string $phone=null,
        /** @var string|null */
        public ?string $fax=null,
        /** @var array<Order>|null */
        public ?array $orders=null
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['customerId'])) $this->customerId = $o['customerId'];
        if (isset($o['companyName'])) $this->companyName = $o['companyName'];
        if (isset($o['address'])) $this->address = $o['address'];
        if (isset($o['city'])) $this->city = $o['city'];
        if (isset($o['region'])) $this->region = $o['region'];
        if (isset($o['postalCode'])) $this->postalCode = $o['postalCode'];
        if (isset($o['country'])) $this->country = $o['country'];
        if (isset($o['phone'])) $this->phone = $o['phone'];
        if (isset($o['fax'])) $this->fax = $o['fax'];
        if (isset($o['orders'])) $this->orders = JsonConverters::fromArray('Order', $o['orders']);
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->customerId)) $o['customerId'] = $this->customerId;
        if (isset($this->companyName)) $o['companyName'] = $this->companyName;
        if (isset($this->address)) $o['address'] = $this->address;
        if (isset($this->city)) $o['city'] = $this->city;
        if (isset($this->region)) $o['region'] = $this->region;
        if (isset($this->postalCode)) $o['postalCode'] = $this->postalCode;
        if (isset($this->country)) $o['country'] = $this->country;
        if (isset($this->phone)) $o['phone'] = $this->phone;
        if (isset($this->fax)) $o['fax'] = $this->fax;
        if (isset($this->orders)) $o['orders'] = JsonConverters::toArray('Order', $this->orders);
        return empty($o) ? new class(){} : $o;
    }
}

/**
 * @template QueryDb of Customer
 */
class QueryCustomers extends QueryDb implements JsonSerializable
{
    /**
     * @param int|null $skip
     * @param int|null $take
     * @param string|null $orderBy
     * @param string|null $orderByDesc
     * @param string|null $include
     * @param string|null $fields
     * @param array<string,string>|null $meta
     */
    public function __construct(
        ?int $skip=null,
        ?int $take=null,
        ?string $orderBy=null,
        ?string $orderByDesc=null,
        ?string $include=null,
        ?string $fields=null,
        ?array $meta=null,
        /** @var string|null */
        public ?string $customerId=null,
        /** @var string|null */
        public ?string $companyNameContains=null,
        /** @var string[]|null */
        public ?array $countryIn=null
    ) {
        parent::__construct($skip,$take,$orderBy,$orderByDesc,$include,$fields,$meta);
    }

    /** @throws Exception */
    public function fromMap($o): void {
        parent::fromMap($o);
        if (isset($o['customerId'])) $this->customerId = $o['customerId'];
        if (isset($o['companyNameContains'])) $this->companyNameContains = $o['companyNameContains'];
        if (isset($o['countryIn'])) $this->countryIn = JsonConverters::fromArray('string', $o['countryIn']);
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = parent::jsonSerialize();
        if (isset($this->customerId)) $o['customerId'] = $this->customerId;
        if (isset($this->companyNameContains)) $o['companyNameContains'] = $this->companyNameContains;
        if (isset($this->countryIn)) $o['countryIn'] = JsonConverters::toArray('string', $this->countryIn);
        return empty($o) ? new class(){} : $o;
    }
}

// @DataContract
/**
 * @template Customer
 */
class QueryResponse implements JsonSerializable
{
    public array $genericArgs = [];
    public static function create(array $genericArgs=[]): QueryResponse {
        $to = new QueryResponse();
        $to->genericArgs = $genericArgs;
        return $to;
    }

    public function __construct(
        // @DataMember(Order=1)
        /** @var int */
        public mixed $offset=0,

        // @DataMember(Order=2)
        /** @var int */
        public mixed $total=0,

        // @DataMember(Order=3)
        /** @var array<Customer>|null */
        public mixed $results=null,

        // @DataMember(Order=4)
        /** @var array<string,string>|null */
        public mixed $meta=null,

        // @DataMember(Order=5)
        /** @var ResponseStatus|null */
        public mixed $responseStatus=null
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['offset'])) $this->offset = $o['offset'];
        if (isset($o['total'])) $this->total = $o['total'];
        if (isset($o['results'])) $this->results = JsonConverters::fromArray($this->genericArgs[0], $o['results']);
        if (isset($o['meta'])) $this->meta = JsonConverters::from(JsonConverters::context('Dictionary',genericArgs:['string','string']), $o['meta']);
        if (isset($o['responseStatus'])) $this->responseStatus = JsonConverters::from('ResponseStatus', $o['responseStatus']);
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->offset)) $o['offset'] = $this->offset;
        if (isset($this->total)) $o['total'] = $this->total;
        if (isset($this->results)) $o['results'] = JsonConverters::toArray($this->genericArgs[0], $this->results);
        if (isset($this->meta)) $o['meta'] = JsonConverters::to(JsonConverters::context('Dictionary',genericArgs:['string','string']), $this->meta);
        if (isset($this->responseStatus)) $o['responseStatus'] = JsonConverters::to('ResponseStatus', $this->responseStatus);
        return empty($o) ? new class(){} : $o;
    }
}

PHP QueryCustomers DTOs

To override the Content-type in your clients, use the HTTP Accept Header, append the .xml suffix or ?format=xml

HTTP + XML

The following are sample HTTP requests and responses. The placeholders shown need to be replaced with actual values.

POST /xml/reply/QueryCustomers HTTP/1.1 
Host: sharpscript.net 
Accept: application/xml
Content-Type: application/xml
Content-Length: length

<QueryCustomers xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SharpScript">
  <Skip xmlns="http://schemas.servicestack.net/types">0</Skip>
  <Take xmlns="http://schemas.servicestack.net/types">0</Take>
  <OrderBy xmlns="http://schemas.servicestack.net/types">String</OrderBy>
  <OrderByDesc xmlns="http://schemas.servicestack.net/types">String</OrderByDesc>
  <Include xmlns="http://schemas.servicestack.net/types">String</Include>
  <Fields xmlns="http://schemas.servicestack.net/types">String</Fields>
  <Meta xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns="http://schemas.servicestack.net/types">
    <d2p1:KeyValueOfstringstring>
      <d2p1:Key>String</d2p1:Key>
      <d2p1:Value>String</d2p1:Value>
    </d2p1:KeyValueOfstringstring>
  </Meta>
  <CompanyNameContains>String</CompanyNameContains>
  <CountryIn xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:string>String</d2p1:string>
  </CountryIn>
  <CustomerId>String</CustomerId>
</QueryCustomers>
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: length

<QueryResponseOfCustomerA703xr44 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.servicestack.net/types">
  <Offset>0</Offset>
  <Total>0</Total>
  <Results xmlns:d2p1="http://schemas.datacontract.org/2004/07/SharpScript">
    <d2p1:Customer>
      <d2p1:Address>String</d2p1:Address>
      <d2p1:City>String</d2p1:City>
      <d2p1:CompanyName>String</d2p1:CompanyName>
      <d2p1:Country>String</d2p1:Country>
      <d2p1:CustomerId>String</d2p1:CustomerId>
      <d2p1:Fax>String</d2p1:Fax>
      <d2p1:Orders>
        <d2p1:Order>
          <d2p1:CustomerId>String</d2p1:CustomerId>
          <d2p1:OrderDate>0001-01-01T00:00:00</d2p1:OrderDate>
          <d2p1:OrderId>0</d2p1:OrderId>
          <d2p1:Total>0</d2p1:Total>
        </d2p1:Order>
      </d2p1:Orders>
      <d2p1:Phone>String</d2p1:Phone>
      <d2p1:PostalCode>String</d2p1:PostalCode>
      <d2p1:Region>String</d2p1:Region>
    </d2p1:Customer>
  </Results>
  <Meta xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfstringstring>
      <d2p1:Key>String</d2p1:Key>
      <d2p1:Value>String</d2p1:Value>
    </d2p1:KeyValueOfstringstring>
  </Meta>
  <ResponseStatus>
    <ErrorCode>String</ErrorCode>
    <Message>String</Message>
    <StackTrace>String</StackTrace>
    <Errors>
      <ResponseError>
        <ErrorCode>String</ErrorCode>
        <FieldName>String</FieldName>
        <Message>String</Message>
        <Meta xmlns:d5p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
          <d5p1:KeyValueOfstringstring>
            <d5p1:Key>String</d5p1:Key>
            <d5p1:Value>String</d5p1:Value>
          </d5p1:KeyValueOfstringstring>
        </Meta>
      </ResponseError>
    </Errors>
    <Meta xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <d3p1:KeyValueOfstringstring>
        <d3p1:Key>String</d3p1:Key>
        <d3p1:Value>String</d3p1:Value>
      </d3p1:KeyValueOfstringstring>
    </Meta>
  </ResponseStatus>
</QueryResponseOfCustomerA703xr44>