2015-08-18 00:00:26 +00:00
< ? 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\Validator\Mapping ;
use Symfony\Component\Validator\Constraint ;
use Symfony\Component\Validator\Constraints\Traverse ;
use Symfony\Component\Validator\Constraints\Valid ;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException ;
/**
* A generic container of { @ link Constraint } objects .
*
* This class supports serialization and cloning .
*
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
class GenericMetadata implements MetadataInterface
{
/**
* @ var Constraint []
*
* @ internal This property is public in order to reduce the size of the
* class ' serialized representation . Do not access it . Use
* { @ link getConstraints ()} and { @ link findConstraints ()} instead .
*/
public $constraints = array ();
/**
* @ var array
*
* @ internal This property is public in order to reduce the size of the
* class ' serialized representation . Do not access it . Use
* { @ link findConstraints ()} instead .
*/
public $constraintsByGroup = array ();
/**
* The strategy for cascading objects .
*
* By default , objects are not cascaded .
*
* @ var int
*
* @ see CascadingStrategy
*
* @ internal This property is public in order to reduce the size of the
* class ' serialized representation . Do not access it . Use
* { @ link getCascadingStrategy ()} instead .
*/
public $cascadingStrategy = CascadingStrategy :: NONE ;
/**
* The strategy for traversing traversable objects .
*
* By default , traversable objects are not traversed .
*
* @ var int
*
* @ see TraversalStrategy
*
* @ internal This property is public in order to reduce the size of the
* class ' serialized representation . Do not access it . Use
* { @ link getTraversalStrategy ()} instead .
*/
public $traversalStrategy = TraversalStrategy :: NONE ;
/**
* Returns the names of the properties that should be serialized .
*
* @ return string []
*/
public function __sleep ()
{
return array (
'constraints' ,
'constraintsByGroup' ,
'cascadingStrategy' ,
'traversalStrategy' ,
);
}
/**
* Clones this object .
*/
public function __clone ()
{
$constraints = $this -> constraints ;
$this -> constraints = array ();
$this -> constraintsByGroup = array ();
foreach ( $constraints as $constraint ) {
$this -> addConstraint ( clone $constraint );
}
}
/**
* Adds a constraint .
*
* If the constraint { @ link Valid } is added , the cascading strategy will be
* changed to { @ link CascadingStrategy :: CASCADE } . Depending on the
2018-11-23 12:29:20 +00:00
* $traverse property of that constraint , the traversal strategy
2015-08-18 00:00:26 +00:00
* will be set to one of the following :
*
2018-11-23 12:29:20 +00:00
* - { @ link TraversalStrategy :: IMPLICIT } if $traverse is enabled
2015-08-18 00:00:26 +00:00
* - { @ link TraversalStrategy :: NONE } if $traverse is disabled
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*
* @ throws ConstraintDefinitionException When trying to add the
* { @ link Traverse } constraint
*/
public function addConstraint ( Constraint $constraint )
{
if ( $constraint instanceof Traverse ) {
2018-11-23 12:29:20 +00:00
throw new ConstraintDefinitionException ( sprintf ( 'The constraint "%s" can only be put on classes. Please use "Symfony\Component\Validator\Constraints\Valid" instead.' , \get_class ( $constraint )));
2015-08-18 00:00:26 +00:00
}
2018-11-23 12:29:20 +00:00
if ( $constraint instanceof Valid && null === $constraint -> groups ) {
2015-08-18 00:00:26 +00:00
$this -> cascadingStrategy = CascadingStrategy :: CASCADE ;
if ( $constraint -> traverse ) {
$this -> traversalStrategy = TraversalStrategy :: IMPLICIT ;
} else {
$this -> traversalStrategy = TraversalStrategy :: NONE ;
}
return $this ;
}
$this -> constraints [] = $constraint ;
foreach ( $constraint -> groups as $group ) {
$this -> constraintsByGroup [ $group ][] = $constraint ;
}
return $this ;
}
/**
* Adds an list of constraints .
*
* @ param Constraint [] $constraints The constraints to add
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function addConstraints ( array $constraints )
{
foreach ( $constraints as $constraint ) {
$this -> addConstraint ( $constraint );
}
return $this ;
}
/**
* { @ inheritdoc }
*/
public function getConstraints ()
{
return $this -> constraints ;
}
/**
* Returns whether this element has any constraints .
*
* @ return bool
*/
public function hasConstraints ()
{
2018-11-23 12:29:20 +00:00
return \count ( $this -> constraints ) > 0 ;
2015-08-18 00:00:26 +00:00
}
/**
* { @ inheritdoc }
*
* Aware of the global group ( * group ) .
*/
public function findConstraints ( $group )
{
return isset ( $this -> constraintsByGroup [ $group ])
? $this -> constraintsByGroup [ $group ]
: array ();
}
/**
* { @ inheritdoc }
*/
public function getCascadingStrategy ()
{
return $this -> cascadingStrategy ;
}
/**
* { @ inheritdoc }
*/
public function getTraversalStrategy ()
{
return $this -> traversalStrategy ;
}
}