PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/spekkionu/passworddb
PHP | 242 lines | 102 code | 11 blank | 129 comment | 42 complexity | 40c9ab95a601c05d3ceb6fc9e2acf529 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Rule checking the value's length
  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: Length.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. * Rule checking the value's length
  51. *
  52. * The rule needs an "allowed length" parameter for its work, it can be either
  53. * - a scalar: the value will be valid if it is exactly this long
  54. * - an array: the value will be valid if its length is between the given values
  55. * (inclusive). If one of these evaluates to 0, then length will be compared
  56. * only with the remaining one.
  57. * See {@link mergeConfig()} for description of possible ways to pass
  58. * configuration parameters.
  59. *
  60. * The Rule considers empty fields as valid and doesn't try to compare their
  61. * lengths with provided limits.
  62. *
  63. * For convenience this Rule is also registered with the names 'minlength' and
  64. * 'maxlength' (having, respectively, 'max' and 'min' parameters set to 0):
  65. * <code>
  66. * $password->addRule('minlength', 'The password should be at least 6 characters long', 6);
  67. * $message->addRule('maxlength', 'Your message is too verbose', 1000);
  68. * </code>
  69. *
  70. * @category HTML
  71. * @package HTML_QuickForm2
  72. * @author Alexey Borzov <avb@php.net>
  73. * @author Bertrand Mansion <golgote@mamasam.com>
  74. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  75. * @version Release: 2.0.0
  76. * @link http://pear.php.net/package/HTML_QuickForm2
  77. */
  78. class HTML_QuickForm2_Rule_Length extends HTML_QuickForm2_Rule
  79. {
  80. /**
  81. * Validates the owner element
  82. *
  83. * @return bool whether length of the element's value is within allowed range
  84. */
  85. protected function validateOwner()
  86. {
  87. if (0 == ($valueLength = strlen($this->owner->getValue()))) {
  88. return true;
  89. }
  90. $allowedLength = $this->getConfig();
  91. if (is_scalar($allowedLength)) {
  92. return $valueLength == $allowedLength;
  93. } else {
  94. return (empty($allowedLength['min']) || $valueLength >= $allowedLength['min']) &&
  95. (empty($allowedLength['max']) || $valueLength <= $allowedLength['max']);
  96. }
  97. }
  98. protected function getJavascriptCallback()
  99. {
  100. $allowedLength = $this->getConfig();
  101. if (is_scalar($allowedLength)) {
  102. $check = "length == {$allowedLength}";
  103. } else {
  104. $checks = array();
  105. if (!empty($allowedLength['min'])) {
  106. $checks[] = "length >= {$allowedLength['min']}";
  107. }
  108. if (!empty($allowedLength['max'])) {
  109. $checks[] = "length <= {$allowedLength['max']}";
  110. }
  111. $check = implode(' && ', $checks);
  112. }
  113. return "function() { var length = " . $this->owner->getJavascriptValue() .
  114. ".length; return qf.rules.empty(length) || ({$check}); }";
  115. }
  116. /**
  117. * Adds the 'min' and 'max' fields from one array to the other
  118. *
  119. * @param array $length Rule configuration, array with 'min' and 'max' keys
  120. * @param array $config Additional configuration, fields will be added to
  121. * $length if it doesn't contain such a key already
  122. *
  123. * @return array
  124. */
  125. protected static function mergeMinMaxLength($length, $config)
  126. {
  127. if (array_key_exists('min', $config) || array_key_exists('max', $config)) {
  128. if (!array_key_exists('min', $length) && array_key_exists('min', $config)) {
  129. $length['min'] = $config['min'];
  130. }
  131. if (!array_key_exists('max', $length) && array_key_exists('max', $config)) {
  132. $length['max'] = $config['max'];
  133. }
  134. } else {
  135. if (!array_key_exists('min', $length)) {
  136. $length['min'] = reset($config);
  137. }
  138. if (!array_key_exists('max', $length)) {
  139. $length['max'] = end($config);
  140. }
  141. }
  142. return $length;
  143. }
  144. /**
  145. * Merges length limits given on rule creation with those given to registerRule()
  146. *
  147. * "Global" length limits may be passed to
  148. * {@link HTML_QuickForm2_Factory::registerRule()} in either of the
  149. * following formats
  150. * - scalar (exact length)
  151. * - array(minlength, maxlength)
  152. * - array(['min' => minlength, ]['max' => maxlength])
  153. *
  154. * "Local" length limits may be passed to the constructor in either of
  155. * the following formats
  156. * - scalar (if global config is unset then it is treated as an exact
  157. * length, if 'min' or 'max' is in global config then it is treated
  158. * as 'max' or 'min', respectively)
  159. * - array(minlength, maxlength)
  160. * - array(['min' => minlength, ]['max' => maxlength])
  161. *
  162. * As usual, global configuration overrides local one.
  163. *
  164. * @param int|array $localConfig Local length limits
  165. * @param int|array $globalConfig Global length limits, usually provided to
  166. * {@link HTML_QuickForm2_Factory::registerRule()}
  167. *
  168. * @return int|array Merged length limits
  169. */
  170. public static function mergeConfig($localConfig, $globalConfig)
  171. {
  172. if (!isset($globalConfig)) {
  173. $length = $localConfig;
  174. } elseif (!is_array($globalConfig)) {
  175. $length = $globalConfig;
  176. } else {
  177. $length = self::mergeMinMaxLength(array(), $globalConfig);
  178. if (isset($localConfig)) {
  179. $length = self::mergeMinMaxLength(
  180. $length, is_array($localConfig)? $localConfig: array($localConfig)
  181. );
  182. }
  183. }
  184. return $length;
  185. }
  186. /**
  187. * Sets the allowed length limits
  188. *
  189. * $config can be either of the following
  190. * - integer (rule checks for exact length)
  191. * - array(minlength, maxlength)
  192. * - array(['min' => minlength, ]['max' => maxlength])
  193. *
  194. * @param int|array $config Length limits
  195. *
  196. * @return HTML_QuickForm2_Rule
  197. * @throws HTML_QuickForm2_InvalidArgumentException if bogus length limits
  198. * were provided
  199. */
  200. public function setConfig($config)
  201. {
  202. if (is_array($config)) {
  203. $config = self::mergeMinMaxLength(array(), $config)
  204. + array('min' => 0, 'max' => 0);
  205. }
  206. if (is_array($config) && ($config['min'] < 0 || $config['max'] < 0)
  207. || !is_array($config) && $config < 0
  208. ) {
  209. throw new HTML_QuickForm2_InvalidArgumentException(
  210. 'Length Rule requires limits to be nonnegative, ' .
  211. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  212. );
  213. } elseif (is_array($config) && $config['min'] == 0 && $config['max'] == 0
  214. || !is_array($config) && 0 == $config
  215. ) {
  216. throw new HTML_QuickForm2_InvalidArgumentException(
  217. 'Length Rule requires at least one non-zero limit, ' .
  218. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  219. );
  220. }
  221. if (!empty($config['min']) && !empty($config['max'])) {
  222. if ($config['min'] > $config['max']) {
  223. list($config['min'], $config['max']) = array($config['max'], $config['min']);
  224. } elseif ($config['min'] == $config['max']) {
  225. $config = $config['min'];
  226. }
  227. }
  228. return parent::setConfig($config);
  229. }
  230. }
  231. ?>