hshn / serializer-extra-bundle
This bundle provides some extra features for serialization
Installs: 1 477
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- symfony/framework-bundle: ~2.4
Requires (Dev)
Suggests
- liip/imagine-bundle: to apply a filter to uploaded images before serializing a file as an uri
- vich/uploader-bundle: to serialize uploaded files as an uri
This package is not auto-updated.
Last update: 2024-12-03 04:39:56 UTC
README
This bundle provides some extra features for serialization.
Exporting authorities of objects
# app/config.yml hshn_serializer_extra: authority: classes: AcmeBundle\Entity\Blog: attributes: OWNER
/** @var $serializer JMS\Serializer\Serializer */ $json = $serializer->serialize($blog, 'json');
The access authorities provided by AuthorizationCheckerInterface::isGranted()
will be exported to the attribute '_authority' when an object was serialized.
{ "key": "value", "_authority": { "OWNER": true } }
Overriding the attribute name
# app/config.yml hshn_serializer_extra: authority: export_to: "my_authority"
{ "key": "value", "my_authority": { "OWNER": true } }
Restrict exporting the authorities by depth
# app/config.yml hshn_serializer_extra: authority: classes: AcmeBundle\Entity\Blog: attributes: [OWNER] max_depth: 0 # default -1 (unlimited)
class Blog { } class User { /** * @var Blog */ private $blog; } $serializer->serialize($blog, 'json'); // will export the blog authorities (depth 0) $serializer->serialize($user, 'json'); // will NOT export the blog authorities (depth 1)
Export files as URLs
This feature require VichUploaderBundle
# app/config.yml hshn_serializer_extra: vich_uploader: classes: AcmeBundle\Entity\Blog: files: - { property: picture } - { property: picture, export_to: image }
/** @var $serializer JMS\Serializer\Serializer */ $json = $serializer->serialize($blog, 'json');
Generated URLs will be exported when serializing an object.
{ "picture": "/url/to/picture", "image": "/url/to/picture" }
Export images as URLs
This feature require LiipImagineBundle
Adding a filter name specification to a file configuration.
# app/config.yml hshn_serializer_extra: vich_uploader: classes: AcmeBundle\Entity\Blog: files: - { property: picture } - { property: picture, export_to: image, filter: thumbnail }
{ "picture": "/url/to/picture", "image": "/url/to/thumbnail/picture" }