/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if ($object instanceof PuliResource) { $event->setType('Puli\\Repository\\Api\\Resource\\PuliResource'); } }
/** * remove translateable strings from object * * @param PreSerializeEvent $event event * * @return void */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if (!$object instanceof TranslatableDocumentInterface) { return; } try { $hash = \spl_object_hash($object); $this->localizedFields[$hash] = []; foreach ($object->getTranslatableFields() as $field) { $isArray = substr($field, -2, 2) === '[]'; $method = $isArray ? substr($field, 0, -2) : $field; $setter = 'set' . ucfirst($method); $getter = 'get' . ucfirst($method); if (!method_exists($object, $setter) || !method_exists($object, $getter)) { continue; } // only allow objects that we can update during postSerialize $value = $object->{$getter}(); if ($isArray && !empty($value) || !$isArray && $value != null) { $this->localizedFields[$hash][$field] = $value; // remove untranslated field to make space for translation struct $object->{$setter}(null); } } } catch (\Doctrine\ODM\MongoDB\DocumentNotFoundException $e) { // @todo if a document references a non-existing document - handle it so it renders to null! } }
public function onPreSerialize(PreSerializeEvent $event) { $person = $event->getObject(); if ($person instanceof PersonInterface) { $this->handler->evaluate($person); } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if ($object instanceof ExtensionContainer) { $event->setType(ExtensionContainer::class); } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if ($object instanceof LazyLoadingInterface) { $event->setType(ClassNameInflector::getUserClassName(get_class($object))); } }
public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); $type = $event->getType(); // If the set type name is not an actual class, but a faked type for which a custom handler exists, we do not // modify it with this subscriber. Also, we forgo autoloading here as an instance of this type is already created, // so it must be loaded if its a real class. $virtualType = !class_exists($type['name'], false); if ($object instanceof PersistentCollection) { if (!$virtualType) { $event->setType('ArrayCollection'); } return; } if (!$object instanceof Proxy && !$object instanceof ORMProxy) { return; } // following lines are commented to stop lazy loading associations // if(!$object->__isInitialized()){ // $object->__load(); // } if (!$virtualType) { $event->setType(get_parent_class($object)); } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if ($object instanceof NodeInterface) { $event->setType('PHPCR\\NodeInterface'); } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if ($object instanceof Structure) { $event->setType(Structure::class); } }
/** * {@inheritDoc} */ public function onPreSerialize(PreSerializeEvent $event) { $model = $event->getObject(); Globals::setUploadDir($this->uploadDir); if ($model instanceof ImageInterface) { $model->generateThumbs($this->liipManager); } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Flag $user */ $flag = $event->getObject(); if (!$flag instanceof Flag) { return; } $this->hydratePictureAndThumb($flag); }
public function onPreSerialize(PreSerializeEvent $event) { $type = $event->getType(); // A HalCollection type mustn't ever be serialized as ArrayCollection for Doctrine's // {*}Collection objects (see parent class). if ($type['name'] == 'ZF\\Hal\\Collection') { return; } parent::onPreSerialize($event); }
public function onPreSerialize(PreSerializeEvent $event) { $person = $event->getObject(); if ($person instanceof PersonInterface) { $imgHelper = $this->uploaderHelper; $templateHelper = $this->templateHelper; $isDev = $this->kernel->getEnvironment() === 'dev'; $person->prepareAPISerialize($imgHelper, $templateHelper, $isDev, $this->request); } }
/** * @param PreSerializeEvent $event * @deprecated с 10.09.2014 */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Campaign $campaign */ $campaign = $event->getObject(); $attribures = $event->getContext()->attributes; $groups = $attribures->get('groups')->get(); if (in_array('own_detailed', $groups)) { $linkedPlatforms = $this->em->getRepository('VifeedVideoViewBundle:VideoView')->getCampaignPlatformsCount($campaign); $campaign->setSocialData('linkedPlatforms', $linkedPlatforms); } }
/** * @param Event $event */ public function onPreSerialize(PreSerializeEvent $event) { foreach ($event->getObject()->getCategories() as $category) { $category->setCertification(null); foreach ($category->getQuestions() as $question) { $question->setCategory(null); foreach ($question->getAnswers() as $answer) { $answer->setQuestion(null); } } } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Tag $tag */ $tag = $event->getObject(); if (!$tag instanceof Tag) { return; } if (in_array($tag->getId(), $this->serializedEntities)) { return; } $this->serializedEntities[] = $tag->getId(); $tag->setGroupName($this->tagGroupRepository->getGroupNameById($tag->getGroupId())); }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Notification $user */ $notification = $event->getObject(); if (!$notification instanceof Notification) { return; } if (in_array($notification->getId(), $this->serializedEntities)) { return; } $this->serializedEntities[] = $notification->getId(); $this->hydratePicture($notification); }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Restaurant $user */ $restaurant = $event->getObject(); if (!$restaurant instanceof Restaurant) { return; } if (in_array($restaurant->getId(), $this->serializedEntities)) { return; } $this->serializedEntities[] = $restaurant->getId(); $this->hydratePictureAndThumb($restaurant); }
public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if ($object instanceof PersistentCollection) { $event->setType('ArrayCollection'); return; } if (!$object instanceof Proxy && !$object instanceof ORMProxy) { return; } $object->__load(); $event->setType(get_parent_class($object)); }
public function onPreSerialize(PreSerializeEvent $event) { /** @var object $object */ $object = $event->getObject(); if (!is_object($object)) { return; } $type = $event->getType(); $polymorphicTypes = array('Xabbuh\\XApi\\Model\\Actor', 'Xabbuh\\XApi\\Model\\Object'); if (in_array($type['name'], $polymorphicTypes)) { $event->setType(get_class($object)); } }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var User $user */ $user = $event->getObject(); if (!$user instanceof User) { return; } if (in_array($user->getId(), $this->serializedEntities)) { return; } $this->serializedEntities[] = $user->getId(); $this->hydrateAvatar($user); $this->hydrateWithSerializationGroups($user); }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Photo $photo */ $photo = $event->getObject(); if (!$photo instanceof Photo) { return; } if (in_array($photo->getId(), $this->serializedEntities)) { return; } $this->serializedEntities[] = $photo->getId(); $photo->setSrc($this->cloudfrontUrl . $photo->getSrc()); $photo->setThumb($this->cloudfrontUrl . $photo->getThumb()); }
/** * @param PreSerializeEvent $event */ public function onPreSerialize(PreSerializeEvent $event) { /** @var Geoname $geoname */ $geoname = $event->getObject(); if (!$geoname instanceof Geoname) { return; } if (in_array($geoname->getId(), $this->serializedEntities)) { return; } $this->serializedEntities[] = $geoname->getId(); if ($geoname->getCountry() === 'US') { $geoname->setFormattedName($geoname->getAsciiName() . ', ' . $geoname->getAdmin1()); } else { $geoname->setFormattedName($geoname->getAsciiName() . ' (' . $geoname->getCountry() . ')'); } }
public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); $type = $event->getType(); // If the set type name is not an actual class, but a faked type for which a custom handler exists, we do not // modify it with this subscriber. Also, we forgo autoloading here as an instance of this type is already created, // so it must be loaded if its a real class. if (!class_exists($type['name'], false)) { return; } if ($object instanceof PersistentCollection || $object instanceof MongoDBPersistentCollection || $object instanceof PHPCRPersistentCollection) { $event->setType('ArrayCollection'); return; } if (!$object instanceof Proxy && !$object instanceof ORMProxy) { return; } $object->__load(); $event->setType(get_parent_class($object)); }
public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); $env = $this->container->get('kernel')->getEnvironment(); if ($object instanceof Image) { if ($env === 'prod') { $object->setDownloadUrl($this->getS3Url() . '/' . $object->getImageName()); } else { $object->setDownloadUrl('client/app/images/voitures/' . $object->getImageName()); } } if ($object instanceof Voiture) { if (!$object->getImagePrincipale()) { if ($env === 'prod') { $object->setDefaultPathImagePrincipale($this->getS3Url() . '/defaultVoiture.png'); } else { $object->setDefaultPathImagePrincipale('client/app/images/voitures/defaultVoiture.png'); } } } }
/** * remove translateable strings from object * * @param PreSerializeEvent $event event * * @return void */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); $this->localizedFields[\spl_object_hash($object)] = array(); try { if ($object instanceof TranslatableDocumentInterface) { foreach ($object->getTranslatableFields() as $field) { $setter = 'set' . ucfirst($field); $getter = 'get' . ucfirst($field); // only allow objects that we can update during postSerialize if (method_exists($object, $setter) && $object->{$getter}() != null) { $this->localizedFields[\spl_object_hash($object)][$field] = $object->{$getter}(); // remove untranslated field to make space for translation struct $object->{$setter}(null); } } } } catch (\Doctrine\ODM\MongoDB\DocumentNotFoundException $e) { // @todo if a document references a non-existing document - handle it so it renders to null! } }
/** * {@InheritDoc} */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); $type = $event->getType(); // If the set type name is not an actual class, but a faked type for which a custom handler exists, we do not // modify it with this subscriber. // Also, we forgo autoloading here as an instance of this type is already created, // so it must be loaded if its a real class. $virtualType = !class_exists($type['name'], false); if ($object instanceof PersistentCollection || $object instanceof MongoDBPersistentCollection || $object instanceof PHPCRPersistentCollection) { if (!$virtualType) { $event->setType('ArrayCollection'); } return; } if (!$object instanceof Proxy && !$object instanceof ORMProxy) { return; } if ($this->enableLazyLoading) { $object->__load(); } if (!$virtualType) { if ($object->__isInitialized()) { $event->setType(get_parent_class($object)); } else { $event->setType(SerializerProxyType::class, array('id' => $object->getId())); } } }
/** * 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] * * @return mixed the return value depends on the direction, and type of visitor */ public function accept($data, array $type = null, Context $context) { $visitor = $context->getVisitor(); // If the type was not given, we infer the most specific type from the // input data in serialization mode. if (null === $type) { if ($context instanceof DeserializationContext) { throw new RuntimeException('The type must be given for all properties when deserializing.'); } $typeName = gettype($data); if ('object' === $typeName) { $typeName = get_class($data); } $type = array('name' => $typeName, 'params' => array()); } else { if ($context instanceof SerializationContext && null === $data) { $type = array('name' => 'NULL', 'params' => array()); } } switch ($type['name']) { case 'NULL': return $visitor->visitNull($data, $type, $context); case 'string': return $visitor->visitString($data, $type, $context); case 'int': case 'integer': return $visitor->visitInteger($data, $type, $context); case 'boolean': return $visitor->visitBoolean($data, $type, $context); case 'double': case 'float': return $visitor->visitDouble($data, $type, $context); case 'array': return $visitor->visitArray($data, $type, $context); case 'resource': $msg = 'Resources are not supported in serialized data.'; if ($context instanceof SerializationContext && null !== ($path = $context->getPath())) { $msg .= ' Path: ' . $path; } throw new RuntimeException($msg); default: // TODO: The rest of this method needs some refactoring. if ($context instanceof SerializationContext) { if (null !== $data) { if ($context->isVisiting($data)) { return null; } $context->startVisiting($data); } // If we're serializing a polymorphic type, then we'll be interested in the // metadata for the actual type of the object, not the base class. if (class_exists($type['name'], false) || interface_exists($type['name'], false)) { if (is_subclass_of($data, $type['name'], false)) { $type = array('name' => get_class($data), 'params' => array()); } } } elseif ($context instanceof DeserializationContext) { $context->increaseDepth(); } // Trigger pre-serialization callbacks, and listeners if they exist. // Dispatch pre-serialization event before handling data to have ability change type in listener if ($context instanceof SerializationContext) { if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $context->getFormat())) { $this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $context->getFormat(), $event = new PreSerializeEvent($context, $data, $type)); $type = $event->getType(); } } elseif ($context instanceof DeserializationContext) { if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_deserialize', $type['name'], $context->getFormat())) { $this->dispatcher->dispatch('serializer.pre_deserialize', $type['name'], $context->getFormat(), $event = new PreDeserializeEvent($context, $data, $type)); $type = $event->getType(); $data = $event->getData(); } } // 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($context->getDirection(), $type['name'], $context->getFormat()))) { $rs = call_user_func($handler, $visitor, $data, $type, $context); $this->leaveScope($context, $data); return $rs; } $exclusionStrategy = $context->getExclusionStrategy(); /** @var $metadata ClassMetadata */ $metadata = $this->metadataFactory->getMetadataForClass($type['name']); if ($context instanceof DeserializationContext && !empty($metadata->discriminatorMap) && $type['name'] === $metadata->discriminatorBaseClass) { $metadata = $this->resolveMetadata($data, $metadata); } if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) { $this->leaveScope($context, $data); return null; } $context->pushClassMetadata($metadata); if ($context instanceof SerializationContext) { foreach ($metadata->preSerializeMethods as $method) { $method->invoke($data); } } $object = $data; if ($context instanceof DeserializationContext) { $object = $this->objectConstructor->construct($visitor, $metadata, $data, $type, $context); } if (isset($metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()])) { $rs = $object->{$metadata->handlerCallbacks[$context->getDirection()][$context->getFormat()]}($visitor, $context instanceof SerializationContext ? null : $data, $context); $this->afterVisitingObject($metadata, $object, $type, $context); return $context instanceof SerializationContext ? $rs : $object; } $visitor->startVisitingObject($metadata, $object, $type, $context); foreach ($metadata->propertyMetadata as $propertyMetadata) { if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipProperty($propertyMetadata, $context)) { continue; } if ($context instanceof DeserializationContext && $propertyMetadata->readOnly) { continue; } $context->pushPropertyMetadata($propertyMetadata); $visitor->visitProperty($propertyMetadata, $data, $context); $context->popPropertyMetadata(); } if ($context instanceof SerializationContext) { $this->afterVisitingObject($metadata, $data, $type, $context); return $visitor->endVisitingObject($metadata, $data, $type, $context); } $rs = $visitor->endVisitingObject($metadata, $data, $type, $context); $this->afterVisitingObject($metadata, $rs, $type, $context); return $rs; } }
public function onPreSerialize(PreSerializeEvent $event) { $lyric = $event->getObject(); /* @lyric \Tekstove\ApiBundle\Model\Lyric */ $this->clearForbiddenLyricsData($lyric); }
/** * 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; } }
/** * {@inheritDoc} */ public function onPreSerialize(PreSerializeEvent $event) { $object = $event->getObject(); if (method_exists($object, 'getReferences')) { $object->getReferences($this->user, $event->getContext()->attributes->get('groups')->get('value')); } }