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
34 * @param Reader $reader
36 public function __construct(Reader $reader)
38 $this->reader = $reader;
44 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
46 $reflectionClass = $classMetadata->getReflectionClass();
47 $className = $reflectionClass->name;
50 $attributesMetadata = $classMetadata->getAttributesMetadata();
52 foreach ($reflectionClass->getProperties() as $property) {
53 if (!isset($attributesMetadata[$property->name])) {
54 $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
55 $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
58 if ($property->getDeclaringClass()->name === $className) {
59 foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
60 if ($annotation instanceof Groups) {
61 foreach ($annotation->getGroups() as $group) {
62 $attributesMetadata[$property->name]->addGroup($group);
64 } elseif ($annotation instanceof MaxDepth) {
65 $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
73 foreach ($reflectionClass->getMethods() as $method) {
74 if ($method->getDeclaringClass()->name !== $className) {
78 $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
79 if ($accessorOrMutator) {
80 $attributeName = lcfirst($matches[2]);
82 if (isset($attributesMetadata[$attributeName])) {
83 $attributeMetadata = $attributesMetadata[$attributeName];
85 $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
86 $classMetadata->addAttributeMetadata($attributeMetadata);
90 foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
91 if ($annotation instanceof Groups) {
92 if (!$accessorOrMutator) {
93 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));
96 foreach ($annotation->getGroups() as $group) {
97 $attributeMetadata->addGroup($group);
99 } elseif ($annotation instanceof MaxDepth) {
100 if (!$accessorOrMutator) {
101 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));
104 $attributeMetadata->setMaxDepth($annotation->getMaxDepth());