فهرست منبع

[Validator] Made all metadata classes serializable

Bernhard Schussek 15 سال پیش
والد
کامیت
f6b9d9e046

+ 23 - 6
src/Symfony/Components/Validator/Mapping/ClassMetadata.php

@@ -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
      *

+ 16 - 4
src/Symfony/Components/Validator/Mapping/ElementMetadata.php

@@ -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

+ 17 - 3
src/Symfony/Components/Validator/Mapping/MemberMetadata.php

@@ -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
      *

+ 12 - 0
tests/Symfony/Tests/Components/Validator/Mapping/ClassMetadataTest.php

@@ -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);
+    }
 }
 

+ 13 - 1
tests/Symfony/Tests/Components/Validator/Mapping/ElementMetadataTest.php

@@ -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 {}

+ 45 - 0
tests/Symfony/Tests/Components/Validator/Mapping/MemberMetadataTest.php

@@ -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()
+    {
+    }
+}