vendor/symfony/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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\Validator\Mapping;
       
    13 
       
    14 use Symfony\Component\Validator\Constraint;
       
    15 use Symfony\Component\Validator\Constraints\Valid;
       
    16 use Symfony\Component\Validator\Exception\ValidatorException;
       
    17 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
       
    18 
       
    19 abstract class MemberMetadata extends ElementMetadata
       
    20 {
       
    21     public $class;
       
    22     public $name;
       
    23     public $property;
       
    24     public $cascaded = false;
       
    25     public $collectionCascaded = false;
       
    26     private $reflMember;
       
    27 
       
    28     /**
       
    29      * Constructor.
       
    30      *
       
    31      * @param string $class    The name of the class this member is defined on
       
    32      * @param string $name     The name of the member
       
    33      * @param string $property The property the member belongs to
       
    34      */
       
    35     public function __construct($class, $name, $property)
       
    36     {
       
    37         $this->class = $class;
       
    38         $this->name = $name;
       
    39         $this->property = $property;
       
    40     }
       
    41 
       
    42     /**
       
    43      * {@inheritDoc}
       
    44      */
       
    45     public function addConstraint(Constraint $constraint)
       
    46     {
       
    47         if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
       
    48             throw new ConstraintDefinitionException(sprintf(
       
    49                 'The constraint %s cannot be put on properties or getters',
       
    50                 get_class($constraint)
       
    51             ));
       
    52         }
       
    53 
       
    54         if ($constraint instanceof Valid) {
       
    55             $this->cascaded = true;
       
    56             $this->collectionCascaded = $constraint->traverse;
       
    57         } else {
       
    58             parent::addConstraint($constraint);
       
    59         }
       
    60 
       
    61         return $this;
       
    62     }
       
    63 
       
    64     /**
       
    65      * Returns the names of the properties that should be serialized
       
    66      *
       
    67      * @return array
       
    68      */
       
    69     public function __sleep()
       
    70     {
       
    71         return array_merge(parent::__sleep(), array(
       
    72             'class',
       
    73             'name',
       
    74             'property',
       
    75             'cascaded', // TESTME
       
    76         ));
       
    77     }
       
    78 
       
    79     /**
       
    80      * Returns the name of the member
       
    81      *
       
    82      * @return string
       
    83      */
       
    84     public function getName()
       
    85     {
       
    86         return $this->name;
       
    87     }
       
    88 
       
    89     /**
       
    90      * Returns the class this member is defined on
       
    91      *
       
    92      * @return string
       
    93      */
       
    94     public function getClassName()
       
    95     {
       
    96         return $this->class;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Returns the name of the property this member belongs to
       
   101      *
       
   102      * @return string The property name
       
   103      */
       
   104     public function getPropertyName()
       
   105     {
       
   106         return $this->property;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Returns whether this member is public
       
   111      *
       
   112      * @return Boolean
       
   113      */
       
   114     public function isPublic()
       
   115     {
       
   116         return $this->getReflectionMember()->isPublic();
       
   117     }
       
   118 
       
   119     /**
       
   120      * Returns whether this member is protected
       
   121      *
       
   122      * @return Boolean
       
   123      */
       
   124     public function isProtected()
       
   125     {
       
   126         return $this->getReflectionMember()->isProtected();
       
   127     }
       
   128 
       
   129     /**
       
   130      * Returns whether this member is private
       
   131      *
       
   132      * @return Boolean
       
   133      */
       
   134     public function isPrivate()
       
   135     {
       
   136         return $this->getReflectionMember()->isPrivate();
       
   137     }
       
   138 
       
   139     /**
       
   140      * Returns whether objects stored in this member should be validated
       
   141      *
       
   142      * @return Boolean
       
   143      */
       
   144     public function isCascaded()
       
   145     {
       
   146         return $this->cascaded;
       
   147     }
       
   148 
       
   149     /**
       
   150      * Returns whether arrays or traversable objects stored in this member
       
   151      * should be traversed and validated in each entry
       
   152      *
       
   153      * @return Boolean
       
   154      */
       
   155     public function isCollectionCascaded()
       
   156     {
       
   157         return $this->collectionCascaded;
       
   158     }
       
   159 
       
   160     /**
       
   161      * Returns the value of this property in the given object
       
   162      *
       
   163      * @param object $object The object
       
   164      *
       
   165      * @return mixed The property value
       
   166      */
       
   167     abstract public function getValue($object);
       
   168 
       
   169     /**
       
   170      * Returns the Reflection instance of the member
       
   171      *
       
   172      * @return object
       
   173      */
       
   174     public function getReflectionMember()
       
   175     {
       
   176         if (!$this->reflMember) {
       
   177             $this->reflMember = $this->newReflectionMember();
       
   178         }
       
   179 
       
   180         return $this->reflMember;
       
   181     }
       
   182 
       
   183     /**
       
   184      * Creates a new Reflection instance for the member
       
   185      *
       
   186      * @return object
       
   187      */
       
   188     abstract protected function newReflectionMember();
       
   189 }