/** * @param AbstractVisitor $visitor * @param array $data * @param string $type * @param Context $context * @return Envelope */ public function deserializeMessage(AbstractVisitor $visitor, array $data, $type, Context $context) { $data['class'] = bernard_decode_class_string($data['class']); $type = ['name' => $data['class'], 'params' => null]; $message = new BernardTokenMessage($data['name'], $context->accept($data['args'], $type)); return $message; }
public function simpleListOfFromXml(XmlDeserializationVisitor $visitor, $node, array $type, Context $context) { $newType = array('name' => $type["params"][0]["name"], 'params' => array()); $ret = array(); foreach (explode(" ", (string) $node) as $v) { $ret[] = $context->accept($v, $newType); } return $ret; }
/** * @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 AbstractVisitor $visitor * @param array $data * @param string $type * @param Context $context * @return Envelope */ public function deserializeToken(AbstractVisitor $visitor, array $data, $type, Context $context) { $token = new Token(); $token->setLocation($data['location']); foreach ($data['data'] as $datum) { $type = $datum['type']; if ($type['name'] === 'array') { $argsData = []; foreach ($datum['args'] as $arrayData) { $arrayKey = $arrayData['key']; $argsData[$arrayKey] = $context->accept($arrayData['args'], $arrayData['type']); } } else { $argsData = $context->accept($datum['args'], $type); } $token->setData($datum['key'], $argsData); } return $token; }
/** * Transforms root of visitor with additional data based on the representation. * * @param PaginatedRepresentation $representation * @param JsonApiSerializationVisitor $visitor * @param Context $context * * @return mixed */ protected function transformRoot(PaginatedRepresentation $representation, JsonApiSerializationVisitor $visitor, Context $context) { // serialize items $data = $context->accept($representation->getItems()); $root = $visitor->getRoot(); $root['meta'] = array('page' => $representation->getPage(), 'limit' => $representation->getLimit(), 'pages' => $representation->getPages(), 'total' => $representation->getTotal()); $root['links'] = array('first' => $this->getUriForPage(1), 'last' => $this->getUriForPage($representation->getPages()), 'next' => $representation->hasNextPage() ? $this->getUriForPage($representation->getNextPage()) : null, 'previous' => $representation->hasPreviousPage() ? $this->getUriForPage($representation->getPreviousPage()) : null); $visitor->setRoot($root); return $data; }
public function resolveResponseContentType(XmlDeserializationVisitor $visitor, $data, array $type, Context $context) { $operation = $visitor->getCurrentObject()->getHead()->getOperation(); switch ($operation) { case OperationType::OPERATION_PAYMENT_INIT: $type['name'] = 'PHPCommerce\\Vendor\\RatePAY\\Service\\Payment\\Type\\Response\\PaymentInitResponseType'; return $context->accept($data, $type); break; case OperationType::OPERATION_PAYMENT_REQUEST: $type['name'] = 'PHPCommerce\\Vendor\\RatePAY\\Service\\Payment\\Type\\Response\\PaymentRequestResponseType'; return $context->accept($data, $type); break; case OperationType::OPERATION_PAYMENT_CONFIRM: $type['name'] = 'PHPCommerce\\Vendor\\RatePAY\\Service\\Payment\\Type\\Response\\PaymentConfirmResponseType'; return $context->accept($data, $type); break; case OperationType::OPERATION_PAYMENT_CHANGE: $type['name'] = 'PHPCommerce\\Vendor\\RatePAY\\Service\\Payment\\Type\\Response\\PaymentChangeResponseType'; return $context->accept($data, $type); break; case OperationType::OPERATION_CONFIRMATION_DELIVER: $type['name'] = 'PHPCommerce\\Vendor\\RatePAY\\Service\\Payment\\Type\\Response\\ConfirmationDeliverResponseType'; return $context->accept($data, $type); break; case OperationType::OPERATION_CONFIGURATION_REQUEST: $type['name'] = 'PHPCommerce\\Vendor\\RatePAY\\Service\\Payment\\Type\\Response\\ConfigurationResponseType'; return $context->accept($data, $type); break; default: throw new RuntimeException("Unknown Operation: " . $operation); break; } }
/** * @param JsonApiSerializationVisitor $visitor * @param Pagerfanta $pagerfanta * @param array $type * @param Context $context * @return Pagerfanta */ public function serializePagerfanta(JsonApiSerializationVisitor $visitor, Pagerfanta $pagerfanta, array $type, Context $context) { $request = $this->requestStack->getCurrentRequest(); $pagerfanta->setNormalizeOutOfRangePages(true); $pagerfanta->setAllowOutOfRangePages(true); $pagerfanta->setMaxPerPage($request->get('page[limit]', $this->paginationOptions['limit'], true)); $pagerfanta->setCurrentPage($request->get('page[number]', 1, true)); $results = $pagerfanta->getCurrentPageResults(); if ($results instanceof \ArrayIterator) { $results = $results->getArrayCopy(); } $data = $context->accept($results); $root = $visitor->getRoot(); $root['meta'] = array('page' => $pagerfanta->getCurrentPage(), 'limit' => $pagerfanta->getMaxPerPage(), 'pages' => $pagerfanta->getNbPages(), 'total' => $pagerfanta->getNbResults()); $root['links'] = array('first' => $this->getUriForPage(1), 'last' => $this->getUriForPage($pagerfanta->getNbPages()), 'prev' => $pagerfanta->hasPreviousPage() ? $this->getUriForPage($pagerfanta->getPreviousPage()) : null, 'next' => $pagerfanta->hasNextPage() ? $this->getUriForPage($pagerfanta->getNextPage()) : null); $visitor->setRoot($root); return $data; }
public function serializeFormViewToJson(JsonSerializationVisitor $visitor, FormView $formView, array $type, Context $context) { $output = array(); if (!$formView->vars['valid']) { if ($formView->vars['errors']) { foreach ($formView->vars['errors'] as $error) { $output['global'] = $error->getMessage(); } } foreach ($formView->children as $fieldName => $child) { if (!$child->vars['valid']) { foreach ($child->vars['errors'] as $error) { $output[$fieldName] = $error->getMessage(); } } } } return $context->accept($output); }
/** * @param $object * @param Relationship $relationship * @param Context $context * * @return array */ protected function processRelationship($object, Relationship $relationship, Context $context) { if (null === $object) { return null; } if (!is_object($object)) { throw new \RuntimeException(sprintf('Cannot process relationship "%s", because it is not an object but a %s.', $relationship->getName(), gettype($object))); } /** @var ClassMetadata $relationshipMetadata */ $relationshipMetadata = $this->hateoasMetadataFactory->getMetadataForClass(get_class($object)); if (null === $relationshipMetadata) { throw new \RuntimeException(sprintf('Metadata for class %s not found. Did you define at as a JSON-API resource?', ClassUtils::getRealClass(get_class($object)))); } $relationshipId = $this->getId($relationshipMetadata, $object); // contains the relations type and id $relationshipDataArray = $this->getRelationshipDataArray($relationshipMetadata, $relationshipId); // only include this relationship if it is needed if ($relationship->isIncludedByDefault() && $this->canIncludeRelationship($relationshipMetadata, $relationshipId)) { $includedRelationship = $relationshipDataArray; // copy data array so we do not override it with our reference $this->includedRelationships[] =& $includedRelationship; $includedRelationship = $context->accept($object); // override previous reference with the serialized data } // the relationship data can only contain one reference to another resource return $relationshipDataArray; }
/** * @param JsonSerializationVisitor $visitor * @param ExtensionContainer $container * @param array $type * @param Context $context * * @return mixed */ public function doSerialize(JsonSerializationVisitor $visitor, ExtensionContainer $container, array $type, Context $context) { return $context->accept($container->toArray()); }
/** * @param JsonSerializationVisitor $visitor * @param NodeInterface $nodeInterface * @param array $type * @param Context $context */ public function doSerialize(JsonSerializationVisitor $visitor, Structure $container, array $type, Context $context) { $array = $container->toArray(); return $context->accept($array); }
/** * @param JsonSerializationVisitor $visitor * @param NodeInterface $resourceInterface * @param array $type * @param Context $context */ public function serializeResource(JsonSerializationVisitor $visitor, Resource $resource, array $type, Context $context) { $data = $this->doSerializeResource($resource); $context->accept($data); }
/** * @param JsonSerializationVisitor $visitor * @param ChildrenCollection $childrenCollection * @param array $type * @param Context $context * * @return mixed */ public function doSerialize(JsonSerializationVisitor $visitor, ChildrenCollection $childrenCollection, array $type, Context $context) { $array = $childrenCollection->toArray(); return $context->accept($array); }
/** * Returns custom-url data with the connected locales. * * @param Portal $portal * @param Environment $environment * @param Context $context * * @return array */ private function getCustomUrlsForEnvironment(Portal $portal, Environment $environment, Context $context) { $customUrls = []; foreach ($environment->getCustomUrls() as $customUrl) { $customUrl = $context->accept($customUrl); $customUrl['locales'] = $context->accept($portal->getLocalizations()); $customUrls[] = $customUrl; } return $customUrls; }
/** * @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; }