コード例 #1
0
 /**
  * {@inheritDoc}
  */
 public function denormalize($data, ContextInterface $context)
 {
     if (!is_array($data)) {
         throw DenormalizationFailedException::unexpected($data, 'array');
     }
     $collectionClass = $context->getAttribute('collection_class');
     $class = $context->getAttribute('class');
     if (!$class) {
         throw new DenormalizationFailedException('Undefined denormalizer class.');
     }
     try {
         $this->normalizerManager->getNormalizerForClass($class);
     } catch (UnsupportedClassException $e) {
         throw new DenormalizationFailedException(sprintf('Not found normalizer for denormalize collection. Collection class "%s". Denormalizer class "%s".', $collectionClass, $class), 0, $e);
     }
     if ($collectionClass) {
         $reflection = Reflection::loadClassReflection($collectionClass);
         $collection = $reflection->newInstanceWithoutConstructor();
     } else {
         $collection = [];
     }
     if (is_object($collection) && !$collection instanceof \ArrayAccess) {
         throw new DenormalizationFailedException(sprintf('The collection instance for denormalize data should implement "ArrayAccess", but "%s" given.', get_class($collection)));
     }
     foreach ($data as $key => $childData) {
         $denormalized = $this->normalizerManager->denormalize($class, $childData);
         $collection[$key] = $denormalized;
     }
     return $collection;
 }
コード例 #2
0
 /**
  * {@inheritDoc}
  */
 public function denormalize($data, ContextInterface $context)
 {
     if (!is_numeric($data)) {
         throw DenormalizationFailedException::unexpected($data, 'numeric');
     }
     return call_user_func(['FivePercent\\Component\\Money\\Money', $this->createMethod], $data);
 }
コード例 #3
0
 /**
  * {@inheritDoc}
  */
 public function denormalize($data, ContextInterface $context)
 {
     if (!is_string($data)) {
         throw DenormalizationFailedException::unexpected($data, 'string');
     }
     try {
         $datetime = \DateTime::createFromFormat($this->dateTimeFormat, $data);
     } catch (\Exception $e) {
         throw new DenormalizationFailedException('Could not denormalize date time.', 0, $e);
     }
     return $datetime;
 }