8000 [Serializer] Serialization groups support · symfony/symfony@57a191b · GitHub
[go: up one dir, main page]

Skip to content

Commit 57a191b

Browse files
dunglasfabpot
authored andcommitted
[Serializer] Serialization groups support
1 parent affcafc commit 57a191b

30 files changed

+1611
-146
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Annotation;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
16+
/**
17+
* Annotation class for @Groups().
18+
*
19+
* @Annotation
20+
* @Target({"PROPERTY", "METHOD"})
21+
*
22+
* @author Kévin Dunglas <dunglas@gmail.com>
23+
*/
24+
class Groups
25+
{
26+
/**
27+
* @var array
28+
*/
29+
private $groups;
30+
31+
/**
32+
* @param array $data
33+
* @throws \InvalidArgumentException
34+
*/
35+
public function __construct(array $data)
36+
{
37+
if (!isset($data['value']) || !$data['value']) {
38+
throw new InvalidArgumentException(sprintf("Parameter of annotation '%s' cannot be empty.", get_class($this)));
39+
}
40+
41+
if (!is_array($data['value'])) {
42+
throw new InvalidArgumentException(sprintf("Parameter of annotation '%s' must be an array of strings.", get_class($this)));
43+
}
44+
45+
foreach ($data['value'] as $group) {
46+
if (!is_string($group)) {
47+
throw new InvalidArgumentException(sprintf("Parameter of annotation '%s' must be an array of strings.", get_class($this)));
48+
}
49+
}
50+
51+
$this->groups = $data['value'];
52+
}
53+
54+
/**
55+
* Gets groups
56+
*
57+
* @return array
58+
*/
59+
public function getGroups()
60+
{
61+
return $this->groups;
62+
}
63+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Exception;
13+
14+
/**
15+
* MappingException
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class MappingException extends RuntimeException
20+
{
21+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Mapping;
13+
14+
/**
15+
* Stores all metadata needed for serializing objects of specific class.
16+
*
17+
* Primarily, the metadata stores serialization groups.
18+
*
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class ClassMetadata
22+
{
23+
/**
24+
* @var string
25+
*
26+
* @internal This property is public in order to reduce the size of the
27+
* class' serialized representation. Do not access it. Use
28+
* {@link getClassName()} instead.
29+
*/
30+
public $name;
31+
32+
/**
33+
* @var array
34+
*
35+
* @internal This property is public in order to reduce the size of the
36+
* class' serialized representation. Do not access it. Use
37+
* {@link getGroups()} instead.
38+
*/
39+
public $attributesGroups = array();
40+
41+
/**
42+
* @var \ReflectionClass
43+
*/
44+
private $reflClass;
45+
46+
/**
47+
* Constructs a metadata for the given class.
48+
*
49+
* @param string $class
50+
*/
51+
public function __construct($class)
52+
{
53+
$this->name = $class;
54+
}
55+
56+
/**
57+
* Returns the name of the backing PHP class.
58+
*
59+
* @return string The name of the backing class.
60+
*/
61+
public function getClassName()
62+
{
63+
return $this->name;
64+
}
65+
66+
/**
67+
* Gets serialization groups.
68+
*
69+
* @return array
70+
*/
71+
public function getAttributesGroups()
72+
{
73+
return $this->attributesGroups;
74+
}
75+
76+
/**
77+
* Adds an attribute to a serialization group
78+
*
79+
* @param string $attribute
80+
* @param string $group
81+
* @throws \InvalidArgumentException
82+
*/
83+
public function addAttributeGroup($attribute, $group)
84+
{
85+
if (!is_string($attribute) || !is_string($group)) {
86+
throw new \InvalidArgumentException('Arguments must be strings.');
87+
}
88+
89+
if (!isset($this->groups[$group]) || !in_array($attribute, $this->attributesGroups[$group])) {
90+
$this->attributesGroups[$group][] = $attribute;
91+
}
92+
}
93+
94+
/**
95+
* Merges attributes' groups.
96+
*
97+
* @param ClassMetadata $classMetadata
98+
*/
99+
public function mergeAttributesGroups(ClassMetadata $classMetadata)
100+
{
101+
foreach ($classMetadata->getAttributesGroups() as $group => $attributes) {
102+
foreach ($attributes as $attribute) {
103+
$this->addAttributeGroup($attribute, $group);
104+
}
105+
}
106+
}
107+
108+
/**
109+
* Returns a ReflectionClass instance for this class.
110+
*
111+
* @return \ReflectionClass
112+
*/
113+
public function getReflectionClass()
114+
{
115+
if (!$this->reflClass) {
116+
$this->reflClass = new \ReflectionClass($this->getClassName());
117+
}
118+
119+
return $this->reflClass;
120+
}
121+
122+
/**
123+
* Returns the names of the properties that should be serialized.
124+
*
125+
* @return string[]
126+
*/
127+
public function __sleep()
128+
{
129+
return array(
130+
'name',
131+
'attributesGroups',
132+
);
133+
}
134+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Mapping\Factory;
13+
14+
use Doctrine\Common\Cache\Cache;
15+
use Symfony\Component\Serializer\Mapping\ClassMetadata;
16+
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
17+
18+
/**
19+
* Returns a {@link ClassMetadata}.
20+
*
21+
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*/
23+
class ClassMetadataFactory
24+
{
25+
/**
26+
* @var LoaderInterface
27+
*/
28+
private $loader;
29+
/**
30+
* @var Cache
31+
*/
32+
private $cache;
33+
/**
34+
* @var array
35+
*/
36+
private $loadedClasses;
37+
38+
/**
39+
* @param LoaderInterface $loader
40+
* @param Cache|null $cache
41+
*/
42+
public function __construct(LoaderInterface $loader, Cache $cache = null)
43+
{
44+
$this->loader = $loader;
45+
$this->cache = $cache;
46+
}
47+
48+
/**
49+
* If the method was called with the same class name (or an object of that
50+
* class) before, the same metadata instance is returned.
51+
*
52+
* If the factory was configured with a cache, this method will first look
53+
* for an existing metadata instance in the cache. If an existing instance
54+
* is found, it will be returned without further ado.
55+
*
56+
* Otherwise, a new metadata instance is created. If the factory was
57+
* configured with a loader, the metadata is passed to the
58+
* {@link LoaderInterface::loadClassMetadata()} method for further
59+
* configuration. At last, the new object is returned.
60+
*
61+
* @param string|object $value
62+
* @return ClassMetadata
63+
* @throws \InvalidArgumentException
64+
65+
*/
66+
public function getMetadataFor($value)
67+
{
68+
$class = $this->getClass($value);
69+
if (!$class) {
70+
throw new \InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value)));
71+
}
72+
73+
if (isset($this->loadedClasses[$class])) {
74+
return $this->loadedClasses[$class];
75+
}
76+
77+
if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) {
78+
return $this->loadedClasses[$class];
79+
}
80+
81+
if (!class_exists($class) && !interface_exists($class)) {
82+
throw new \InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $class));
83+
}
84+
85+
$metadata = new ClassMetadata($class);
86+
87+
$reflClass = $metadata->getReflectionClass();
88+
89+
// Include constraints from the parent class
90+
if ($parent = $reflClass->getParentClass()) {
91+
$metadata->mergeAttributesGroups($this->getMetadataFor($parent->name));
92+
}
93+
94+
// Include constraints from all implemented interfaces
95+
foreach ($reflClass->getInterfaces() as $interface) {
96+
$metadata->mergeAttributesGroups($this->getMetadataFor($interface->name));
97+
}
98+
99+
if ($this->loader) {
100+
$this->loader->loadClassMetadata($metadata);
101+
}
102+
103+
if ($this->cache) {
104+
$this->cache->save($class, $metadata);
105+
}
106+
107+
return $this->loadedClasses[$class] = $metadata;
108+
}
109+
110+
/**
111+
* Checks if class has metadata.
112+
*
113+
* @param mixed $value
114+
* @return bool
115+
*/
116+
public function hasMetadataFor($value)
117+
{
118+
$class = $this->getClass($value);
119+
120+
return class_exists($class) || interface_exists($class);
121+
}
122+
123+
/**
124+
* Gets a class name for a given class or instance.
125+
*
126+
* @param $value
127+
* @return string|bool
128+
*/
129+
private function getClass($value)
130+
{
131+
if (!is_object($value) && !is_string($value)) {
132+
return false;
133+
}
134+
135+
return ltrim(is_object($value) ? get_class($value) : $value, '\\');
136+
}
137+
}

0 commit comments

Comments
 (0)
0