/** * Serialize data object to id. * * @param VisitorInterface $visitor * @param object $data * @param array $type * @param Context $context * * @return int|null */ public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context) { $className = $this->manager->getClass(); if ($data instanceof $className) { $galleryHasMedias = $data->getGalleryHasMedias(); /* d($galleryHasMedias); */ $galData = []; foreach ($galleryHasMedias as $galMedia) { $data = $galMedia->getMedia(); $provider = $this->getPool()->getProvider($data->getProviderName()); $format = 'wide'; $format = $provider->getFormatName($data, $format); $options = $provider->getHelperProperties($data, $format, []); /* d($options); */ $options['width'] = null; $options['height'] = null; $html = $this->render($provider->getTemplate('helper_view'), array('media' => $data, 'format' => $format, 'options' => $options)); $galData[] = ['id' => $data->getId(), 'width' => $data->getBox()->getWidth(), 'height' => $data->getBox()->getHeight(), 'url' => $provider->generatePublicUrl($data, $format), 'html' => $html]; } /* dd($galData); */ return $visitor->visitArray($galData, $type, $context); } return; }
public function serializeDateTimeImmutable(VisitorInterface $visitor, \DateTimeInterface $date, array $type, Context $context) { if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) { return $visitor->visitSimpleString($date->format($this->getFormat($type)), $type, $context); } return $visitor->visitString($date->format($this->getFormat($type)), $type, $context); }
/** * @param VisitorInterface $visitor * @param FloatType $data * @param array $type * @param Context $context * * @return mixed|void */ public function serializeFloatType(VisitorInterface $visitor, FloatType $data, array $type, Context $context) { $numberFormatter = new \NumberFormatter($this->getLocale($type), $this->getFormat($type)); if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) { return $visitor->visitSimpleString($numberFormatter->format($data->getValue()), $type, $context); } return $visitor->visitString($numberFormatter->format($data->getValue()), $type, $context); }
/** * Serialize data object to id. * * @param VisitorInterface $visitor * @param object $data * @param array $type * @param Context $context * * @return int|null */ public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context) { $className = $this->manager->getClass(); if ($data instanceof $className) { return $visitor->visitInteger($data->getId(), $type, $context); } return; }
public function serializeDateInterval(VisitorInterface $visitor, \DateInterval $date, array $type, Context $context) { $iso8601DateIntervalString = $this->format($date); if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) { return $visitor->visitSimpleString($iso8601DateIntervalString, $type, $context); } return $visitor->visitString($iso8601DateIntervalString, $type, $context); }
public function deserializeCollection(VisitorInterface $visitor, $data, array $type, Context $context) { // See above. Set parameter type to PropelCollection<T> or PropelCollection<K,V> $type['name'] = 'array'; $collection = new PropelCollection(); $collection->setData($visitor->visitArray($data, $type, $context)); return $collection; }
/** * @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 Task[] $tasks * @param array $type * @param Context $context * @return string * @throws ReferenceException */ public function serialize(VisitorInterface $visitor, $tasks, array $type, Context $context) { $list = []; foreach ($tasks as $task) { if (!$task->getUuid()) { throw new ReferenceException("you can't save a task that has dependencies to tasks that have not been saved"); } $list[] = $task->getUuid(); } return $visitor->visitArray($list, $type, $context); }
/** * Serialize data object to id. * * @param VisitorInterface $visitor * @param object $data * @param array $type * @param Context $context * * @return int|null */ public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context) { $className = $this->manager->getClass(); if ($data instanceof $className) { $provider = $this->getPool()->getProvider($data->getProviderName()); $format = 'archive'; /*$format = 'wide';*/ $format = $provider->getFormatName($data, $format); $data = ['id' => $data->getId(), 'width' => $data->getBox()->getWidth(), 'height' => $data->getBox()->getHeight(), 'url' => $provider->generatePublicUrl($data, $format)]; return $visitor->visitArray($data, $type, $context); } }
public function serializeCollection(VisitorInterface $visitor, Collection $collection, array $type, Context $context) { // We change the base type, and pass through possible parameters. $type['name'] = 'array'; //don't include items that will produce null elements $dataArray = []; foreach ($collection->toArray() as $element) { if (!$context->isVisiting($element)) { $dataArray[] = $element; } } return $visitor->visitArray($dataArray, $type, $context); }
/** * @param VisitorInterface $visitor * @param object $value * @param array $type * @param Context $context * @return mixed */ public function serializeValue(VisitorInterface $visitor, $value, array $type, Context $context) { // We don't know how to serialize, so we're handling the most common types if ($value instanceof Serializable) { $value = $value->serialize(); } elseif (is_callable(array($value, 'toString'))) { $value = call_user_func(array($value, 'toString')); } elseif (is_callable(array($value, '__toString'))) { $value = call_user_func(array($value, '__toString')); } else { $value = ''; } return $visitor->visitString($value, $type, $context); }
/** * Serialize identifier(s). * * Converts an object or collection of objects to an id or array of ids. * * @param VisitorInterface $visitor * @param mixed $data * @param array $type * @param Context $context * @return */ public function serializeIdentifier(VisitorInterface $visitor, $data, array $type, Context $context) { if (!is_object($data)) { return; } if ($this->isIterable($data)) { $identifiers = array(); foreach ($data as $datum) { $identifiers[] = $this->getIdentifier($datum); } return $visitor->visitArray($identifiers, $type, $context); } return $visitor->visitInteger($this->getIdentifier($data), $type, $context); }
/** * @param \JMS\Serializer\VisitorInterface $visitor * @param \JMS\Serializer\Context $context * @return string */ private function getFieldPath(VisitorInterface $visitor, Context $context) { $path = ''; foreach ($context->getMetadataStack() as $element) { if ($element instanceof PropertyMetadata) { $name = $element->serializedName !== null ? $element->serializedName : $element->name; if ($visitor instanceof AbstractVisitor) { $name = $visitor->getNamingStrategy()->translateName($element); } $path = $name . self::PATH_FIELD_SEPARATOR . $path; } } $path = rtrim($path, self::PATH_FIELD_SEPARATOR); return $path; }
public function serializeCollection(VisitorInterface $visitor, Collection $collection, array $type, Context $context) { $viewModelClass = null; if ($this->viewModel !== null) { $viewModelClass = get_class($this->viewModel); } // Only serialize to HalCollection when: // 1. We're not rendering a view model (viewModel is NULL). So we are respecting the defined type. // 2. We're actually rendering to Hal. // Note that we're using the class name because other view models might inherit from HalJsonModel... if ($viewModelClass == 'ZF\\Hal\\View\\HalJsonModel') { return new HalCollection($collection->toArray()); } // We change the base type, and pass through possible parameters. $type['name'] = 'array'; return $visitor->visitArray($collection->toArray(), $type, $context); }
/** * Serialize data object to id. * * @param VisitorInterface $visitor * @param object $data * @param array $type * @param Context $context * * @return int|null */ public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context) { //$data is a -> Media /* dd(func_get_args()); */ $className = $this->manager->getClass(); if ($data instanceof $className) { $provider = $this->getPool()->getProvider($data->getProviderName()); $format_reference = $provider->getFormatName($data, 'reference'); $format_preview = $provider->getFormatName($data, 'prev'); $options = ['width' => null, 'height' => null]; $options = $provider->getHelperProperties($data, $format_reference, $options); /* $html = $this->render($provider->getTemplate('helper_view'), array( */ $html = $this->render('AppBundle:Provider:view_video_api.html.twig', array('media' => $data, 'format' => $format_reference, 'options' => $options)); $data = ['id' => $data->getId(), 'preview' => $provider->generatePublicUrl($data, $format_preview), 'reference' => $provider->generatePublicUrl($data, $format_reference), 'html' => $html]; return $visitor->visitArray($data, $type, $context); } return; return $this->render($provider->getTemplate('helper_view'), array('media' => $media, 'format' => $format, 'options' => $options)); }
/** * @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; }
public function deserializeCollection(VisitorInterface $visitor, $data, array $type, Context $context) { // See above. $type['name'] = 'array'; // When there is not root set for the visitor we need to handle the vector result setting // manually this is related to https://github.com/schmittjoh/serializer/issues/95 $isRoot = null === $visitor->getResult(); if ($isRoot && $visitor instanceof GenericDeserializationVisitor) { $metadata = new ClassMetadata(Vector::class); $vector = new Vector(); $visitor->startVisitingObject($metadata, $vector, $type, $context); $array = $visitor->visitArray($data, $type, $context); $vector->setAll($array); $visitor->endVisitingObject($metadata, $vector, $type, $context); return $vector; } // No a root so just return the vector return new Vector($visitor->visitArray($data, $type, $context)); }
public function deserializeCollection(VisitorInterface $visitor, $data, array $type, Context $context) { // See above. $type['name'] = 'array'; return new ArrayCollection($visitor->visitArray($data, $type, $context)); }
public function deserializeStdClass(VisitorInterface $visitor, $data, array $type, Context $context) { // See above. $type['name'] = 'array'; return (object) $visitor->visitArray($data, $type, $context); }
/** * @param VisitorInterface $visitor * @param Carbon $date * @param array $type * @param Context $context * @return string */ public function serializeCarbon(VisitorInterface $visitor, Carbon $date, array $type, Context $context) { $date = clone $date; $date->setTimezone($this->defaultTimezone); return $visitor->visitString($date->format($this->getFormat($type)), $type, $context); }
public function serializeUuid(VisitorInterface $visitor, UuidInterface $uuid, array $type, Context $context) { return $visitor->visitString($uuid->toString(), $type, $context); }
/** * Serialize a phone number. * * @param VisitorInterface $visitor Serialization visitor. * @param PhoneNumber $phoneNumber Phone number. * @param array $type Type. * @param mixed $context Context. * * @return mixed Serialized phone number. */ public function serializePhoneNumber(VisitorInterface $visitor, PhoneNumber $phoneNumber, array $type, $context) { $formatted = $this->phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::E164); return $visitor->visitString($formatted, $type, $context); }
public function serializeDateInterval(VisitorInterface $visitor, \DateInterval $date, array $type, Context $context) { $iso8601DateIntervalString = $this->format($date); return $visitor->visitString($iso8601DateIntervalString, $type, $context); }
/** * @param $in * @param VisitorInterface $visitor * @param DeserializationContext $context * @return DirectoryEntries */ public function read($in, VisitorInterface $visitor, DeserializationContext $context) { $data = $context->accept($in, $this->getType()); $visitor->setNavigator($context->getNavigator()); return new DirectoryEntries($data); }
public function serializeMetadata(VisitorInterface $visitor, Metadata $metadata, array $type, Context $context) { return $visitor->visitArray($metadata->toArray(), $type, $context); }
public function deserializeSequence(VisitorInterface $visitor, $data, array $type) { // See above. $type['name'] = 'array'; return new Sequence($visitor->visitArray($data, $type)); }
public function serializeDateTimeStamp(VisitorInterface $visitor, \DateTime $date, array $type, Context $context) { return (int) $visitor->visitString($date->format('U'), $type, $context); }
public function serializeCurrencyToJson(VisitorInterface $visitor, Currency $currency, array $type, Context $context) { return $visitor->visitString($date->format($this->getField($type)), $type, $context); }
public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, array $type, Context $context) { return $visitor->visitString($date->format($this->getFormat($type)), $type, $context); }
/** * Called for each node of the graph that is being traversed. * * @param mixed $data the data depends on the direction, and type of visitor * @param null|array $type array has the format ["name" => string, "params" => array] * @param VisitorInterface $visitor * * @return mixed the return value depends on the direction, and type of visitor */ public function accept($data, array $type = null, VisitorInterface $visitor) { // If the type was not given, we infer the most specific type from the // input data in serialization mode. if (null === $type) { if (!$this->context->isSerializing()) { $msg = 'The type must be given for all properties when deserializing.'; if (null !== ($path = $this->context->getPath())) { $msg .= ' Path: ' . $path; } throw new \RuntimeException($msg); } $typeName = gettype($data); if ('object' === $typeName) { $typeName = get_class($data); } $type = array('name' => $typeName, 'params' => array()); } else { if ($this->context->isSerializing() && null === $data) { $type = array('name' => 'NULL', 'params' => array()); } } switch ($type['name']) { case 'NULL': return $visitor->visitNull($data, $type); case 'string': return $visitor->visitString($data, $type); case 'integer': return $visitor->visitInteger($data, $type); case 'boolean': return $visitor->visitBoolean($data, $type); case 'double': case 'float': return $visitor->visitDouble($data, $type); case 'array': return $visitor->visitArray($data, $type); case 'resource': $msg = 'Resources are not supported in serialized data.'; if (null !== ($path = $this->context->getPath())) { $msg .= ' Path: ' . $path; } throw new \RuntimeException($msg); default: $isSerializing = $this->context->isSerializing(); if ($isSerializing && null !== $data) { if ($this->context->isVisiting($data)) { return null; } $this->context->startVisiting($data); } // First, try whether a custom handler exists for the given type. This is done // before loading metadata because the type name might not be a class, but // could also simply be an artifical type. if (null !== ($handler = $this->handlerRegistry->getHandler($this->context->getDirection(), $type['name'], $this->context->getFormat()))) { $rs = call_user_func($handler, $visitor, $data, $type); $this->context->stopVisiting($data); return $rs; } // Trigger pre-serialization callbacks, and listeners if they exist. if ($isSerializing) { if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $this->context->getFormat())) { $this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $this->context->getFormat(), $event = new PreSerializeEvent($visitor, $data, $type)); $type = $event->getType(); } } // Load metadata, and check whether this class should be excluded. $metadata = $this->metadataFactory->getMetadataForClass($type['name']); if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata, $this->context)) { $this->context->stopVisiting($data); return null; } if ($isSerializing) { foreach ($metadata->preSerializeMethods as $method) { $method->invoke($data); } } $object = $data; if (!$isSerializing) { $object = $this->objectConstructor->construct($visitor, $metadata, $data, $type); } if (isset($metadata->handlerCallbacks[$this->context->getDirection()][$this->context->getFormat()])) { $rs = $object->{$metadata->handlerCallbacks[$this->context->getDirection()][$this->context->getFormat()]}($visitor, $isSerializing ? null : $data); $this->afterVisitingObject($visitor, $metadata, $object, $type); return $isSerializing ? $rs : $object; } $visitor->startVisitingObject($metadata, $object, $type); foreach ($metadata->propertyMetadata as $propertyMetadata) { if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata, $this->context)) { continue; } if (!$isSerializing && $propertyMetadata->readOnly) { continue; } $visitor->visitProperty($propertyMetadata, $data); } if ($isSerializing) { $this->afterVisitingObject($visitor, $metadata, $data, $type); return $visitor->endVisitingObject($metadata, $data, $type); } $rs = $visitor->endVisitingObject($metadata, $data, $type); $this->afterVisitingObject($visitor, $metadata, $rs, $type); return $rs; } }