Implementing your own Serializer

edit

Implementing your own Serializer

edit

If you want to use your own custom serializer, you need to implement the SerializerInterface interface. Please keep in mind that the client uses a single Serializer object for all endpoints and all connections.

class MyCustomSerializer implements SerializerInterface
{

    /**
     * Serialize request body
     *
     * @param string|array $data Request body
     *
     * @return string
     */
    public function serialize($data)
    {
        // code here
    }

    /**
     * Deserialize response body
     *
     * @param string $data Response body
     * @param array  $headers Response Headers
     *
     * @return array|string
     */
    public function deserialize($data, $headers)
    {
        // code here
    }
}


To then use your custom serializer, you can specify the namespace path in the setSerializer() method of the ClientBuilder object:

$client = ClientBuilder::create()
            ->setSerializer('\MyProject\Serializers\MyCustomSerializer');
            ->build();

Alternatively, if your serializer has a constructor or further initialization that should occur before given to the client, you can instantiate an object and provide that instead:

$mySerializer = new MyCustomSerializer($a, $b, $c);
$mySerializer->setFoo("bar");

$client = ClientBuilder::create()
            ->setSerializer($mySerializer);
            ->build();