Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Mapping\Loader; Chris@0: Chris@0: use Doctrine\Common\Annotations\Reader; Chris@0: use Symfony\Component\Serializer\Annotation\Groups; Chris@0: use Symfony\Component\Serializer\Annotation\MaxDepth; Chris@0: use Symfony\Component\Serializer\Exception\MappingException; Chris@0: use Symfony\Component\Serializer\Mapping\AttributeMetadata; Chris@0: use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; Chris@0: Chris@0: /** Chris@0: * Annotation loader. Chris@0: * Chris@0: * @author Kévin Dunglas Chris@0: */ Chris@0: class AnnotationLoader implements LoaderInterface Chris@0: { Chris@0: private $reader; Chris@0: Chris@0: public function __construct(Reader $reader) Chris@0: { Chris@0: $this->reader = $reader; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function loadClassMetadata(ClassMetadataInterface $classMetadata) Chris@0: { Chris@0: $reflectionClass = $classMetadata->getReflectionClass(); Chris@0: $className = $reflectionClass->name; Chris@0: $loaded = false; Chris@0: Chris@0: $attributesMetadata = $classMetadata->getAttributesMetadata(); Chris@0: Chris@0: foreach ($reflectionClass->getProperties() as $property) { Chris@0: if (!isset($attributesMetadata[$property->name])) { Chris@0: $attributesMetadata[$property->name] = new AttributeMetadata($property->name); Chris@0: $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]); Chris@0: } Chris@0: Chris@0: if ($property->getDeclaringClass()->name === $className) { Chris@0: foreach ($this->reader->getPropertyAnnotations($property) as $annotation) { Chris@0: if ($annotation instanceof Groups) { Chris@0: foreach ($annotation->getGroups() as $group) { Chris@0: $attributesMetadata[$property->name]->addGroup($group); Chris@0: } Chris@0: } elseif ($annotation instanceof MaxDepth) { Chris@0: $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth()); Chris@0: } Chris@0: Chris@0: $loaded = true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: foreach ($reflectionClass->getMethods() as $method) { Chris@0: if ($method->getDeclaringClass()->name !== $className) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches); Chris@0: if ($accessorOrMutator) { Chris@0: $attributeName = lcfirst($matches[2]); Chris@0: Chris@0: if (isset($attributesMetadata[$attributeName])) { Chris@0: $attributeMetadata = $attributesMetadata[$attributeName]; Chris@0: } else { Chris@0: $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName); Chris@0: $classMetadata->addAttributeMetadata($attributeMetadata); Chris@0: } Chris@0: } Chris@0: Chris@0: foreach ($this->reader->getMethodAnnotations($method) as $annotation) { Chris@0: if ($annotation instanceof Groups) { Chris@0: if (!$accessorOrMutator) { Chris@0: throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); Chris@0: } Chris@0: Chris@0: foreach ($annotation->getGroups() as $group) { Chris@0: $attributeMetadata->addGroup($group); Chris@0: } Chris@0: } elseif ($annotation instanceof MaxDepth) { Chris@0: if (!$accessorOrMutator) { Chris@0: throw new MappingException(sprintf('MaxDepth on "%s::%s" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); Chris@0: } Chris@0: Chris@0: $attributeMetadata->setMaxDepth($annotation->getMaxDepth()); Chris@0: } Chris@0: Chris@0: $loaded = true; Chris@0: } Chris@0: } Chris@0: Chris@0: return $loaded; Chris@0: } Chris@0: }