Example #1
0
 /**
  * @param VisitorInterface $visitor
  * @param array $data
  * @param array $type
  * @param Context $context
  *
  * @return PageBridge
  */
 public function doDeserialize(VisitorInterface $visitor, array $data, array $type, Context $context)
 {
     $document = $context->accept($data['document'], ['name' => PageDocument::class]);
     $structure = $this->structureFactory->getStructureMetadata('page', $data['structure']);
     $bridge = new PageBridge($structure, $this->inspector, $this->propertyFactory, $document);
     // filthy hack to set the Visitor::$result to null and force the
     // serializer to return the Bridge and not the Document
     $visitor->setNavigator($context->getNavigator());
     return $bridge;
 }
 /**
  * @param VisitorInterface $visitor
  * @param mixed            $data
  * @param array            $type
  * @param Context          $context
  * @return array|null
  * @throws RuntimeException If $data contains more elements than $type['params']
  */
 public function deserializeJobParameterArray(VisitorInterface $visitor, $data, array $type, Context $context)
 {
     /**
      * If $type['params'] is not set this most likely means, that a job is being deserialized, so we check if the JobDeserializationSubscriber set the type of params at the end of the $data array
      *
      * @see JobDeserializationSubscriber::onPreDeserialize()
      */
     $deserializeJob = false;
     if (count($type['params']) == 0 && is_array($data) && is_array(end($data)) && in_array('abc.job.type', array_keys(end($data)))) {
         $jobType = $this->extractJobType($data);
         $type['params'] = $this->getParamTypes($jobType);
         $deserializeJob = true;
     }
     if (is_array($data) && count($data) > count($type['params'])) {
         throw new RuntimeException(sprintf('Invalid job parameters, the parameters contain more elements that defined (%s)', implode(',', $type['params'])));
     }
     $result = [];
     for ($i = 0; $i < count($type['params']); $i++) {
         if (!is_array($data) || !isset($data[$i]) || null == $data[$i]) {
             $result[$i] = null;
         } else {
             if (!is_array($type['params'][$i])) {
                 $type['params'][$i] = ['name' => $type['params'][$i], 'params' => array()];
             }
             $result[$i] = $context->accept($data[$i], $type['params'][$i]);
         }
     }
     if (count($data) > 0 && !$deserializeJob) {
         /**
          * Since serializer always returns the result of $context->accept unless visitor result is empty,
          * we have to make sure that the visitor result is null in case only root is type JobParameterArray::class
          *
          * @see Serializer::handleDeserializeResult()
          */
         $visitor->setNavigator($context->getNavigator());
     }
     return $result;
 }