annotate vendor/symfony/validator/Mapping/GetterMetadata.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Validator\Mapping;
Chris@0 13
Chris@0 14 use Symfony\Component\Validator\Exception\ValidatorException;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Stores all metadata needed for validating a class property via its getter
Chris@0 18 * method.
Chris@0 19 *
Chris@0 20 * A property getter is any method that is equal to the property's name,
Chris@0 21 * prefixed with either "get" or "is". That method will be used to access the
Chris@0 22 * property's value.
Chris@0 23 *
Chris@0 24 * The getter will be invoked by reflection, so the access of private and
Chris@0 25 * protected getters is supported.
Chris@0 26 *
Chris@0 27 * This class supports serialization and cloning.
Chris@0 28 *
Chris@0 29 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 30 *
Chris@0 31 * @see PropertyMetadataInterface
Chris@0 32 */
Chris@0 33 class GetterMetadata extends MemberMetadata
Chris@0 34 {
Chris@0 35 /**
Chris@0 36 * Constructor.
Chris@0 37 *
Chris@0 38 * @param string $class The class the getter is defined on
Chris@0 39 * @param string $property The property which the getter returns
Chris@0 40 * @param string|null $method The method that is called to retrieve the value being validated (null for auto-detection)
Chris@0 41 *
Chris@0 42 * @throws ValidatorException
Chris@0 43 */
Chris@0 44 public function __construct($class, $property, $method = null)
Chris@0 45 {
Chris@0 46 if (null === $method) {
Chris@0 47 $getMethod = 'get'.ucfirst($property);
Chris@0 48 $isMethod = 'is'.ucfirst($property);
Chris@0 49 $hasMethod = 'has'.ucfirst($property);
Chris@0 50
Chris@0 51 if (method_exists($class, $getMethod)) {
Chris@0 52 $method = $getMethod;
Chris@0 53 } elseif (method_exists($class, $isMethod)) {
Chris@0 54 $method = $isMethod;
Chris@0 55 } elseif (method_exists($class, $hasMethod)) {
Chris@0 56 $method = $hasMethod;
Chris@0 57 } else {
Chris@0 58 throw new ValidatorException(sprintf('Neither of these methods exist in class %s: %s, %s, %s', $class, $getMethod, $isMethod, $hasMethod));
Chris@0 59 }
Chris@0 60 } elseif (!method_exists($class, $method)) {
Chris@0 61 throw new ValidatorException(sprintf('The %s() method does not exist in class %s.', $method, $class));
Chris@0 62 }
Chris@0 63
Chris@0 64 parent::__construct($class, $method, $property);
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * {@inheritdoc}
Chris@0 69 */
Chris@0 70 public function getPropertyValue($object)
Chris@0 71 {
Chris@0 72 return $this->newReflectionMember($object)->invoke($object);
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 protected function newReflectionMember($objectOrClassName)
Chris@0 79 {
Chris@0 80 return new \ReflectionMethod($objectOrClassName, $this->getName());
Chris@0 81 }
Chris@0 82 }