コード例 #1
0
 /**
  * @dataProvider providerPublicMethodData
  */
 public function testAccessorTypePublicMethod($property, $getterInit, $setterInit, $getterName, $setterName)
 {
     $object = new PropertyMetadataPublicMethod();
     $metadata = new PropertyMetadata(get_class($object), $property);
     $metadata->setAccessor(PropertyMetadata::ACCESS_TYPE_PUBLIC_METHOD, $getterInit, $setterInit);
     $this->assertEquals($getterName, $metadata->getter);
     $this->assertEquals($setterName, $metadata->setter);
     $metadata->setValue($object, 'x');
     $this->assertEquals(sprintf('%1$s:%1$s:x', strtoupper($property)), $metadata->getValue($object));
 }
コード例 #2
0
 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);
 }
コード例 #3
0
 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();
 }
コード例 #4
0
 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;
 }
コード例 #5
0
 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;
     }
 }
コード例 #6
0
 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();
 }