Download the PHP package dbstudios/doze without Composer
On this page you can find all versions of the php package dbstudios/doze. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dbstudios/doze
More information about dbstudios/doze
Files in dbstudios/doze
Package doze
Short Description An easy to use library for building REST APIs
License GPL-3.0
Informations about the package doze
Installation
Basic Configuration
Doze only requires two things to function out of the box: a serializer, and a responder. For the serializer, Doze makes
use of the symfony/serializer
package. You can find more
information on using it's features in their README, but a simple example can be found below.
In the example, we first create a new serializer instance. The first argument to the constructor is an array of
normalizers, which the serializer uses to simplify data prior to serialization. Both DateTimeNormalizer
and
ObjectNormalizer
are shipped with symfony/serializer
, and are used to normalize DateTime
and generic objects,
respectively. The second argument is a list of supported encoder formats. Our example only supports JSON.
Next, we create a new Responder
instance, and call it's createResponse()
method to obtain our Response
object,
which contains things like the headers, HTTP status code, and the response body. Please see the
symfony/httpfoundation
package for more information on the
Response
object.
Field Selector Notation
Sometimes, a developer using your API may only want or need a few specific fields from an object or array. To that end, Doze implements a selector notation that can be used to limit which fields are serialized and returned.
When calling Responder::createResponse()
, you can provide an "attributes" key, which tells the serializer which
attributes should be serialized and returned. Using the "attributes" context key is documented in the serializer package,
but Doze also supports an alternative method of building the attributes filter.
In the example below, $responder
is the Responder
instance we created in the previous example.
Since the fields selector functionality is intended to be exposed to the end user of your API, we're retrieving the
selector from the $_GET
global variable.
Notice that the JSON output by $response->getContent()
does not contain the field "someOtherField", which was not part
of the selector.
Field selectors can also contain nested selectors. In the example below, assume that $data
has a field named
"nestedObject", which has the fields "name" and "property".
Serializing Database Objects
Objects loaded from the database by certain libraries (such as Doctrine cannot be cleanly serialized, since they automatically link to related objects. In some cases, serializing a database entity can result in a huge tree of child entities being serialized as well. This causes a lot of unnecessary data to be sent across the wire.
Doze provides a special normalizer, the EntityNormalizer
, that is aware of such database entities (via the
EntityInterface
interface), and will serialize ONLY the entities ID unless it is explicitly requested using
field selectors. To use it, any classes that are used by your DBAL must implement the
EntityInterface
, and the EntityNormalizer
must be in your serializer's normalizer stack above any other
normalizers that might accidentally try to serialize your objects (such as the ObjectNormalizer
used in previous
examples).
With the extra normalizer added, any objects that implement EntityInterface
will have ONLY their ID serialized,
instead of the entire object, and any child objects. This can be overwritten by using explicitly selecting the
entity (and, optionally, and of it's fields) using the Field Selector Notation.
Paged Collections
Coming soon.
All versions of doze with dependencies
symfony/http-foundation Version ^4.0
sensio/framework-extra-bundle Version ^5.0
symfony/serializer Version ^4.0
symfony/property-access Version ^4.0
dbstudios/doctrine-entities Version ^1.0