-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess] Allow custom property accessors #22190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added custom property accessors
- Loading branch information
commit e6aa7f1832fc3b8846a919976c6fb3cb9f225f9c
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/PropertyAccess/Annotation/AdderAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Annotation; | ||
|
||
/** | ||
* Property accessor adder configuration annotation. | ||
* | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class AdderAccessor | ||
{ | ||
/** | ||
* Associates this method to the adder of this property. | ||
* | ||
* @var string | ||
*/ | ||
public $property; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/PropertyAccess/Annotation/GetterAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Annotation; | ||
|
||
/** | ||
* Property accessor getter configuration annotation. | ||
* | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class GetterAccessor | ||
{ | ||
/** | ||
* Associates this method to the getter of this property. | ||
* | ||
* @var string | ||
*/ | ||
public $property; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/PropertyAccess/Annotation/PropertyAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Annotation; | ||
|
||
/** | ||
* Property accessor configuration annotation. | ||
* | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class PropertyAccessor | ||
{ | ||
/** | ||
* Custom setter method for the property. | ||
* | ||
* @var string | ||
*/ | ||
public $setter; | ||
|
||
/** | ||
* Custom getter method for the property. | ||
* | ||
* @var string | ||
*/ | ||
public $getter; | ||
|
||
/** | ||
* Custom adder method for the property. | ||
* | ||
* @var string | ||
*/ | ||
public $adder; | ||
|
||
/** | ||
* Custom remover method for the property. | ||
* | ||
* @var string | ||
*/ | ||
public $remover; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/PropertyAccess/Annotation/RemoverAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Annotation; | ||
|
||
/** | ||
* Property accessor remover configuration annotation. | ||
* | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class RemoverAccessor | ||
{ | ||
/** | ||
* Associates this method to the remover of this property. | ||
* | ||
* @var string | ||
*/ | ||
public $property; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/PropertyAccess/Annotation/SetterAccessor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Annotation; | ||
|
||
/** | ||
* Property accessor setter configuration annotation. | ||
* | ||
* @Annotation | ||
* @Target({"METHOD"}) | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class SetterAccessor | ||
{ | ||
/** | ||
* Associates this method to the setter of this property. | ||
* | ||
* @var string | ||
*/ | ||
public $property; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/PropertyAccess/Exception/MappingException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Exception; | ||
|
||
/** | ||
* MappingException. | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class MappingException extends RuntimeException | ||
{ | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/PropertyAccess/Exception/NoSuchMetadataException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Exception; | ||
|
||
/** | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class NoSuchMetadataException extends AccessException | ||
{ | ||
} |
136 changes: 136 additions & 0 deletions
136
src/Symfony/Component/PropertyAccess/Mapping/ClassMetadata.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Mapping; | ||
|
||
/** | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class ClassMetadata | ||
{ | ||
/** | ||
* @var string | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getName()} instead. | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var PropertyMetadata[] | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getPropertyMetadataCollection()} instead. | ||
*/ | ||
public $propertyMetadataCollection = array(); | ||
|
||
/** | ||
* @var \ReflectionClass | ||
*/ | ||
private $reflClass; | ||
|
||
/** | ||
* Constructs a metadata for the given class. | ||
* | ||
* @param string $class | ||
*/ | ||
public function __construct($class) | ||
{ | ||
$this->name = $class; | ||
} | ||
|
||
/** | ||
* Returns the name of the backing PHP class. | ||
* | ||
* @return string the name of the backing class | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Adds an {@link AttributeMetadataInterface}. | ||
* | ||
* @param PropertyMetadata $propertyMetadata | ||
*/ | ||
public function addPropertyMetadata(PropertyMetadata $propertyMetadata) | ||
{ | ||
$this->propertyMetadataCollection[$propertyMetadata->getName()] = $propertyMetadata; | ||
} | ||
|
||
/** | ||
* Gets the list of {@link PropertyMetadata}. | ||
* | ||
* @return PropertyMetadata[] | ||
*/ | ||
public function getPropertyMetadataCollection() | ||
{ | ||
return $this->propertyMetadataCollection; | ||
} | ||
|
||
/** | ||
* Return metadata for a particular property, or null if it doesn't exist. | ||
* | ||
* @param string $property | ||
* | ||
* @return PropertyMetadata|null | ||
*/ | ||
public function getMetadataForProperty($property) | ||
{ | ||
return isset($this->propertyMetadataCollection[$property]) ? $this->propertyMetadataCollection[$property] : null; | ||
} | ||
|
||
/** | ||
* Merges a {@link ClassMetadata} into the current one. | ||
* | ||
* @param self $classMetadata | ||
*/ | ||
public function merge(ClassMetadata $classMetadata) | ||
{ | ||
foreach ($classMetadata->getPropertyMetadataCollection() as $attributeMetadata) { | ||
if (isset($this->propertyMetadataCollection[$attributeMetadata->getName()])) { | ||
$this->propertyMetadataCollection[$attributeMetadata->getName()]->merge($attributeMetadata); | ||
} else { | ||
$this->addPropertyMetadata($attributeMetadata); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns a {@link \ReflectionClass} instance for this class. | ||
* | ||
* @return \ReflectionClass | ||
*/ | ||
public function getReflectionClass() | ||
{ | ||
if (!$this->reflClass) { | ||
$this->reflClass = new \ReflectionClass($this->getName()); | ||
} | ||
|
||
return $this->reflClass; | ||
} | ||
|
||
/** | ||
* Returns the names of the properties that should be serialized. | ||
* | ||
* @return string[] | ||
*/ | ||
public function __sleep() | ||
{ | ||
return array( | ||
'name', | ||
'propertyMetadataCollection', | ||
); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/PropertyAccess/Mapping/Factory/BlackHoleMetadataFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyAccess\Mapping\Factory; | ||
|
||
/** | ||
* Metadata factory that does not store metadata. | ||
* | ||
* This implementation is useful if you want to validate values against | ||
* constraints only and you don't need to add constraints to classes and | ||
* properties. | ||
* | ||
* @author Luis Ramón López <lrlopez@gmail.com> | ||
*/ | ||
class BlackHoleMetadataFactory implements MetadataFactoryInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMetadataFor($value) | ||
{ | ||
throw new \LogicException('This class does not support metadata.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasMetadataFor($value) | ||
{ | ||
return false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we can make this value object immutable? We do that for new VO (PSR-7 style, see the PropertyInfo component for instance).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be done. But I'm curious about what would be the benefits. This class is used internally only, it's not exposed outside the component. Some parts of this code are borrowed from the
Serializer
component but I infer that the current implementation intent is to allow fast & easy merging of metadata from different sources and classes in the hierarchy.