4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Mapping\Loader;
14 use Doctrine\Common\Annotations\Reader;
15 use Symfony\Component\Serializer\Annotation\Groups;
16 use Symfony\Component\Serializer\Annotation\MaxDepth;
17 use Symfony\Component\Serializer\Exception\MappingException;
18 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
19 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
24 * @author Kévin Dunglas <dunglas@gmail.com>
26 class AnnotationLoader implements LoaderInterface
30 public function __construct(Reader $reader)
32 $this->reader = $reader;
38 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
40 $reflectionClass = $classMetadata->getReflectionClass();
41 $className = $reflectionClass->name;
44 $attributesMetadata = $classMetadata->getAttributesMetadata();
46 foreach ($reflectionClass->getProperties() as $property) {
47 if (!isset($attributesMetadata[$property->name])) {
48 $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
49 $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
52 if ($property->getDeclaringClass()->name === $className) {
53 foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
54 if ($annotation instanceof Groups) {
55 foreach ($annotation->getGroups() as $group) {
56 $attributesMetadata[$property->name]->addGroup($group);
58 } elseif ($annotation instanceof MaxDepth) {
59 $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
67 foreach ($reflectionClass->getMethods() as $method) {
68 if ($method->getDeclaringClass()->name !== $className) {
72 $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
73 if ($accessorOrMutator) {
74 $attributeName = lcfirst($matches[2]);
76 if (isset($attributesMetadata[$attributeName])) {
77 $attributeMetadata = $attributesMetadata[$attributeName];
79 $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
80 $classMetadata->addAttributeMetadata($attributeMetadata);
84 foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
85 if ($annotation instanceof Groups) {
86 if (!$accessorOrMutator) {
87 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));
90 foreach ($annotation->getGroups() as $group) {
91 $attributeMetadata->addGroup($group);
93 } elseif ($annotation instanceof MaxDepth) {
94 if (!$accessorOrMutator) {
95 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));
98 $attributeMetadata->setMaxDepth($annotation->getMaxDepth());