Test

<back to all web services

HelloWithGenericInheritance

<?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};


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

    public function __construct(
        /** @var array<T>|null */
        public mixed $items=null,
        /** @var array<int>|null */
        public mixed $counts=null
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['items'])) $this->items = JsonConverters::fromArray($this->genericArgs[0], $o['items']);
        if (isset($o['counts'])) $this->counts = JsonConverters::fromArray('int', $o['counts']);
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->items)) $o['items'] = JsonConverters::toArray($this->genericArgs[0], $this->items);
        if (isset($this->counts)) $o['counts'] = JsonConverters::toArray('int', $this->counts);
        return empty($o) ? new class(){} : $o;
    }
}

class Poco implements JsonSerializable
{
    public function __construct(
        /** @var string|null */
        public ?string $name=null
    ) {
    }

    /** @throws Exception */
    public function fromMap($o): void {
        if (isset($o['name'])) $this->name = $o['name'];
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = [];
        if (isset($this->name)) $o['name'] = $this->name;
        return empty($o) ? new class(){} : $o;
    }
}

/**
 * @template HelloBase of Poco
 */
class HelloWithGenericInheritance extends HelloBase implements JsonSerializable
{
    /**
     * @param array<T>|null $items
     * @param array<int>|null $counts
     */
    public function __construct(
        ?array $items=null,
        ?array $counts=null,
        /** @var string|null */
        public ?string $result=null
    ) {
        parent::__construct($items,$counts);
    }

    /** @throws Exception */
    public function fromMap($o): void {
        parent::fromMap($o);
        if (isset($o['result'])) $this->result = $o['result'];
    }
    
    /** @throws Exception */
    public function jsonSerialize(): mixed
    {
        $o = parent::jsonSerialize();
        if (isset($this->result)) $o['result'] = $this->result;
        return empty($o) ? new class(){} : $o;
    }
}

PHP HelloWithGenericInheritance DTOs

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

To embed the response in a jsonp callback, append ?callback=myCallback

HTTP + JSON

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

POST /json/oneway/HelloWithGenericInheritance HTTP/1.1 
Host: test.servicestack.net 
Accept: application/json
Content-Type: application/json
Content-Length: length

{"result":"String","items":[{"name":"String"}],"counts":[0]}