PageRenderTime 748ms CodeModel.GetById 36ms RepoModel.GetById 12ms app.codeStats 0ms

/library/Respect/Validation/Rules/Each.php

https://bitbucket.org/imasters/validation
PHP | 118 lines | 65 code | 21 blank | 32 comment | 14 complexity | 846e44bbb31639feb33497bdf2081466 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Respect\Validation\Rules;
  3. use Traversable;
  4. use Respect\Validation\Validatable;
  5. use Respect\Validation\Exceptions\ValidationException;
  6. class Each extends AbstractRule
  7. {
  8. public $itemValidator;
  9. public $keyValidator;
  10. public function __construct(Validatable $itemValidator = null,
  11. Validatable $keyValidator=null)
  12. {
  13. $this->itemValidator = $itemValidator;
  14. $this->keyValidator = $keyValidator;
  15. }
  16. public function assert($input)
  17. {
  18. $exceptions = array();
  19. if (empty($input))
  20. return true;
  21. if (!is_array($input) || $input instanceof Traversable)
  22. throw $this->reportError($input);
  23. foreach ($input as $key => $item)
  24. if (isset($this->itemValidator))
  25. try {
  26. $this->itemValidator->assert($item);
  27. } catch (ValidationException $e) {
  28. $exceptions[] = $e;
  29. } elseif (isset($this->keyValidator))
  30. try {
  31. $this->keyValidator->assert($item);
  32. } catch (ValidationException $e) {
  33. $exceptions[] = $e;
  34. }
  35. if (!empty($exceptions))
  36. throw $this->reportError($input)->setRelated($exceptions);
  37. return true;
  38. }
  39. public function check($input)
  40. {
  41. if (empty($input))
  42. return true;
  43. if (!is_array($input) || $input instanceof Traversable)
  44. throw $this->reportError($input);
  45. foreach ($input as $key => $item)
  46. if (isset($this->itemValidator))
  47. $this->itemValidator->check($item);
  48. elseif (isset($this->keyValidator))
  49. $this->keyValidator->check($key);
  50. return true;
  51. }
  52. public function validate($input)
  53. {
  54. if (empty($input))
  55. return true;
  56. elseif (!is_array($input) || $input instanceof Traversable)
  57. return false;
  58. foreach ($input as $key => $item)
  59. if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
  60. return false;
  61. elseif (isset($this->keyValidator) && !$this->keyValidator->validate($key))
  62. return false;
  63. return true;
  64. }
  65. }
  66. /**
  67. * LICENSE
  68. *
  69. * Copyright (c) 2009-2011, Alexandre Gomes Gaigalas.
  70. * All rights reserved.
  71. *
  72. * Redistribution and use in source and binary forms, with or without modification,
  73. * are permitted provided that the following conditions are met:
  74. *
  75. * * Redistributions of source code must retain the above copyright notice,
  76. * this list of conditions and the following disclaimer.
  77. *
  78. * * Redistributions in binary form must reproduce the above copyright notice,
  79. * this list of conditions and the following disclaimer in the documentation
  80. * and/or other materials provided with the distribution.
  81. *
  82. * * Neither the name of Alexandre Gomes Gaigalas nor the names of its
  83. * contributors may be used to endorse or promote products derived from this
  84. * software without specific prior written permission.
  85. *
  86. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  87. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  88. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  89. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  90. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  91. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  92. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  93. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  94. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  95. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  96. *
  97. */