Example #1
0
<?php

use JMS\SerializerBundle\Metadata\ClassMetadata;
use JMS\SerializerBundle\Metadata\PropertyMetadata;
$metadata = new ClassMetadata('JMS\\SerializerBundle\\Tests\\Fixtures\\Price');
$pMetadata = new PropertyMetadata('JMS\\SerializerBundle\\Tests\\Fixtures\\Price', 'price');
$pMetadata->setType('double');
$pMetadata->xmlValue = true;
$metadata->addPropertyMetadata($pMetadata);
return $metadata;
 protected function loadMetadataFromFile(\ReflectionClass $class, $path)
 {
     $previous = libxml_use_internal_errors(true);
     $elem = simplexml_load_file($path);
     libxml_use_internal_errors($previous);
     if (false === $elem) {
         throw new XmlErrorException(libxml_get_last_error());
     }
     $metadata = new ClassMetadata($name = $class->getName());
     if (!($elems = $elem->xpath("./class[@name = '" . $name . "']"))) {
         throw new RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
     }
     $elem = reset($elems);
     $metadata->fileResources[] = $path;
     $metadata->fileResources[] = $class->getFileName();
     $exclusionPolicy = strtoupper($elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
     $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower($exclude) : false;
     $classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
     if (null !== ($accessorOrder = $elem->attributes()->{'accessor-order'})) {
         $metadata->setAccessorOrder((string) $accessorOrder, preg_split('/\\s*,\\s*/', (string) $elem->attributes()->{'custom-accessor-order'}));
     }
     if (null !== ($xmlRootName = $elem->attributes()->{'xml-root-name'})) {
         $metadata->xmlRootName = (string) $xmlRootName;
     }
     if (!$excludeAll) {
         foreach ($class->getProperties() as $property) {
             if ($name !== $property->getDeclaringClass()->getName()) {
                 continue;
             }
             $pMetadata = new PropertyMetadata($name, $pName = $property->getName());
             $isExclude = $isExpose = false;
             $pElems = $elem->xpath("./property[@name = '" . $pName . "']");
             if ($pElems) {
                 $pElem = reset($pElems);
                 if (null !== ($exclude = $pElem->attributes()->exclude)) {
                     $isExclude = 'true' === strtolower($exclude);
                 }
                 if (null !== ($expose = $pElem->attributes()->expose)) {
                     $isExpose = 'true' === strtolower($expose);
                 }
                 if (null !== ($version = $pElem->attributes()->{'since-version'})) {
                     $pMetadata->sinceVersion = (string) $version;
                 }
                 if (null !== ($version = $pElem->attributes()->{'until-version'})) {
                     $pMetadata->untilVersion = (string) $version;
                 }
                 if (null !== ($serializedName = $pElem->attributes()->{'serialized-name'})) {
                     $pMetadata->serializedName = (string) $serializedName;
                 }
                 if (null !== ($type = $pElem->attributes()->type)) {
                     $pMetadata->type = (string) $type;
                 } else {
                     if (isset($pElem->type)) {
                         $pMetadata->type = (string) $pElem->type;
                     }
                 }
                 if (isset($pElem->{'xml-list'})) {
                     $pMetadata->xmlCollection = true;
                     $colConfig = $pElem->{'xml-list'};
                     if (isset($colConfig->attributes()->inline)) {
                         $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
                     }
                     if (isset($colConfig->attributes()->{'entry-name'})) {
                         $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
                     }
                 }
                 if (isset($pElem->{'xml-map'})) {
                     $pMetadata->xmlCollection = true;
                     $colConfig = $pElem->{'xml-map'};
                     if (isset($colConfig->attributes()->inline)) {
                         $pMetadata->xmlCollectionInline = 'true' === (string) $colConfig->attributes()->inline;
                     }
                     if (isset($colConfig->attributes()->{'entry-name'})) {
                         $pMetadata->xmlEntryName = (string) $colConfig->attributes()->{'entry-name'};
                     }
                     if (isset($colConfig->attributes()->{'key-attribute-name'})) {
                         $pMetadata->xmlKeyAttribute = (string) $colConfig->attributes()->{'key-attribute-name'};
                     }
                 }
                 if (isset($pElem->attributes()->{'xml-attribute'})) {
                     $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'};
                 }
                 if (isset($pElem->attributes()->{'xml-value'})) {
                     $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'};
                 }
                 $getter = $pElem->attributes()->{'accessor-getter'};
                 $setter = $pElem->attributes()->{'accessor-setter'};
                 $pMetadata->setAccessor((string) ($pElem->attributes()->{'access-type'} ?: $classAccessType), $getter ? (string) $getter : null, $setter ? (string) $setter : null);
             }
             if (ExclusionPolicy::NONE === (string) $exclusionPolicy && !$isExclude || ExclusionPolicy::ALL === (string) $exclusionPolicy && $isExpose) {
                 $metadata->addPropertyMetadata($pMetadata);
             }
         }
     }
     foreach ($elem->xpath('./callback-method') as $method) {
         if (!isset($method->attributes()->type)) {
             throw new RuntimeException('The type attribute must be set for all callback-method elements.');
         }
         if (!isset($method->attributes()->name)) {
             throw new RuntimeException('The name attribute must be set for all callback-method elements.');
         }
         switch ((string) $method->attributes()->type) {
             case 'pre-serialize':
                 $metadata->addPreSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
                 break;
             case 'post-serialize':
                 $metadata->addPostSerializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
                 break;
             case 'post-deserialize':
                 $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string) $method->attributes()->name));
                 break;
             default:
                 throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
         }
     }
     return $metadata;
 }
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $classMetadata = new ClassMetadata($name = $class->getName());
     $classMetadata->fileResources[] = $class->getFilename();
     $exclusionPolicy = 'NONE';
     $excludeAll = false;
     $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY;
     foreach ($this->reader->getClassAnnotations($class) as $annot) {
         if ($annot instanceof ExclusionPolicy) {
             $exclusionPolicy = $annot->policy;
         } else {
             if ($annot instanceof XmlRoot) {
                 $classMetadata->xmlRootName = $annot->name;
             } else {
                 if ($annot instanceof Exclude) {
                     $excludeAll = true;
                 } else {
                     if ($annot instanceof AccessType) {
                         $classAccessType = $annot->type;
                     } else {
                         if ($annot instanceof AccessorOrder) {
                             $classMetadata->setAccessorOrder($annot->order, $annot->custom);
                         }
                     }
                 }
             }
         }
     }
     if (!$excludeAll) {
         foreach ($class->getProperties() as $property) {
             if ($property->getDeclaringClass()->getName() !== $name) {
                 continue;
             }
             $propertyMetadata = new PropertyMetadata($name, $property->getName());
             $isExclude = $isExpose = false;
             $AccessType = $classAccessType;
             $accessor = array(null, null);
             foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
                 if ($annot instanceof Since) {
                     $propertyMetadata->sinceVersion = $annot->version;
                 } else {
                     if ($annot instanceof Until) {
                         $propertyMetadata->untilVersion = $annot->version;
                     } else {
                         if ($annot instanceof SerializedName) {
                             $propertyMetadata->serializedName = $annot->name;
                         } else {
                             if ($annot instanceof Expose) {
                                 $isExpose = true;
                             } else {
                                 if ($annot instanceof Exclude) {
                                     $isExclude = true;
                                 } else {
                                     if ($annot instanceof Type) {
                                         $propertyMetadata->type = $annot->name;
                                     } else {
                                         if ($annot instanceof XmlList) {
                                             $propertyMetadata->xmlCollection = true;
                                             $propertyMetadata->xmlCollectionInline = $annot->inline;
                                             $propertyMetadata->xmlEntryName = $annot->entry;
                                         } else {
                                             if ($annot instanceof XmlMap) {
                                                 $propertyMetadata->xmlCollection = true;
                                                 $propertyMetadata->xmlCollectionInline = $annot->inline;
                                                 $propertyMetadata->xmlEntryName = $annot->entry;
                                                 $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute;
                                             } else {
                                                 if ($annot instanceof XmlAttribute) {
                                                     $propertyMetadata->xmlAttribute = true;
                                                 } else {
                                                     if ($annot instanceof XmlValue) {
                                                         $propertyMetadata->xmlValue = true;
                                                     } else {
                                                         if ($annot instanceof AccessType) {
                                                             $AccessType = $annot->type;
                                                         } else {
                                                             if ($annot instanceof Accessor) {
                                                                 $accessor = array($annot->getter, $annot->setter);
                                                             } else {
                                                                 if ($annot instanceof Groups) {
                                                                     $propertyMetadata->setGroups($annot->names);
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $propertyMetadata->setAccessor($AccessType, $accessor[0], $accessor[1]);
             if (ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude || ExclusionPolicy::ALL === $exclusionPolicy && $isExpose) {
                 $classMetadata->addPropertyMetadata($propertyMetadata);
             }
         }
     }
     foreach ($class->getMethods() as $method) {
         if ($method->getDeclaringClass()->getName() !== $name) {
             continue;
         }
         foreach ($this->reader->getMethodAnnotations($method) as $annot) {
             if ($annot instanceof PreSerialize) {
                 $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName()));
                 continue 2;
             } else {
                 if ($annot instanceof PostDeserialize) {
                     $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName()));
                     continue 2;
                 } else {
                     if ($annot instanceof PostSerialize) {
                         $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName()));
                         continue 2;
                     }
                 }
             }
         }
     }
     return $classMetadata;
 }
Example #4
0
 protected function loadMetadataFromFile(\ReflectionClass $class, $file)
 {
     $config = Yaml::parse(file_get_contents($file));
     if (!isset($config[$name = $class->getName()])) {
         throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $class->getName(), $file));
     }
     $config = $config[$name];
     $metadata = new ClassMetadata($name);
     $metadata->fileResources[] = $file;
     $metadata->fileResources[] = $class->getFileName();
     $exclusionPolicy = isset($config['exclusion_policy']) ? strtoupper($config['exclusion_policy']) : 'NONE';
     $excludeAll = isset($config['exclude']) ? (bool) $config['exclude'] : false;
     $classAccessType = isset($config['access_type']) ? $config['access_type'] : PropertyMetadata::ACCESS_TYPE_PROPERTY;
     if (isset($config['accessor_order'])) {
         $metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
     }
     if (isset($config['xml_root_name'])) {
         $metadata->xmlRootName = (string) $config['xml_root_name'];
     }
     if (!$excludeAll) {
         foreach ($class->getProperties() as $property) {
             if ($name !== $property->getDeclaringClass()->getName()) {
                 continue;
             }
             $pMetadata = new PropertyMetadata($name, $pName = $property->getName());
             $isExclude = $isExpose = false;
             if (isset($config['properties'][$pName])) {
                 $pConfig = $config['properties'][$pName];
                 if (isset($pConfig['exclude'])) {
                     $isExclude = (bool) $pConfig['exclude'];
                 }
                 if (isset($pConfig['expose'])) {
                     $isExpose = (bool) $pConfig['expose'];
                 }
                 if (isset($pConfig['since_version'])) {
                     $pMetadata->sinceVersion = (string) $pConfig['since_version'];
                 }
                 if (isset($pConfig['until_version'])) {
                     $pMetadata->untilVersion = (string) $pConfig['until_version'];
                 }
                 if (isset($pConfig['serialized_name'])) {
                     $pMetadata->serializedName = (string) $pConfig['serialized_name'];
                 }
                 if (isset($pConfig['type'])) {
                     $pMetadata->type = (string) $pConfig['type'];
                 }
                 if (isset($pConfig['xml_list'])) {
                     $pMetadata->xmlCollection = true;
                     $colConfig = $pConfig['xml_list'];
                     if (isset($colConfig['inline'])) {
                         $pMetadata->xmlCollectionInline = (bool) $colConfig['inline'];
                     }
                     if (isset($colConfig['entry_name'])) {
                         $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
                     }
                 }
                 if (isset($pConfig['xml_map'])) {
                     $pMetadata->xmlCollection = true;
                     $colConfig = $pConfig['xml_map'];
                     if (isset($colConfig['inline'])) {
                         $pMetadata->xmlCollectionInline = (bool) $colConfig['inline'];
                     }
                     if (isset($colConfig['entry_name'])) {
                         $pMetadata->xmlEntryName = (string) $colConfig['entry_name'];
                     }
                     if (isset($colConfig['key_attribute_name'])) {
                         $pMetadata->xmlKeyAttribute = $colConfig['key_attribute_name'];
                     }
                 }
                 if (isset($pConfig['xml_attribute'])) {
                     $pMetadata->xmlAttribute = (bool) $pConfig['xml_attribute'];
                 }
                 if (isset($pConfig['xml_value'])) {
                     $pMetadata->xmlValue = (bool) $pConfig['xml_value'];
                 }
                 $pMetadata->setAccessor(isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType, isset($pConfig['accessor']) ? $pConfig['accessor'] : null);
                 if (isset($pConfig['inline'])) {
                     $pMetadata->inline = (bool) $pConfig['inline'];
                 }
                 if (isset($pConfig['read_only'])) {
                     $pMetadata->readOnly = (bool) $pConfig['read_only'];
                 }
             }
             if (ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude || ExclusionPolicy::ALL === $exclusionPolicy && $isExpose) {
                 $metadata->addPropertyMetadata($pMetadata);
             }
         }
     }
     if (isset($config['callback_methods'])) {
         $cConfig = $config['callback_methods'];
         if (isset($cConfig['pre_serialize'])) {
             $metadata->preSerializeMethods = $this->getCallbackMetadata($class, $cConfig['pre_serialize']);
         }
         if (isset($cConfig['post_serialize'])) {
             $metadata->postSerializeMethods = $this->getCallbackMetadata($class, $cConfig['post_serialize']);
         }
         if (isset($cConfig['post_deserialize'])) {
             $metadata->postDeserializeMethods = $this->getCallbackMetadata($class, $cConfig['post_deserialize']);
         }
     }
     return $metadata;
 }
Example #5
0
use JMS\SerializerBundle\Metadata\ClassMetadata;
use JMS\SerializerBundle\Metadata\PropertyMetadata;
$metadata = new ClassMetadata('JMS\\SerializerBundle\\Tests\\Fixtures\\BlogPost');
$metadata->xmlRootName = 'blog-post';
$pMetadata = new PropertyMetadata('JMS\\SerializerBundle\\Tests\\Fixtures\\BlogPost', 'title');
$pMetadata->setType('string');
$pMetadata->groups = array('comments', 'post');
$metadata->addPropertyMetadata($pMetadata);
$pMetadata = new PropertyMetadata('JMS\\SerializerBundle\\Tests\\Fixtures\\BlogPost', 'createdAt');
$pMetadata->setType('DateTime');
$pMetadata->xmlAttribute = true;
$metadata->addPropertyMetadata($pMetadata);
$pMetadata = new PropertyMetadata('JMS\\SerializerBundle\\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\\SerializerBundle\\Tests\\Fixtures\\BlogPost', 'comments');
$pMetadata->setType('ArrayCollection<JMS\\SerializerBundle\\Tests\\Fixtures\\Comment>');
$pMetadata->xmlCollection = true;
$pMetadata->xmlCollectionInline = true;
$pMetadata->xmlEntryName = 'comment';
$pMetadata->groups = array('comments');
$metadata->addPropertyMetadata($pMetadata);
$pMetadata = new PropertyMetadata('JMS\\SerializerBundle\\Tests\\Fixtures\\BlogPost', 'author');
$pMetadata->setType('JMS\\SerializerBundle\\Tests\\Fixtures\\Author');
$pMetadata->groups = array('post');
$metadata->addPropertyMetadata($pMetadata);
return $metadata;