浏览代码

[Validator] Adding a significant amount of PHPDoc to the Validator component.

Ryan Weaver 14 年之前
父节点
当前提交
11085fd6a6

+ 3 - 0
src/Symfony/Component/Validator/Constraint.php

@@ -30,6 +30,9 @@ abstract class Constraint
 {
 {
     const DEFAULT_GROUP = 'Default';
     const DEFAULT_GROUP = 'Default';
 
 
+    /**
+     * @var array
+     */
     public $groups = array(self::DEFAULT_GROUP);
     public $groups = array(self::DEFAULT_GROUP);
 
 
     /**
     /**

+ 9 - 0
src/Symfony/Component/Validator/ConstraintValidator.php

@@ -17,6 +17,9 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
     private $messageTemplate;
     private $messageTemplate;
     private $messageParameters;
     private $messageParameters;
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function initialize(ValidationContext $context)
     public function initialize(ValidationContext $context)
     {
     {
         $this->context = $context;
         $this->context = $context;
@@ -24,11 +27,17 @@ abstract class ConstraintValidator implements ConstraintValidatorInterface
         $this->messageParameters = array();
         $this->messageParameters = array();
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function getMessageTemplate()
     public function getMessageTemplate()
     {
     {
         return $this->messageTemplate;
         return $this->messageTemplate;
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function getMessageParameters()
     public function getMessageParameters()
     {
     {
         return $this->messageParameters;
         return $this->messageParameters;

+ 10 - 0
src/Symfony/Component/Validator/ConstraintValidatorFactory.php

@@ -14,10 +14,20 @@ namespace Symfony\Component\Validator;
 use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
 use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\Constraint;
 
 
+/**
+ * Default implementation of the ConstraintValidatorFactoryInterface.
+ *
+ * This enforces the convention that the validatedBy() method on any
+ * Constrain will return the class name of the ConstraintValidator that
+ * should validate the Constraint.
+ */
 class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
 class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
 {
 {
     protected $validators = array();
     protected $validators = array();
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function getInstance(Constraint $constraint)
     public function getInstance(Constraint $constraint)
     {
     {
         $className = $constraint->validatedBy();
         $className = $constraint->validatedBy();

+ 11 - 0
src/Symfony/Component/Validator/ConstraintValidatorFactoryInterface.php

@@ -13,7 +13,18 @@ namespace Symfony\Component\Validator;
 
 
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\Constraint;
 
 
+/**
+ * Specifies an object able to return the correct ConstraintValidatorInterface
+ * instance given a Constrain object.
+ */
 interface ConstraintValidatorFactoryInterface
 interface ConstraintValidatorFactoryInterface
 {
 {
+    /**
+     * Given a Constrain, this returns the ConstraintValidatorInterface
+     * object that should be used to verify its validity.
+     *
+     * @param Constraint $constraint The source constraint
+     * @return ConstraintValidatorInterface
+     */
     function getInstance(Constraint $constraint);
     function getInstance(Constraint $constraint);
 }
 }

+ 16 - 0
src/Symfony/Component/Validator/ConstraintValidatorInterface.php

@@ -13,11 +13,27 @@ namespace Symfony\Component\Validator;
 
 
 interface ConstraintValidatorInterface
 interface ConstraintValidatorInterface
 {
 {
+    /**
+     * Initialize the constraint validator.
+     *
+     * @param ValidationContext $context The current validation context
+     */
     function initialize(ValidationContext $context);
     function initialize(ValidationContext $context);
 
 
+    /**
+     * @param  mixed $value The value that should be validated
+     * @param Constraint $constraint The constrain for the validation
+     * @return boolean Whether or not the value is valid
+     */
     function isValid($value, Constraint $constraint);
     function isValid($value, Constraint $constraint);
 
 
+    /**
+     * @return string
+     */
     function getMessageTemplate();
     function getMessageTemplate();
 
 
+    /**
+     * @return array
+     */
     function getMessageParameters();
     function getMessageParameters();
 }
 }

+ 14 - 0
src/Symfony/Component/Validator/ConstraintViolation.php

@@ -11,6 +11,9 @@ namespace Symfony\Component\Validator;
  * with this source code in the file LICENSE.
  * with this source code in the file LICENSE.
  */
  */
 
 
+/**
+ * Represents a single violation of a constraint.
+ */
 class ConstraintViolation
 class ConstraintViolation
 {
 {
     protected $messageTemplate;
     protected $messageTemplate;
@@ -28,16 +31,27 @@ class ConstraintViolation
         $this->invalidValue = $invalidValue;
         $this->invalidValue = $invalidValue;
     }
     }
 
 
+    /**
+     * @return string
+     */
     public function getMessageTemplate()
     public function getMessageTemplate()
     {
     {
         return $this->messageTemplate;
         return $this->messageTemplate;
     }
     }
 
 
+    /**
+     * @return array
+     */
     public function getMessageParameters()
     public function getMessageParameters()
     {
     {
         return $this->messageParameters;
         return $this->messageParameters;
     }
     }
 
 
+    /**
+     * Returns the violation message.
+     *
+     * @return string
+     */
     public function getMessage()
     public function getMessage()
     {
     {
         return str_replace(array_keys($this->messageParameters), array_values($this->messageParameters), $this->messageTemplate);
         return str_replace(array_keys($this->messageParameters), array_values($this->messageParameters), $this->messageTemplate);

+ 22 - 0
src/Symfony/Component/Validator/ConstraintViolationList.php

@@ -11,10 +11,16 @@ namespace Symfony\Component\Validator;
  * with this source code in the file LICENSE.
  * with this source code in the file LICENSE.
  */
  */
 
 
+/**
+ * An array-acting object that holds many ConstrainViolation instances.
+ */
 class ConstraintViolationList implements \IteratorAggregate, \Countable
 class ConstraintViolationList implements \IteratorAggregate, \Countable
 {
 {
     protected $violations = array();
     protected $violations = array();
 
 
+    /**
+     * @return string
+     */
     public function __toString()
     public function __toString()
     {
     {
         $string = '';
         $string = '';
@@ -32,11 +38,21 @@ EOF;
         return $string;
         return $string;
     }
     }
 
 
+    /**
+     * Add a ConstraintViolation to this list.
+     *
+     * @param ConstraintViolation $violation
+     */
     public function add(ConstraintViolation $violation)
     public function add(ConstraintViolation $violation)
     {
     {
         $this->violations[] = $violation;
         $this->violations[] = $violation;
     }
     }
 
 
+    /**
+     * Merge an existing ConstraintViolationList into this list.
+     *
+     * @param ConstraintViolationList $violations
+     */
     public function addAll(ConstraintViolationList $violations)
     public function addAll(ConstraintViolationList $violations)
     {
     {
         foreach ($violations->violations as $violation) {
         foreach ($violations->violations as $violation) {
@@ -44,11 +60,17 @@ EOF;
         }
         }
     }
     }
 
 
+    /**
+     * @see IteratorAggregate
+     */
     public function getIterator()
     public function getIterator()
     {
     {
         return new \ArrayIterator($this->violations);
         return new \ArrayIterator($this->violations);
     }
     }
 
 
+    /**
+     * @see Countable
+     */
     public function count()
     public function count()
     {
     {
         return count($this->violations);
         return count($this->violations);

+ 19 - 0
src/Symfony/Component/Validator/GraphWalker.php

@@ -20,6 +20,13 @@ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
 use Symfony\Component\Validator\Mapping\MemberMetadata;
 use Symfony\Component\Validator\Mapping\MemberMetadata;
 
 
+/**
+ * Responsible for walking over and initializing validation on different
+ * types of items.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
 class GraphWalker
 class GraphWalker
 {
 {
     protected $context;
     protected $context;
@@ -33,11 +40,23 @@ class GraphWalker
         $this->metadataFactory = $metadataFactory;
         $this->metadataFactory = $metadataFactory;
     }
     }
 
 
+    /**
+     * @return ConstraintViolationList
+     */
     public function getViolations()
     public function getViolations()
     {
     {
         return $this->context->getViolations();
         return $this->context->getViolations();
     }
     }
 
 
+    /**
+     * Initialize validation on the given object using the given metadata
+     * instance and validation group.
+     *
+     * @param ClassMetadata $metadata
+     * @param  object $object The object to validate
+     * @param  string $group The validator group to use for validation
+     * @param  string $propertyPath
+     */
     public function walkClass(ClassMetadata $metadata, $object, $group, $propertyPath)
     public function walkClass(ClassMetadata $metadata, $object, $group, $propertyPath)
     {
     {
         $this->context->setCurrentClass($metadata->getClassName());
         $this->context->setCurrentClass($metadata->getClassName());

+ 9 - 2
src/Symfony/Component/Validator/Mapping/ClassMetadata.php

@@ -16,6 +16,12 @@ use Symfony\Component\Validator\Constraints\Valid;
 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
 use Symfony\Component\Validator\Exception\GroupDefinitionException;
 use Symfony\Component\Validator\Exception\GroupDefinitionException;
 
 
+/**
+ * Represents all the configured constraints on a given class.
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
 class ClassMetadata extends ElementMetadata
 class ClassMetadata extends ElementMetadata
 {
 {
     public $name;
     public $name;
@@ -70,12 +76,12 @@ class ClassMetadata extends ElementMetadata
      *
      *
      * For each class, the group "Default" is an alias for the group
      * For each class, the group "Default" is an alias for the group
      * "<ClassName>", where <ClassName> is the non-namespaced name of the
      * "<ClassName>", where <ClassName> is the non-namespaced name of the
-     * class. All constraints implicitely or explicitely assigned to group
+     * class. All constraints implicitly or explicitly assigned to group
      * "Default" belong to both of these groups, unless the class defines
      * "Default" belong to both of these groups, unless the class defines
      * a group sequence.
      * a group sequence.
      *
      *
      * If a class defines a group sequence, validating the class in "Default"
      * If a class defines a group sequence, validating the class in "Default"
-     * will validate the group sequence. The constraints assinged to "Default"
+     * will validate the group sequence. The constraints assigned to "Default"
      * can still be validated by validating the class in "<ClassName>".
      * can still be validated by validating the class in "<ClassName>".
      *
      *
      * @return string  The name of the default group
      * @return string  The name of the default group
@@ -202,6 +208,7 @@ class ClassMetadata extends ElementMetadata
      * Returns all metadatas of members describing the given property
      * Returns all metadatas of members describing the given property
      *
      *
      * @param string $property The name of the property
      * @param string $property The name of the property
+     * @array of MemberMetadata
      */
      */
     public function getMemberMetadatas($property)
     public function getMemberMetadatas($property)
     {
     {

+ 18 - 0
src/Symfony/Component/Validator/ValidationContext.php

@@ -13,6 +13,15 @@ namespace Symfony\Component\Validator;
 
 
 use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
 use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
 
 
+/**
+ * The central object representing a single validation process.
+ *
+ * This object is used by the GraphWalker to initialize validation of different
+ * items and keep track of the violations.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
 class ValidationContext
 class ValidationContext
 {
 {
     protected $root;
     protected $root;
@@ -52,6 +61,9 @@ class ValidationContext
         ));
         ));
     }
     }
 
 
+    /**
+     * @return ConstraintViolationList
+     */
     public function getViolations()
     public function getViolations()
     {
     {
         return $this->violations;
         return $this->violations;
@@ -102,11 +114,17 @@ class ValidationContext
         return $this->group;
         return $this->group;
     }
     }
 
 
+    /**
+     * @return GraphWalker
+     */
     public function getGraphWalker()
     public function getGraphWalker()
     {
     {
         return $this->graphWalker;
         return $this->graphWalker;
     }
     }
 
 
+    /**
+     * @return ClassMetadataFactoryInterface
+     */
     public function getClassMetadataFactory()
     public function getClassMetadataFactory()
     {
     {
         return $this->metadataFactory;
         return $this->metadataFactory;

+ 21 - 0
src/Symfony/Component/Validator/Validator.php

@@ -15,6 +15,15 @@ use Symfony\Component\Validator\Mapping\ElementMetadata;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
 use Symfony\Component\Validator\Mapping\ClassMetadata;
 use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
 use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
 
 
+/**
+ * The default implementation of the ValidatorInterface.
+ *
+ * This service can be used to validate objects, properties and raw values
+ * against constraints.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
 class Validator implements ValidatorInterface
 class Validator implements ValidatorInterface
 {
 {
     protected $metadataFactory;
     protected $metadataFactory;
@@ -29,6 +38,9 @@ class Validator implements ValidatorInterface
         $this->validatorFactory = $validatorFactory;
         $this->validatorFactory = $validatorFactory;
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function validate($object, $groups = null)
     public function validate($object, $groups = null)
     {
     {
         $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
         $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
@@ -40,6 +52,9 @@ class Validator implements ValidatorInterface
         return $this->validateGraph($object, $walk, $groups);
         return $this->validateGraph($object, $walk, $groups);
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function validateProperty($object, $property, $groups = null)
     public function validateProperty($object, $property, $groups = null)
     {
     {
         $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
         $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
@@ -51,6 +66,9 @@ class Validator implements ValidatorInterface
         return $this->validateGraph($object, $walk, $groups);
         return $this->validateGraph($object, $walk, $groups);
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function validatePropertyValue($class, $property, $value, $groups = null)
     public function validatePropertyValue($class, $property, $value, $groups = null)
     {
     {
         $metadata = $this->metadataFactory->getClassMetadata($class);
         $metadata = $this->metadataFactory->getClassMetadata($class);
@@ -62,6 +80,9 @@ class Validator implements ValidatorInterface
         return $this->validateGraph($class, $walk, $groups);
         return $this->validateGraph($class, $walk, $groups);
     }
     }
 
 
+    /**
+     * {@inheritDoc}
+     */
     public function validateValue($value, Constraint $constraint, $groups = null)
     public function validateValue($value, Constraint $constraint, $groups = null)
     {
     {
         $walk = function(GraphWalker $walker, $group) use ($constraint, $value) {
         $walk = function(GraphWalker $walker, $group) use ($constraint, $value) {

+ 31 - 0
src/Symfony/Component/Validator/ValidatorInterface.php

@@ -20,11 +20,42 @@ use Symfony\Component\Validator\Constraint;
  */
  */
 interface ValidatorInterface
 interface ValidatorInterface
 {
 {
+    /**
+     * Validate the given object.
+     *
+     * @param object $object The object to validate
+     * @param array|null $groups The validator groups to use for validating
+     * @return ConstraintViolationList
+     */
     function validate($object, $groups = null);
     function validate($object, $groups = null);
 
 
+    /**
+     * Validate a single property of an object against its current value.
+     *
+     * @param object $object The object to validate
+     * @param string $property The name of the property to validate
+     * @param array|null $groups The validator groups to use for validating
+     * @return ConstraintViolationList
+     */
     function validateProperty($object, $property, $groups = null);
     function validateProperty($object, $property, $groups = null);
 
 
+    /**
+     * Validate a single property of an object against the given value.
+     *
+     * @param string $class The class on which the property belongs
+     * @param string $property The name of the property to validate
+     * @param array|null $groups The validator groups to use for validating
+     * @return ConstraintViolationList
+     */
     function validatePropertyValue($class, $property, $value, $groups = null);
     function validatePropertyValue($class, $property, $value, $groups = null);
 
 
+    /**
+     * Validates a given value against a specific Constraint.
+     *
+     * @param mixed $value The value to validate
+     * @param Constraint $constraint The constraint to validate against
+     * @param array|null $groups The validator groups to use for validating
+     * @return ConstraintViolationList
+     */
     function validateValue($value, Constraint $constraint, $groups = null);
     function validateValue($value, Constraint $constraint, $groups = null);
 }
 }