8000 [PropertyAccess] Allow custom property accessors by lrlopez · Pull Request #22190 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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
lrlopez committed Nov 6, 2017
commit e6aa7f1832fc3b8846a919976c6fb3cb9f225f9c
30 changes: 30 additions & 0 deletions src/Symfony/Component/PropertyAccess/Annotation/AdderAccessor.php
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 src/Symfony/Component/PropertyAccess/Annotation/GetterAccessor.php
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;
}
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;
}
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 src/Symfony/Component/PropertyAccess/Annotation/SetterAccessor.php
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;
}
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
{
}
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 src/Symfony/Component/PropertyAccess/Mapping/ClassMetadata.php
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)
Copy link
Member

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).

Copy link
Contributor Author

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.

{
$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',
);
}
}
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;
}
}
Loading
0