PageRenderTime 39ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/system/library/PEAR/HTML/QuickForm2/Rule/Each.php

https://bitbucket.org/spekkionu/passworddb
PHP | 161 lines | 55 code | 5 blank | 101 comment | 3 complexity | 8ad6dea3285ddfe0921f809485d71685 MD5 | raw file
Possible License(s): BSD-2-Clause
  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-2012, 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 323363 2012-02-19 15:09:07Z 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. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  71. * @version Release: 2.0.0
  72. * @link http://pear.php.net/package/HTML_QuickForm2
  73. */
  74. class HTML_QuickForm2_Rule_Each extends HTML_QuickForm2_Rule
  75. {
  76. /**
  77. * Validates the owner's children using the template Rule
  78. *
  79. * @return bool Whether all children are valid according to a template Rule
  80. */
  81. protected function validateOwner()
  82. {
  83. $rule = clone $this->getConfig();
  84. foreach ($this->owner->getRecursiveIterator(RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
  85. try {
  86. $rule->setOwner($child);
  87. if (!$rule->validateOwner()) {
  88. return false;
  89. }
  90. } catch (HTML_QuickForm2_InvalidArgumentException $e) {}
  91. }
  92. return true;
  93. }
  94. /**
  95. * Builds the callbacks for the owner's children using the template Rule
  96. *
  97. * @return string Javascript function calling all children's callbacks
  98. */
  99. protected function getJavascriptCallback()
  100. {
  101. $rule = clone $this->getConfig();
  102. $callbacks = array();
  103. foreach ($this->owner->getRecursiveIterator(RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
  104. try {
  105. $rule->setOwner($child);
  106. $callbacks[] = $rule->getJavascriptCallback();
  107. } catch (HTML_QuickForm2_InvalidArgumentException $e) {}
  108. }
  109. return "function () { return qf.rules.each([\n\t\t" . implode(",\n\t\t", $callbacks) . "\n\t]); }";
  110. }
  111. /**
  112. * Sets the template Rule to use for actual validation
  113. *
  114. * We do not allow using Required rules here, they are able to validate
  115. * containers themselves without the help of Each rule.
  116. *
  117. * @param HTML_QuickForm2_Rule $config Template Rule
  118. *
  119. * @return HTML_QuickForm2_Rule
  120. * @throws HTML_QuickForm2_InvalidArgumentException if $config is either not
  121. * an instance of Rule or is an instance of Rule_Required
  122. */
  123. public function setConfig($config)
  124. {
  125. if (!$config instanceof HTML_QuickForm2_Rule) {
  126. throw new HTML_QuickForm2_InvalidArgumentException(
  127. 'Each Rule requires a template Rule to validate with, ' .
  128. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  129. );
  130. } elseif ($config instanceof HTML_QuickForm2_Rule_Required) {
  131. throw new HTML_QuickForm2_InvalidArgumentException(
  132. 'Cannot use "required" Rule as a template'
  133. );
  134. }
  135. return parent::setConfig($config);
  136. }
  137. /**
  138. * Sets the element that will be validated by this rule
  139. *
  140. * @param HTML_QuickForm2_Node $owner Container to validate
  141. *
  142. * @throws HTML_QuickForm2_InvalidArgumentException if trying to use
  143. * this Rule on something that isn't a Container
  144. */
  145. public function setOwner(HTML_QuickForm2_Node $owner)
  146. {
  147. if (!$owner instanceof HTML_QuickForm2_Container) {
  148. throw new HTML_QuickForm2_InvalidArgumentException(
  149. 'Each Rule can only validate Containers, '.
  150. get_class($owner) . ' given'
  151. );
  152. }
  153. parent::setOwner($owner);
  154. }
  155. }
  156. ?>