public function loadMetadataForClass(\ReflectionClass $class) { $classMetadata = new ClassMetadata($name = $class->name); $classMetadata->fileResources[] = $class->getFilename(); $propertiesMetadata = array(); $propertiesAnnotations = array(); $exclusionPolicy = 'NONE'; $excludeAll = false; $classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY; $readOnlyClass = false; foreach ($this->reader->getClassAnnotations($class) as $annot) { if ($annot instanceof ExclusionPolicy) { $exclusionPolicy = $annot->policy; } elseif ($annot instanceof XmlRoot) { $classMetadata->xmlRootName = $annot->name; $classMetadata->xmlRootNamespace = $annot->namespace; } elseif ($annot instanceof XmlNamespace) { $classMetadata->registerNamespace($annot->uri, $annot->prefix); } elseif ($annot instanceof Exclude) { $excludeAll = true; } elseif ($annot instanceof AccessType) { $classAccessType = $annot->type; } elseif ($annot instanceof ReadOnly) { $readOnlyClass = true; } elseif ($annot instanceof AccessorOrder) { $classMetadata->setAccessorOrder($annot->order, $annot->custom); } elseif ($annot instanceof Discriminator) { if ($annot->disabled) { $classMetadata->discriminatorDisabled = true; } else { $classMetadata->setDiscriminator($annot->field, $annot->map); } } } foreach ($class->getMethods() as $method) { if ($method->class !== $name) { continue; } $methodAnnotations = $this->reader->getMethodAnnotations($method); foreach ($methodAnnotations as $annot) { if ($annot instanceof PreSerialize) { $classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name)); continue 2; } elseif ($annot instanceof PostDeserialize) { $classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->name)); continue 2; } elseif ($annot instanceof PostSerialize) { $classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->name)); continue 2; } elseif ($annot instanceof VirtualProperty) { $virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->name); $propertiesMetadata[] = $virtualPropertyMetadata; $propertiesAnnotations[] = $methodAnnotations; continue 2; } elseif ($annot instanceof HandlerCallback) { $classMetadata->addHandlerCallback(GraphNavigator::parseDirection($annot->direction), $annot->format, $method->name); continue 2; } } } if (!$excludeAll) { foreach ($class->getProperties() as $property) { if ($property->class !== $name) { continue; } $propertiesMetadata[] = new PropertyMetadata($name, $property->getName()); $propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property); } foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) { $isExclude = false; $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata; $propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass; $accessType = $classAccessType; $accessor = array(null, null); $propertyAnnotations = $propertiesAnnotations[$propertyKey]; foreach ($propertyAnnotations as $annot) { if ($annot instanceof Since) { $propertyMetadata->sinceVersion = $annot->version; } elseif ($annot instanceof Until) { $propertyMetadata->untilVersion = $annot->version; } elseif ($annot instanceof SerializedName) { $propertyMetadata->serializedName = $annot->name; } elseif ($annot instanceof Expose) { $isExpose = true; } elseif ($annot instanceof Exclude) { $isExclude = true; } elseif ($annot instanceof Type) { $propertyMetadata->setType($annot->name); } elseif ($annot instanceof XmlElement) { $propertyMetadata->xmlAttribute = false; $propertyMetadata->xmlElementCData = $annot->cdata; $propertyMetadata->xmlNamespace = $annot->namespace; } elseif ($annot instanceof XmlList) { $propertyMetadata->xmlCollection = true; $propertyMetadata->xmlCollectionInline = $annot->inline; $propertyMetadata->xmlEntryName = $annot->entry; } elseif ($annot instanceof XmlMap) { $propertyMetadata->xmlCollection = true; $propertyMetadata->xmlCollectionInline = $annot->inline; $propertyMetadata->xmlEntryName = $annot->entry; $propertyMetadata->xmlKeyAttribute = $annot->keyAttribute; } elseif ($annot instanceof XmlCollectionEntry) { $propertyMetadata->xmlCollectionEntry = $annot->callback; } elseif ($annot instanceof XmlKeyValuePairs) { $propertyMetadata->xmlKeyValuePairs = true; } elseif ($annot instanceof XmlAttribute) { $propertyMetadata->xmlAttribute = true; $propertyMetadata->xmlNamespace = $annot->namespace; } elseif ($annot instanceof XmlValue) { $propertyMetadata->xmlValue = true; $propertyMetadata->xmlElementCData = $annot->cdata; } elseif ($annot instanceof XmlElement) { $propertyMetadata->xmlElementCData = $annot->cdata; } elseif ($annot instanceof AccessType) { $accessType = $annot->type; } elseif ($annot instanceof ReadOnly) { $propertyMetadata->readOnly = $annot->readOnly; } elseif ($annot instanceof Accessor) { $accessor = array($annot->getter, $annot->setter); } elseif ($annot instanceof Groups) { $propertyMetadata->groups = $annot->groups; foreach ((array) $propertyMetadata->groups as $groupName) { if (false !== strpos($groupName, ',')) { throw new InvalidArgumentException(sprintf('Invalid group name "%s" on "%s", did you mean to create multiple groups?', implode(', ', $propertyMetadata->groups), $propertyMetadata->class . '->' . $propertyMetadata->name)); } } } elseif ($annot instanceof Inline) { $propertyMetadata->inline = true; } elseif ($annot instanceof XmlAttributeMap) { $propertyMetadata->xmlAttributeMap = true; } elseif ($annot instanceof MaxDepth) { $propertyMetadata->maxDepth = $annot->depth; } } $propertyMetadata->setAccessor($accessType, $accessor[0], $accessor[1]); if (ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude || ExclusionPolicy::ALL === $exclusionPolicy && $isExpose) { $classMetadata->addPropertyMetadata($propertyMetadata); } } } return $classMetadata; }
private function addClassProperties(ClassMetadata $metadata, array $config) { if (isset($config['custom_accessor_order']) && !isset($config['accessor_order'])) { $config['accessor_order'] = 'custom'; } 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 (isset($config['xml_root_namespace'])) { $metadata->xmlRootNamespace = (string) $config['xml_root_namespace']; } if (array_key_exists('xml_namespaces', $config)) { foreach ($config['xml_namespaces'] as $prefix => $uri) { $metadata->registerNamespace($uri, $prefix); } } if (isset($config['discriminator'])) { if (isset($config['discriminator']['disabled']) && true === $config['discriminator']['disabled']) { $metadata->discriminatorDisabled = true; } else { if (!isset($config['discriminator']['field_name'])) { throw new RuntimeException('The "field_name" attribute must be set for discriminators.'); } if (!isset($config['discriminator']['map']) || !is_array($config['discriminator']['map'])) { throw new RuntimeException('The "map" attribute must be set, and be an array for discriminators.'); } $metadata->setDiscriminator($config['discriminator']['field_name'], $config['discriminator']['map']); } } }
<?php 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/';
<?php use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; $metadata = new ClassMetadata('JMS\\Serializer\\Tests\\Fixtures\\BlogPost'); $metadata->xmlRootName = 'blog-post'; $metadata->registerNamespace('http://example.com/namespace'); $metadata->registerNamespace('http://schemas.google.com/g/2005', 'gd'); $metadata->registerNamespace('http://www.w3.org/2005/Atom', 'atom'); $metadata->registerNamespace('http://purl.org/dc/elements/1.1/', 'dc'); $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'); $pMetadata->xmlNamespace = "http://purl.org/dc/elements/1.1/"; $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', 'etag');
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->name); 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); $propertiesMetadata = array(); $propertiesNodes = array(); 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 (null !== ($xmlRootNamespace = $elem->attributes()->{'xml-root-namespace'})) { $metadata->xmlRootNamespace = (string) $xmlRootNamespace; } $readOnlyClass = 'true' === strtolower($elem->attributes()->{'read-only'}); $discriminatorFieldName = (string) $elem->attributes()->{'discriminator-field-name'}; $discriminatorMap = array(); foreach ($elem->xpath('./discriminator-class') as $entry) { if (!isset($entry->attributes()->value)) { throw new RuntimeException('Each discriminator-class element must have a "value" attribute.'); } $discriminatorMap[(string) $entry->attributes()->value] = (string) $entry; } if ('true' === (string) $elem->attributes()->{'discriminator-disabled'}) { $metadata->discriminatorDisabled = true; } elseif (!empty($discriminatorFieldName) || !empty($discriminatorMap)) { $metadata->setDiscriminator($discriminatorFieldName, $discriminatorMap); } foreach ($elem->xpath('./xml-namespace') as $xmlNamespace) { if (!isset($xmlNamespace->attributes()->uri)) { throw new RuntimeException('The prefix attribute must be set for all xml-namespace elements.'); } if (isset($xmlNamespace->attributes()->prefix)) { $prefix = (string) $xmlNamespace->attributes()->prefix; } else { $prefix = null; } $metadata->registerNamespace((string) $xmlNamespace->attributes()->uri, $prefix); } foreach ($elem->xpath('./virtual-property') as $method) { if (!isset($method->attributes()->method)) { throw new RuntimeException('The method attribute must be set for all virtual-property elements.'); } $virtualPropertyMetadata = new VirtualPropertyMetadata($name, (string) $method->attributes()->method); $propertiesMetadata[] = $virtualPropertyMetadata; $propertiesNodes[] = $method; } if (!$excludeAll) { foreach ($class->getProperties() as $property) { if ($name !== $property->class) { continue; } $propertiesMetadata[] = new PropertyMetadata($name, $pName = $property->getName()); $pElems = $elem->xpath("./property[@name = '" . $pName . "']"); $propertiesNodes[] = $pElems ? reset($pElems) : null; } foreach ($propertiesMetadata as $propertyKey => $pMetadata) { $isExclude = false; $isExpose = $pMetadata instanceof VirtualPropertyMetadata; $pElem = $propertiesNodes[$propertyKey]; if (!empty($pElem)) { 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->setType((string) $type); } elseif (isset($pElem->type)) { $pMetadata->setType((string) $pElem->type); } if (null !== ($groups = $pElem->attributes()->groups)) { $pMetadata->groups = preg_split('/\\s*,\\s*/', (string) $groups); } 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->{'xml-element'})) { $colConfig = $pElem->{'xml-element'}; if (isset($colConfig->attributes()->cdata)) { $pMetadata->xmlElementCData = 'true' === (string) $colConfig->attributes()->cdata; } if (isset($colConfig->attributes()->namespace)) { $pMetadata->xmlNamespace = (string) $colConfig->attributes()->namespace; } } if (isset($pElem->attributes()->{'xml-attribute'})) { $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute'}; } if (isset($pElem->attributes()->{'xml-attribute-map'})) { $pMetadata->xmlAttribute = 'true' === (string) $pElem->attributes()->{'xml-attribute-map'}; } if (isset($pElem->attributes()->{'xml-value'})) { $pMetadata->xmlValue = 'true' === (string) $pElem->attributes()->{'xml-value'}; } if (isset($pElem->attributes()->{'xml-key-value-pairs'})) { $pMetadata->xmlKeyValuePairs = 'true' === (string) $pElem->attributes()->{'xml-key-value-pairs'}; } if (isset($pElem->attributes()->{'max-depth'})) { $pMetadata->maxDepth = (int) $pElem->attributes()->{'max-depth'}; } //we need read-only before setter and getter set, because that method depends on flag being set if (null !== ($readOnly = $pElem->attributes()->{'read-only'})) { $pMetadata->readOnly = 'true' === strtolower($readOnly); } else { $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass; } $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 (null !== ($inline = $pElem->attributes()->inline)) { $pMetadata->inline = 'true' === strtolower($inline); } if (null !== ($set = $pElem->attributes()->{'recursion-groups-set'})) { throw new \Exception('recursion_groups are not implemented for yaml'); } if (null !== ($add = $pElem->attributes()->{'recursion-groups-add'})) { throw new \Exception('recursion_groups are not implemented for yaml'); } if (null !== ($remove = $pElem->attributes()->{'recursion-groups-remove'})) { throw new \Exception('recursion_groups are not implemented for yaml'); } } 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; case 'handler': if (!isset($method->attributes()->format)) { throw new RuntimeException('The format attribute must be set for "handler" callback methods.'); } if (!isset($method->attributes()->direction)) { throw new RuntimeException('The direction attribute must be set for "handler" callback methods.'); } $direction = GraphNavigator::parseDirection((string) $method->attributes()->direction); $format = (string) $method->attributes()->format; $metadata->addHandlerCallback($direction, $format, (string) $method->attributes()->name); break; default: throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name)); } } return $metadata; }
<?php use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; $metadata = new ClassMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleSubClassObject'); $metadata->registerNamespace('http://better.foo.example.org', 'foo'); $metadata->registerNamespace('http://foo.example.org', 'old_foo'); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleSubClassObject', 'moo'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://better.foo.example.org"; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleSubClassObject', 'baz'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://foo.example.org"; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleSubClassObject', 'qux'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://new.foo.example.org"; $metadata->addPropertyMetadata($pMetadata); return $metadata;
<?php use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; $metadata = new ClassMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleClassObject'); $metadata->registerNamespace('http://foo.example.org', 'foo'); $metadata->registerNamespace('http://old.foo.example.org', 'old_foo'); $metadata->registerNamespace('http://new.foo.example.org', 'new_foo'); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleClassObject', 'foo'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://old.foo.example.org"; $pMetadata->xmlAttribute = true; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleClassObject', 'bar'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://foo.example.org"; $metadata->addPropertyMetadata($pMetadata); $pMetadata = new PropertyMetadata('JMS\\Serializer\\Tests\\Fixtures\\SimpleClassObject', 'moo'); $pMetadata->setType('string'); $pMetadata->xmlNamespace = "http://new.foo.example.org"; $metadata->addPropertyMetadata($pMetadata); return $metadata;