ValidationBuilder.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Config\Definition\Builder;
  11. /**
  12. * This class builds validation conditions.
  13. *
  14. * @author Christophe Coevoet <stof@notk.org>
  15. */
  16. class ValidationBuilder
  17. {
  18. protected $node;
  19. public $rules = array();
  20. /**
  21. * Constructor.
  22. *
  23. * @param NodeDefinition $node The related node
  24. */
  25. public function __construct(NodeDefinition $node)
  26. {
  27. $this->node = $node;
  28. }
  29. /**
  30. * Registers a closure to run as normalization or an expression builder to build it if null is provided.
  31. *
  32. * @param \Closure $closure
  33. *
  34. * @return ExprBuilder|$this
  35. */
  36. public function rule(\Closure $closure = null)
  37. {
  38. if (null !== $closure) {
  39. $this->rules[] = $closure;
  40. return $this;
  41. }
  42. return $this->rules[] = new ExprBuilder($this->node);
  43. }
  44. }