Learn how to implement serializers and how to dramatically shorten the amount of time and code it takes to implement them
Each serializer middleware package should always contain two classes: a serializer and a deserializer.
You can dramatically shorten the amount of time and code by using the Phapi Serializer Helper classes.
$ php composer.phar require phapi/serializer:1.*
These classes contains a wast majority of the needed code for a serializer. Since there is only two things that is different between different serializers there is no point in writing the same code over and over again in each serializer.
The two things that separates serializers are:
serialize()
or deserialize()
method. Note that the method should throw an InternalServerError if the serialize/deserialize fails.Here's an example that can be used as a starting point for a new serializer:
<?php
namespace Phapi\Middleware\Serializer\Example;
use Phapi\Exception\InternalServerError;
use Phapi\Serializer\Serializer;
class Example extends Serializer
{
/**
* Valid mime types
*
* @var array
*/
protected $mimeTypes = [
'application/example'
];
/**
* Serialize body
*
* @param array $unserializedBody
* @return string
* @throws InternalServerError
*/
protected function serialize(array $unserializedBody = [])
{
}
}
See the JSON serializer and deserializer for a working example.