/libs/HTML/QuickForm2/Rule/Each.php

https://github.com/CodeYellowBV/piwik · PHP · 137 lines · 40 code · 4 blank · 93 comment · 3 complexity · ae1d806fbcc145a71901c3b1253c8347 MD5 · raw file

  1. <?php
  2. /**
  3. * Validates all elements in a Container using a template Rule
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>,
  10. * Bertrand Mansion <golgote@mamasam.com>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm2
  39. * @author Alexey Borzov <avb@php.net>
  40. * @author Bertrand Mansion <golgote@mamasam.com>
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version SVN: $Id: Each.php 294057 2010-01-26 21:10:28Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Base class for HTML_QuickForm2 rules
  47. */
  48. // require_once 'HTML/QuickForm2/Rule.php';
  49. /**
  50. * Validates all elements in a Container using a template Rule
  51. *
  52. * This Rule needs one configuration parameter for its work: the template Rule
  53. * to use for actual validation. It can be passed either to
  54. * {@link HTML_QuickForm2_Rule::__construct() the Rule constructor} as local
  55. * configuration or to {@link HTML_QuickForm2_Factory::registerRule()} as
  56. * global one. As usual, global configuration overrides local.
  57. *
  58. * The container will be considered valid if all its elements are valid
  59. * according to a template Rule.
  60. *
  61. * <code>
  62. * $group->addRule('each', 'The fields should contain only letters',
  63. * $group->createRule('regex', '/^[a-z]+$/i'));
  64. * </code>
  65. *
  66. * @category HTML
  67. * @package HTML_QuickForm2
  68. * @author Alexey Borzov <avb@php.net>
  69. * @author Bertrand Mansion <golgote@mamasam.com>
  70. * @version Release: @package_version@
  71. */
  72. class HTML_QuickForm2_Rule_Each extends HTML_QuickForm2_Rule
  73. {
  74. /**
  75. * Validates the owner's children using the template Rule
  76. *
  77. * @return bool Whether all children are valid according to a template Rule
  78. */
  79. protected function validateOwner()
  80. {
  81. $rule = clone $this->getConfig();
  82. foreach ($this->owner->getRecursiveIterator(RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
  83. $rule->setOwner($child);
  84. if (!$rule->validateOwner()) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. /**
  91. * Sets the template Rule to use for actual validation
  92. *
  93. * We do not allow using Required rules here, they are able to validate
  94. * containers themselves without the help of Each rule.
  95. *
  96. * @param HTML_QuickForm2_Rule Template Rule
  97. * @return HTML_QuickForm2_Rule
  98. * @throws HTML_QuickForm2_InvalidArgumentException if $config is either not
  99. * an instance of Rule or is an instance of Rule_Required
  100. */
  101. public function setConfig($config)
  102. {
  103. if (!$config instanceof HTML_QuickForm2_Rule) {
  104. throw new HTML_QuickForm2_InvalidArgumentException(
  105. 'Each Rule requires a template Rule to validate with, ' .
  106. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  107. );
  108. } elseif ($config instanceof HTML_QuickForm2_Rule_Required) {
  109. throw new HTML_QuickForm2_InvalidArgumentException(
  110. 'Cannot use "required" Rule as a template'
  111. );
  112. }
  113. return parent::setConfig($config);
  114. }
  115. /**
  116. * Sets the element that will be validated by this rule
  117. *
  118. * @param HTML_QuickForm2_Container Container to validate
  119. * @throws HTML_QuickForm2_InvalidArgumentException if trying to use
  120. * this Rule on something that isn't a Container
  121. */
  122. public function setOwner(HTML_QuickForm2_Node $owner)
  123. {
  124. if (!$owner instanceof HTML_QuickForm2_Container) {
  125. throw new HTML_QuickForm2_InvalidArgumentException(
  126. 'Each Rule can only validate Containers, '.
  127. get_class($owner) . ' given'
  128. );
  129. }
  130. parent::setOwner($owner);
  131. }
  132. }
  133. ?>