2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Validator\Mapping;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class ClassMetadata extends ElementMetadata
|
|
|
|
{
|
2010-06-29 16:20:29 +01:00
|
|
|
public $name;
|
|
|
|
public $shortName;
|
|
|
|
public $members = array();
|
|
|
|
public $properties = array();
|
|
|
|
public $getters = array();
|
|
|
|
public $groupSequence = array();
|
2010-06-24 10:24:08 +01:00
|
|
|
private $reflClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a metadata for the given class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
*/
|
|
|
|
public function __construct($class)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->name = $class;
|
|
|
|
$this->shortName = substr($class, strrpos($class, '\\') + 1);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-29 16:20:29 +01:00
|
|
|
/**
|
|
|
|
* Returns the properties to be serialized
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
return array_merge(parent::__sleep(), array(
|
|
|
|
'getters',
|
|
|
|
'groupSequence',
|
|
|
|
'members',
|
|
|
|
'name',
|
|
|
|
'properties',
|
|
|
|
'shortName'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the fully qualified name of the class
|
|
|
|
*
|
|
|
|
* @return string The fully qualified class name
|
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the class name without namespace
|
|
|
|
*
|
|
|
|
* @return string The local class name in the namespace
|
|
|
|
*/
|
|
|
|
public function getShortClassName()
|
|
|
|
{
|
|
|
|
return $this->shortName;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function addConstraint(Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$constraint->addImplicitGroupName($this->getShortClassName());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
parent::addConstraint($constraint);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Adds a constraint to the given property
|
|
|
|
*
|
|
|
|
* @param string $property The name of the property
|
|
|
|
* @param Constraint $constraint The constraint
|
|
|
|
* @return ClassMetadata This object
|
|
|
|
*/
|
|
|
|
public function addPropertyConstraint($property, Constraint $constraint)
|
|
|
|
{
|
|
|
|
if (!isset($this->properties[$property])) {
|
|
|
|
$this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->addMemberMetadata($this->properties[$property]);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$constraint->addImplicitGroupName($this->getShortClassName());
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->properties[$property]->addConstraint($constraint);
|
|
|
|
|
|
|
|
return $this;
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Adds a constraint to the getter of the given property
|
|
|
|
*
|
|
|
|
* The name of the getter is assumed to be the name of the property with an
|
|
|
|
* uppercased first letter and either the prefix "get" or "is".
|
|
|
|
*
|
|
|
|
* @param string $property The name of the property
|
|
|
|
* @param Constraint $constraint The constraint
|
|
|
|
* @return ClassMetadata This object
|
|
|
|
*/
|
|
|
|
public function addGetterConstraint($property, Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!isset($this->getters[$property])) {
|
|
|
|
$this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
|
|
|
|
|
|
|
|
$this->addMemberMetadata($this->getters[$property]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$constraint->addImplicitGroupName($this->getShortClassName());
|
|
|
|
|
|
|
|
$this->getters[$property]->addConstraint($constraint);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Merges the constraints of the given metadata into this object
|
|
|
|
*
|
|
|
|
* @param ClassMetadata $source The source metadata
|
|
|
|
*/
|
|
|
|
public function mergeConstraints(ClassMetadata $source)
|
|
|
|
{
|
|
|
|
foreach ($source->getConstraints() as $constraint) {
|
|
|
|
$this->addConstraint(clone $constraint);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
foreach ($source->getConstrainedProperties() as $property) {
|
|
|
|
foreach ($source->getMemberMetadatas($property) as $member) {
|
|
|
|
$member = clone $member;
|
|
|
|
|
|
|
|
foreach ($member->getConstraints() as $constraint) {
|
|
|
|
$constraint->addImplicitGroupName($this->getShortClassName());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addMemberMetadata($member);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!$member->isPrivate()) {
|
|
|
|
$property = $member->getPropertyName();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
|
|
|
|
$this->properties[$property] = $member;
|
|
|
|
} else if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
|
|
|
|
$this->getters[$property] = $member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a member metadata
|
|
|
|
*
|
|
|
|
* @param MemberMetadata $metadata
|
|
|
|
*/
|
|
|
|
protected function addMemberMetadata(MemberMetadata $metadata)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$property = $metadata->getPropertyName();
|
|
|
|
|
|
|
|
if (!isset($this->members[$property])) {
|
|
|
|
$this->members[$property] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->members[$property][] = $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all metadatas of members describing the given property
|
|
|
|
*
|
|
|
|
* @param string $property The name of the property
|
|
|
|
*/
|
|
|
|
public function getMemberMetadatas($property)
|
|
|
|
{
|
|
|
|
return $this->members[$property];
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns all properties for which constraints are defined
|
|
|
|
*
|
|
|
|
* @return array An array of property names
|
|
|
|
*/
|
|
|
|
public function getConstrainedProperties()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
return array_keys($this->members);
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Sets the default group sequence for this class
|
|
|
|
*
|
|
|
|
* @param array $groups An array of group names
|
|
|
|
*/
|
|
|
|
public function setGroupSequence(array $groups)
|
|
|
|
{
|
|
|
|
$this->groupSequence = $groups;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this class has an overridden default group sequence
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasGroupSequence()
|
|
|
|
{
|
|
|
|
return count($this->groupSequence) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default group sequence for this class
|
|
|
|
*
|
|
|
|
* @return array An array of group names
|
|
|
|
*/
|
|
|
|
public function getGroupSequence()
|
|
|
|
{
|
|
|
|
return $this->groupSequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a ReflectionClass instance for this class
|
|
|
|
*
|
|
|
|
* @return ReflectionClass
|
|
|
|
*/
|
|
|
|
public function getReflectionClass()
|
|
|
|
{
|
|
|
|
if (!$this->reflClass) {
|
|
|
|
$this->reflClass = new \ReflectionClass($this->getClassName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->reflClass;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|