jms / serializer-service-provider
Silex Service Provider for the Serializer Bundle
Installs: 7 893
Dependents: 2
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 11
Open Issues: 6
pkg:composer/jms/serializer-service-provider
Requires
- php: >=5.3.3
- jms/serializer-bundle: 0.9.*
- silex/silex: dev-master
- symfony/routing: 2.1.*
Requires (Dev)
- doctrine/common: >=2.1,<2.4-dev
This package is not auto-updated.
Last update: 2025-10-25 18:41:08 UTC
README
The SerializerServiceProvider provides a service for serializing
objects. This service provider uses the JMS\SerializerBundle for
serializing.
Parameters
- 
serializer.src_directory: The directory where theJMS\SerializerBundlesource is located.
- 
serializer.cache.directory: The directory to use for storing the metadata cache.
- 
serializer.naming_strategy.seperator(optional): The separator string used when normalizing properties.
- 
serializer.naming_strategy.lower_case(optional): Boolean flag indicating if the properties should be normalized as lower case strings.
- 
serializer.date_time_handler.format(optional): The format used to serialize and deserializeDateTimeobjects. Refer to the PHP documentation for supported Date/Time formats.
- 
serializer.date_time_handler.default_timezone(optional): The timezone to use when serializing and deserializingDateTimeobjects. Refer to the PHP documentation for a list of supported timezones.
- 
serializer.disable_external_entities(optional): Boolean flag indicating if the serializer should disable external entities for the XML serialization format.
Services
- serializer: An instance of- JMS\SerializerBundle\Serializer\Serializer.
Registering
<?php $app = new Silex\Application(); $app->register(new JMS\SerializerServiceProvider\SerializerServiceProvider(), array( 'serializer.src_directory' => 'path/to/vendor/jms/serializer-bundle/src', 'serializer.cache.directory' => 'path/to/cache' ));
Usage
Annotate the class you wish to serialize, refer to the annotation documentation
<?php use JMS\SerializerBundle\Annotation; // The serializer bundle doesn't need getters or setters class Page { /** * @Type("integer") */ private $id; /** * @Type("string") */ private $title; /** * @Type("string") */ private $body; /** * @Type("DateTime") */ private $created; /** * @Type("Author") */ private $author; /** * @Type("boolean") */ private $featured; }
<?php use JMS\SerializerBundle\Annotation; // The serializer bundle doesn't need getters or setters class Author { /** * @Type("string") */ private $name; }
The SerializerServiceProvider provider provides a serializer
service. Use it in your application to serialize and deserialize your
objects:
<?php use Silex\Application; use JMS\SerializerServiceProvider\SerializerServiceProvider; use Symfony\Component\HttpFoundation\Response; $app = new Application(); // Make sure that the PHP script can write in the cache directory and that // the directory exists. $app->register(new SerializerServiceProvider(), array( 'serializer.src_directory' => __DIR__."/../vendor/jms/serializer-bundle/src", 'serializer.cache.directory' => __DIR__."/../cache/serializer" )); // only accept content types supported by the serializer via the assert method. $app->get("/pages/{id}.{_format}", function ($id) use ($app) { // assume a page_repository service exists that returns Page objects. $page = $app['page_repository']->find($id); $format = $app['request']->getFormat(); if (!$page instanceof Page) { $this->abort("No page found for id: $id"); } return new Response($app['serializer']->serialize($page, $format), 200, array( "Content-Type" => $app['request']->getMimeType($format) )); })->assert("_format", "xml|json") ->assert("id", "\d+");
License:
This service provider is available under the MIT LICENSE. Please note
that the required JMSSerializerBundle is made available under the Apache 2 LICENCE.
Credits:
Allow me to thank Johannes Schmitt (@schmittjoh) for making the
JMSSerializerBundle.