123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /*
- * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- namespace JMS\SerializerBundle\Serializer;
- use JMS\SerializerBundle\Serializer\Construction\ObjectConstructorInterface;
- use JMS\SerializerBundle\Serializer\Handler\HandlerRegistryInterface;
- use JMS\SerializerBundle\Serializer\EventDispatcher\EventDispatcherInterface;
- use JMS\SerializerBundle\Exception\UnsupportedFormatException;
- use Metadata\MetadataFactoryInterface;
- use JMS\SerializerBundle\Serializer\Exclusion\VersionExclusionStrategy;
- use JMS\SerializerBundle\Serializer\Exclusion\GroupsExclusionStrategy;
- use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
- use PhpCollection\MapInterface;
- /**
- * Serializer Implementation.
- *
- * @author Johannes M. Schmitt <schmittjoh@gmail.com>
- */
- class Serializer implements SerializerInterface
- {
- private $factory;
- private $handlerRegistry;
- private $objectConstructor;
- private $dispatcher;
- private $typeParser;
- private $serializationVisitors;
- private $deserializationVisitors;
- private $exclusionStrategy;
- private $serializeNull;
- /**
- * Constructor.
- *
- * @param \Metadata\MetadataFactoryInterface $factory
- * @param Handler\HandlerRegistryInterface $handlerRegistry
- * @param Construction\ObjectConstructorInterface $objectConstructor
- * @param \PhpCollection\MapInterface<VisitorInterface> $serializationVisitors
- * @param \PhpCollection\MapInterface<VisitorInterface> $deserializationVisitors
- * @param EventDispatcher\EventDispatcherInterface $dispatcher
- * @param TypeParser $typeParser
- */
- public function __construct(MetadataFactoryInterface $factory, HandlerRegistryInterface $handlerRegistry, ObjectConstructorInterface $objectConstructor, MapInterface $serializationVisitors, MapInterface $deserializationVisitors, EventDispatcherInterface $dispatcher = null, TypeParser $typeParser = null)
- {
- $this->factory = $factory;
- $this->handlerRegistry = $handlerRegistry;
- $this->objectConstructor = $objectConstructor;
- $this->dispatcher = $dispatcher;
- $this->typeParser = $typeParser ?: new TypeParser();
- $this->serializationVisitors = $serializationVisitors;
- $this->deserializationVisitors = $deserializationVisitors;
- $this->serializeNull = false;
- }
- /**
- * @param boolean $serializeNull
- */
- public function setSerializeNull($serializeNull)
- {
- $this->serializeNull = $serializeNull;
- }
- public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy = null)
- {
- $this->exclusionStrategy = $exclusionStrategy;
- }
- /**
- * @param integer $version
- */
- public function setVersion($version)
- {
- if (null === $version) {
- $this->exclusionStrategy = null;
- return;
- }
- $this->exclusionStrategy = new VersionExclusionStrategy($version);
- }
- /**
- * @param null|array $groups
- */
- public function setGroups($groups)
- {
- if ( ! $groups) {
- $this->exclusionStrategy = null;
- return;
- }
- $this->exclusionStrategy = new GroupsExclusionStrategy((array) $groups);
- }
- public function serialize($data, $format)
- {
- $visitor = $this->serializationVisitors->get($format)->get();
- $visitor->setSerializeNull($this->serializeNull);
- $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
- $navigator->accept($visitor->prepare($data), null, $visitor);
- return $visitor->getResult();
- }
- public function deserialize($data, $type, $format)
- {
- $visitor = $this->deserializationVisitors->get($format)->get();
- $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_DESERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
- $navigatorResult = $navigator->accept($visitor->prepare($data), $this->typeParser->parse($type), $visitor);
- // This is a special case if the root is handled by a callback on the object iself.
- if ((null === $visitorResult = $visitor->getResult()) && null !== $navigatorResult) {
- return $navigatorResult;
- }
- return $visitorResult;
- }
- }
|