/** * @dataProvider providerPublicMethodException */ public function testAccessorTypePublicMethodException($getter, $setter, $message) { $this->setExpectedException('\\JMS\\Serializer\\Exception\\RuntimeException', $message); $object = new PropertyMetadataPublicMethod(); $metadata = new PropertyMetadata(get_class($object), 'e'); $metadata->setAccessor(PropertyMetadata::ACCESS_TYPE_PUBLIC_METHOD, $getter, $setter); }
public function visitProperty(PropertyMetadata $propertyMetadata, $data, Context $context) { $v = $propertyMetadata->getValue($data); $isSaveChanges = $context instanceof SaveChangesContextInterface; if ($this->isProxyObject($v) && ($isSaveChanges || !$v->__isInitialized())) { return; } if (!$propertyMetadata->reflection) { return; } return parent::visitProperty($propertyMetadata, $data, $context); }
public function shouldSkipProperty(PropertyMetadata $property, Context $context) { if ($context instanceof DeserializationContext) { return false; } $data = $context->getObject(); if ($data instanceof \Staffim\DTOBundle\Hateoas\CollectionRepresentation) { return false; } if ($property->class == 'Hateoas\\Configuration\\Relation') { return false; } return is_object($data) && $property->getValue($data) === UnknownValue::create(); }
protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata) { $propertyName = $propertyMetadata->name; if ($doctrineMetadata->hasField($propertyName) && ($fieldType = $this->normalizeFieldType($doctrineMetadata->getTypeOfField($propertyName)))) { $propertyMetadata->setType($fieldType); } elseif ($doctrineMetadata->hasAssociation($propertyName)) { $targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName); if (null === ($targetMetadata = $this->tryLoadingDoctrineMetadata($targetEntity))) { return; } // For inheritance schemes, we cannot add any type as we would only add the super-type of the hierarchy. // On serialization, this would lead to only the supertype being serialized, and properties of subtypes // being ignored. if ($targetMetadata instanceof DoctrineClassMetadata && !$targetMetadata->isInheritanceTypeNone()) { return; } if (!$doctrineMetadata->isSingleValuedAssociation($propertyName)) { $targetEntity = "ArrayCollection<{$targetEntity}>"; } $propertyMetadata->setType($targetEntity); } }
protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata) { $propertyName = $propertyMetadata->name; if ($doctrineMetadata->hasField($propertyName) && ($fieldType = $this->normalizeFieldType($doctrineMetadata->getTypeOfField($propertyName)))) { $field = $doctrineMetadata->getField($propertyName); if (!empty($field['multivalue'])) { $fieldType = 'array'; } $propertyMetadata->setType($fieldType); } elseif ($doctrineMetadata->hasAssociation($propertyName)) { try { $targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName); } catch (\Exception $e) { return; } if (null === $this->tryLoadingDoctrineMetadata($targetEntity)) { return; } if (!$doctrineMetadata->isSingleValuedAssociation($propertyName)) { $targetEntity = "ArrayCollection<{$targetEntity}>"; } $propertyMetadata->setType($targetEntity); } }
public function visitProperty(PropertyMetadata $metadata, $object, Context $context) { $v = $metadata->getValue($object); if (null === $v && !$context->shouldSerializeNull()) { return; } if ($metadata->xmlAttribute) { $this->setCurrentMetadata($metadata); $node = $this->navigator->accept($v, $metadata->type, $context); $this->revertCurrentMetadata(); if (!$node instanceof \DOMCharacterData) { throw new RuntimeException(sprintf('Unsupported value for XML attribute for %s. Expected character data, but got %s.', $metadata->name, json_encode($v))); } $attributeName = $this->namingStrategy->translateName($metadata); $this->setAttributeOnNode($this->currentNode, $attributeName, $node->nodeValue, $metadata->xmlNamespace); return; } if ($metadata->xmlValue && $this->currentNode->childNodes->length > 0 || !$metadata->xmlValue && $this->hasValue) { throw new RuntimeException(sprintf('If you make use of @XmlValue, all other properties in the class must have the @XmlAttribute annotation. Invalid usage detected in class %s.', $metadata->class)); } if ($metadata->xmlValue) { $this->hasValue = true; $this->setCurrentMetadata($metadata); $node = $this->navigator->accept($v, $metadata->type, $context); $this->revertCurrentMetadata(); if ($node !== null) { if (!$node instanceof \DOMCharacterData) { throw new RuntimeException(sprintf('Unsupported value for property %s::$%s. Expected character data, but got %s.', $metadata->reflection->class, $metadata->reflection->name, is_object($node) ? get_class($node) : gettype($node))); } $this->currentNode->appendChild($node); } return; } if ($metadata->xmlAttributeMap) { if (!is_array($v)) { throw new RuntimeException(sprintf('Unsupported value type for XML attribute map. Expected array but got %s.', gettype($v))); } foreach ($v as $key => $value) { $this->setCurrentMetadata($metadata); $node = $this->navigator->accept($value, null, $context); $this->revertCurrentMetadata(); if (!$node instanceof \DOMCharacterData) { throw new RuntimeException(sprintf('Unsupported value for a XML attribute map value. Expected character data, but got %s.', json_encode($v))); } $this->setAttributeOnNode($this->currentNode, $key, $node->nodeValue, $metadata->xmlNamespace); } return; } if ($addEnclosingElement = (!$metadata->xmlCollection || !$metadata->xmlCollectionInline) && !$metadata->inline) { $elementName = $this->namingStrategy->translateName($metadata); if (null !== $metadata->xmlNamespace) { $element = $this->createElement($elementName, $metadata->xmlNamespace); } else { $defaultNamespace = $this->getClassDefaultNamespace($this->objectMetadataStack->top()); $element = $this->createElement($elementName, $defaultNamespace); } $this->setCurrentNode($element); } $this->setCurrentMetadata($metadata); if (null !== ($node = $this->navigator->accept($v, $metadata->type, $context))) { $this->currentNode->appendChild($node); } $this->revertCurrentMetadata(); if ($addEnclosingElement) { $this->revertCurrentNode(); if ($element->hasChildNodes() || $element->hasAttributes() || $node === null && $v !== null) { $this->currentNode->appendChild($element); } } $this->hasValue = false; }
/** * Assigns the data type for a property. * * @param PropertyMetadata $propertyMetadata * @param string $type * @throws Exception\UnsupportedType */ protected function setTypeMetadata(PropertyMetadata $propertyMetadata, $type) { $propertyMetadata->setType($type); }
public function visitProperty(PropertyMetadata $metadata, $data, Context $context) { $v = $metadata->getValue($data); $v = $this->navigator->accept($v, $metadata->type, $context); if (null === $v && !$context->shouldSerializeNull()) { return; } $k = $this->namingStrategy->translateName($metadata); if ($metadata->inline && is_array($v)) { $this->data = array_merge($this->data, $v); } else { $this->data[$k] = $v; } }
public function visitProperty(PropertyMetadata $metadata, $data, Context $context) { $v = $metadata->getValue($data); if (null === $v && !$context->shouldSerializeNull()) { return; } $name = $this->namingStrategy->translateName($metadata); if (!$metadata->inline) { $this->writer->writeln(Inline::dump($name) . ':')->indent(); } $this->setCurrentMetadata($metadata); $count = $this->writer->changeCount; if (null !== ($v = $this->navigator->accept($v, $metadata->type, $context))) { $this->writer->rtrim(false)->writeln(' ' . $v); } elseif ($count === $this->writer->changeCount && !$metadata->inline) { $this->writer->revert(); } if (!$metadata->inline) { $this->writer->outdent(); } $this->revertCurrentMetadata(); }
$pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost', 'id'); $pMetadata->setType('string'); $pMetadata->groups = array('comments', 'post'); $pMetadata->xmlElementCData = false; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost', 'title'); $pMetadata->setType('string'); $pMetadata->groups = array('comments', 'post'); $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost', 'createdAt'); $pMetadata->setType('DateTime'); $pMetadata->xmlAttribute = true; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost', 'published'); $pMetadata->setType('boolean'); $pMetadata->serializedName = 'is_published'; $pMetadata->groups = array('post'); $pMetadata->xmlAttribute = true; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost', 'comments'); $pMetadata->setType('ArrayCollection<JMS\\Serializer\\Tests\\Fixtures\\Comment>'); $pMetadata->xmlCollection = true; $pMetadata->xmlCollectionInline = true; $pMetadata->xmlEntryName = 'comment'; $pMetadata->groups = array('comments'); $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost', 'author'); $pMetadata->setType('JMS\\Serializer\\Tests\\Fixtures\\Author'); $pMetadata->groups = array('post'); $metadata->addPropertyMetadata($pMetadata); return $metadata;
use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; $metadata = new ClassMetadata('JMS\\Serializer\\Tests\\Fixtures\\ObjectWithXmlNamespaces'); $metadata->xmlRootName = 'test-object'; $metadata->xmlRootNamespace = 'http://example.com/namespace'; $metadata->registerNamespace('http://example.com/namespace'); $metadata->registerNamespace('http://schemas.google.com/g/2005', 'gd'); $metadata->registerNamespace('http://www.w3.org/2005/Atom', 'atom'); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\ObjectWithXmlNamespaces', 'title'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://purl.org/dc/elements/1.1/"; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\ObjectWithXmlNamespaces', 'createdAt'); $pMetadata->setType('DateTime'); $pMetadata->xmlAttribute = true; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\ObjectWithXmlNamespaces', 'etag'); $pMetadata->setType('string'); $pMetadata->xmlAttribute = true; $pMetadata->xmlNamespace = 'http://schemas.google.com/g/2005'; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\ObjectWithXmlNamespaces', 'author'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = 'http://www.w3.org/2005/Atom'; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\ObjectWithXmlNamespaces', 'language'); $pMetadata->setType('string'); $pMetadata->xmlAttribute = true; $pMetadata->xmlNamespace = 'http://purl.org/dc/elements/1.1/'; $metadata->addPropertyMetadata($pMetadata); return $metadata;
<?php use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; $metadata = new ClassMetadata('JMS\\Serializer\\Tests\\Fixtures\\Person'); $metadata->xmlRootName = 'child'; $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\Person', 'name'); $pMetadata->setType('string'); $pMetadata->xmlValue = true; $pMetadata->xmlElementCData = false; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\Person', 'age'); $pMetadata->setType('integer'); $pMetadata->xmlAttribute = true; $metadata->addPropertyMetadata($pMetadata); return $metadata;
<?php use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; $metadata = new ClassMetadata('JMS\\Serializer\\Tests\\Fixtures\\Price'); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\Price', 'price'); $pMetadata->setType('double'); $pMetadata->xmlValue = true; $metadata->addPropertyMetadata($pMetadata); return $metadata;