[Validator] Made all metadata classes serializable

This commit is contained in:
Bernhard Schussek 2010-06-29 17:20:29 +02:00
parent a747987625
commit f6b9d9e046
6 changed files with 126 additions and 14 deletions

View File

@ -7,12 +7,12 @@ use Symfony\Components\Validator\Exception\ValidatorException;
class ClassMetadata extends ElementMetadata
{
private $name;
private $shortName;
private $members = array();
private $properties = array();
private $getters = array();
private $groupSequence = array();
public $name;
public $shortName;
public $members = array();
public $properties = array();
public $getters = array();
public $groupSequence = array();
private $reflClass;
/**
@ -26,6 +26,23 @@ class ClassMetadata extends ElementMetadata
$this->shortName = substr($class, strrpos($class, '\\') + 1);
}
/**
* Returns the properties to be serialized
*
* @return array
*/
public function __sleep()
{
return array_merge(parent::__sleep(), array(
'getters',
'groupSequence',
'members',
'name',
'properties',
'shortName'
));
}
/**
* Returns the fully qualified name of the class
*

View File

@ -2,13 +2,25 @@
namespace Symfony\Components\Validator\Mapping;
use \ReflectionClass;
use Symfony\Components\Validator\Constraint;
class ElementMetadata
abstract class ElementMetadata
{
private $constraints = array();
private $constraintsByGroup = array();
public $constraints = array();
public $constraintsByGroup = array();
/**
* Returns the names of the properties that should be serialized
*
* @return array
*/
public function __sleep()
{
return array(
'constraints',
'constraintsByGroup',
);
}
/**
* Clones this object

View File

@ -6,9 +6,9 @@ use Symfony\Components\Validator\Exception\ValidatorException;
abstract class MemberMetadata extends ElementMetadata
{
private $class;
private $name;
private $property;
public $class;
public $name;
public $property;
private $reflMember;
/**
@ -25,6 +25,20 @@ abstract class MemberMetadata extends ElementMetadata
$this->property = $property;
}
/**
* Returns the names of the properties that should be serialized
*
* @return array
*/
public function __sleep()
{
return array_merge(parent::__sleep(), array(
'class',
'name',
'property'
));
}
/**
* Returns the name of the member
*

View File

@ -119,5 +119,17 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($reflClass, $this->metadata->getReflectionClass());
}
public function testSerialize()
{
$this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
$this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->addGetterConstraint('lastName', new ConstraintB());
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
}

View File

@ -15,7 +15,7 @@ class ElementMetadataTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->metadata = new ElementMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
$this->metadata = new TestElementMetadata('Symfony\Tests\Components\Validator\Fixtures\Entity');
}
public function testAddConstraints()
@ -47,4 +47,16 @@ class ElementMetadataTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array($constraint1), $this->metadata->findConstraints('TestGroup'));
}
public function testSerialize()
{
$this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
$this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
}
class TestElementMetadata extends ElementMetadata {}

View File

@ -0,0 +1,45 @@
<?php
namespace Symfony\Tests\Components\Validator\Mapping;
require_once __DIR__.'/../Fixtures/ConstraintA.php';
require_once __DIR__.'/../Fixtures/ConstraintB.php';
use Symfony\Tests\Components\Validator\Fixtures\ConstraintA;
use Symfony\Tests\Components\Validator\Fixtures\ConstraintB;
use Symfony\Components\Validator\Mapping\MemberMetadata;
class MemberMetadataTest extends \PHPUnit_Framework_TestCase
{
protected $metadata;
public function setUp()
{
$this->metadata = new TestMemberMetadata(
'Symfony\Tests\Components\Validator\Fixtures\Entity',
'getLastName',
'lastName'
);
}
public function testSerialize()
{
$this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
$this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
}
class TestMemberMetadata extends MemberMetadata
{
public function getValue($object)
{
}
protected function newReflectionMember()
{
}
}