8000 Added custom property accessors · symfony/symfony@2f2ad49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f2ad49

Browse files
committed
Added custom property accessors
1 parent ed57e74 commit 2f2ad49

40 files changed

+2280
-20
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\PropertyAccess\Annotation;
13+
14+
/**
15+
* Property accessor adder configuration annotation.
16+
*
17+
* @Annotation
18+
* @Target({"METHOD"})
19+
*
20+
* @author Luis Ramón López <lrlopez@gmail.com>
21+
*/
22+
class AdderAccessor
23+
{
24+
/**
25+
* Associates this method to the adder of this property.
26+
*
27+
* @var string
28+
*/
29+
public $property;
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\PropertyAccess\Annotation;
13+
14+
/**
15+
* Property accessor getter configuration annotation.
16+
*
17+
* @Annotation
18+
* @Target({"METHOD"})
19+
*
20+
* @author Luis Ramón López <lrlopez@gmail.com>
10000 21+
*/
22+
class GetterAccessor
23+
{
24+
/**
25+
* Associates this method to the getter of this property.
26+
*
27+
* @var string
28+
*/
29+
public $property;
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\PropertyAccess\Annotation;
13+
14+
/**
15+
* Property accessor configuration annotation.
16+
*
17+
* @Annotation
18+
* @Target({"PROPERTY"})
19+
*
20+
* @author Luis Ramón López <lrlopez@gmail.com>
21+
*/
22+
class PropertyAccessor
23+
{
24+
/**
25+
* Custom setter method for the property.
26+
*
27+
* @var string
28+
*/
29+
public $setter;
30+
31+
/**
32+
* Custom getter method for the property.
33+
*
34+
* @var string
35+
*/
36+
public $getter;
37+
38+
/**
39+
* Custom adder method for the property.
40+
*
41+
* @var string
42+
*/
43+
public $adder;
44+
45+
/**
46+
* Custom remover method for the property.
47+
*
48+
* @var string
49+
*/
50+
public $remover;
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\PropertyAccess\Annotation;
13+
14+
/**
15+
* Property accessor remover configuration annotation.
16+
*
17+
* @Annotation
18+
* @Target({"METHOD"})
19+
*
20+
* @author Luis Ramón López <lrlopez@gmail.com>
21+
*/
22+
class RemoverAccessor
23+
{
24+
/**
25+
* Associates this method to the remover of this property.
26+
*
27+
* @var string
28+
*/
29+
public $property;
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\PropertyAccess\Annotation;
13+
14+
/**
15+
* Property accessor setter configuration annotation.
16+
*
17+
* @Annotation
18+
* @Target({"METHOD"})
19+
*
20+
* @author Luis Ramón López <lrlopez@gmail.com>
21+
*/
22+
class SetterAccessor
23+
{
24+
/**
25+
* Associates this method to the setter of this property.
26+
*
27+
* @var string
28+
*/
29+
public $property;
30+
}
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\PropertyAccess\Exception;
13+
14+
/**
15+
* MappingException.
16+
*
17+
* @author Luis Ramón López <lrlopez@gmail.com>
18+
*/
19+
class MappingException extends RuntimeException
20+
{
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\PropertyAccess\Exception;
13+
14+
/**
15+
* @author Luis Ramón López <lrlopez@gmail.com>
16+
*/
17+
class NoSuchMetadataException extends AccessException
18+
{
19+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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\PropertyAccess\Mapping;
13+
14+
/**
15+
* @author Luis Ramón López <lrlopez@gmail.com>
16+
*/
17+
class ClassMetadata
18+
{
19+
/**
20+
* @var string
21+
*
22+
* @internal This property is public in order to reduce the size of the
23+
* class' serialized representation. Do not access it. Use
24+
* {@link getName()} instead.
25+
*/
26+
public $name;
27+
28+
/**
29+
* @var PropertyMetadata[]
30+
*
31+
* @internal This property is public in order to reduce the size of the
32+
* class' serialized representation. Do not access it. Use
33+
* {@link getPropertyMetadataCollection()} instead.
34+
*/
35+
public $propertyMetadataCollection = array();
36+
37+
/**
38+
* @var \ReflectionClass
39+
*/
40+
private $reflClass;
41+
42+
/**
43+
* Constructs a metadata for the given class.
44+
*
45+
* @param string $class
46+
*/
47+
public function __construct($class)
48+
{
49+
$this->name = $class;
50+
}
51+
52+
/**
53+
* Returns the name of the backing PHP class.
54+
*
55+
* @return string the name of the backing class
56+
*/
57+
public function getName()
58+
{
59+
return $this->name;
60+
}
61+
62+
/**
63+
* Adds an {@link AttributeMetadataInterface}.
64+
*
65+
* @param PropertyMetadata $propertyMetadata
66+
*/
67+
public function addPropertyMetadata(PropertyMetadata $propertyMetadata)
68+
{
69+
$this->propertyMetadataCollection[$propertyMetadata->getName()] = $propertyMetadata;
70+
}
71+
72+
/**
73+
* Gets the list of {@link PropertyMetadata}.
74+
*
75+
* @return PropertyMetadata[]
76+
*/
77+
public function getPropertyMetadataCollection()
78+
{
79+
return $this->propertyMetadataCollection;
80+
}
81+
82+
/**
83+
* Return metadata for a particular property, or null if it doesn't exist.
84+
*
85+
* @param string $property
86+
*
87+
* @return PropertyMetadata|null
88+
*/
89+
public function getMetadataForProperty($property)
90+
{
91+
return isset($this->propertyMetadataCollection[$property]) ? $this->propertyMetadataCollection[$property] : null;
92+
}
93+
94+
/**
95+
* Merges a {@link ClassMetadata} into the current one.
96+
*
97+
* @param self $classMetadata
98+
*/
99+
public function merge(ClassMetadata $classMetadata)
100+
{
101+
foreach ($classMetadata->getPropertyMetadataCollection() as $attributeMetadata) {
102+
if (isset($this->propertyMetadataCollection[$attributeMetadata->getName()])) {
103+
$this->propertyMetadataCollection[$attributeMetadata->getName()]->merge($attributeMetadata);
104+
} else {
105+
$this->addPropertyMetadata($attributeMetadata);
106+
}
107+
}
108+
}
109+
110+
/**
111+
* Returns a {@link \ReflectionClass} instance for this class.
112+
*
113+
* @return \ReflectionClass
114+
*/
115+
public function getReflectionClass()
116+
{
117+
if (!$this->reflClass) {
118+
$this->reflClass = new \ReflectionClass($this->getName());
119+
}
120+
121+
return $this->reflClass;
122+
}
123+
124+
/**
125+
* Returns the names of the properties that should be serialized.
126+
*
127+
* @return string[]
128+
*/
129+
public function __sleep()
130+
{
131+
return array(
132+
'name',
133+
'propertyMetadataCollection',
134+
);
135+
}
136+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\PropertyAccess\Mapping\Factory;
13+
14+
/**
15+
* Metadata factory that does not store metadata.
16+
*
17+
* This implementation is useful if you want to validate values against
18+
* constraints only and you don't need to add constraints to classes and
19+
* properties.
20+
*
21+
* @author Luis Ramón López <lrlopez@gmail.com>
22+
*/
23+
class BlackHoleMetadataFactory implements MetadataFactoryInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function getMetadataFor($value)
29+
{
30+
throw new \LogicException('This class does not support metadata.');
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function hasMetadataFor($value)
37+
{
38+
return false;
39+
}
40+
}

0 commit comments

Comments
 (0)
0