AbstractEnvelope.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use Respect\Validation\Exceptions\ValidationException;
  9. use Respect\Validation\Validatable;
  10. /**
  11. * Abstract class that creates an envelope around another rule.
  12. *
  13. * This class is usefull when you want to create rules that use other rules, but
  14. * having an custom message.
  15. *
  16. * @author Henrique Moody <henriquemoody@gmail.com>
  17. */
  18. abstract class AbstractEnvelope extends AbstractRule
  19. {
  20. /**
  21. * @var Validatable
  22. */
  23. private $validatable;
  24. /**
  25. * @var mixed[]
  26. */
  27. private $parameters;
  28. /**
  29. * Initializes the rule.
  30. *
  31. * @param mixed[] $parameters
  32. */
  33. public function __construct(Validatable $validatable, array $parameters = [])
  34. {
  35. $this->validatable = $validatable;
  36. $this->parameters = $parameters;
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function validate($input): bool
  42. {
  43. return $this->validatable->validate($input);
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function reportError($input, array $extraParameters = []): ValidationException
  49. {
  50. return parent::reportError($input, $extraParameters + $this->parameters);
  51. }
  52. }