Validator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator;
  11. use Symfony\Component\Validator\Mapping\ElementMetadata;
  12. use Symfony\Component\Validator\Mapping\ClassMetadata;
  13. use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
  14. /**
  15. * The default implementation of the ValidatorInterface.
  16. *
  17. * This service can be used to validate objects, properties and raw values
  18. * against constraints.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  22. */
  23. class Validator implements ValidatorInterface
  24. {
  25. protected $metadataFactory;
  26. protected $validatorFactory;
  27. public function __construct(
  28. ClassMetadataFactoryInterface $metadataFactory,
  29. ConstraintValidatorFactoryInterface $validatorFactory
  30. )
  31. {
  32. $this->metadataFactory = $metadataFactory;
  33. $this->validatorFactory = $validatorFactory;
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function validate($object, $groups = null)
  39. {
  40. $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
  41. $walk = function(GraphWalker $walker, $group) use ($metadata, $object) {
  42. return $walker->walkObject($metadata, $object, $group, '');
  43. };
  44. return $this->validateGraph($object, $walk, $groups);
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function validateProperty($object, $property, $groups = null)
  50. {
  51. $metadata = $this->metadataFactory->getClassMetadata(get_class($object));
  52. $walk = function(GraphWalker $walker, $group) use ($metadata, $property, $object) {
  53. return $walker->walkProperty($metadata, $property, $object, $group, '');
  54. };
  55. return $this->validateGraph($object, $walk, $groups);
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function validatePropertyValue($class, $property, $value, $groups = null)
  61. {
  62. $metadata = $this->metadataFactory->getClassMetadata($class);
  63. $walk = function(GraphWalker $walker, $group) use ($metadata, $property, $value) {
  64. return $walker->walkPropertyValue($metadata, $property, $value, $group, '');
  65. };
  66. return $this->validateGraph($class, $walk, $groups);
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function validateValue($value, Constraint $constraint, $groups = null)
  72. {
  73. $walk = function(GraphWalker $walker, $group) use ($constraint, $value) {
  74. return $walker->walkConstraint($constraint, $value, $group, '');
  75. };
  76. return $this->validateGraph($value, $walk, $groups);
  77. }
  78. protected function validateGraph($root, \Closure $walk, $groups = null)
  79. {
  80. $walker = new GraphWalker($root, $this->metadataFactory, $this->validatorFactory);
  81. $groups = $groups ? (array)$groups : array(Constraint::DEFAULT_GROUP);
  82. foreach ($groups as $group) {
  83. $walk($walker, $group);
  84. }
  85. return $walker->getViolations();
  86. }
  87. }