PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/HTML/QuickForm2/Rule/Length.php

https://github.com/CodeYellowBV/piwik
PHP | 236 lines | 101 code | 11 blank | 124 comment | 42 complexity | 977498e22d978636d6b703a83a64da44 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Rule checking the value's length
  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: Length.php 299480 2010-05-19 06:55:03Z 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. * @version Release: @package_version@
  75. */
  76. class HTML_QuickForm2_Rule_Length extends HTML_QuickForm2_Rule
  77. {
  78. /**
  79. * Validates the owner element
  80. *
  81. * @return bool whether length of the element's value is within allowed range
  82. */
  83. protected function validateOwner()
  84. {
  85. if (0 == ($valueLength = strlen($this->owner->getValue()))) {
  86. return true;
  87. }
  88. $allowedLength = $this->getConfig();
  89. if (is_scalar($allowedLength)) {
  90. return $valueLength == $allowedLength;
  91. } else {
  92. return (empty($allowedLength['min']) || $valueLength >= $allowedLength['min']) &&
  93. (empty($allowedLength['max']) || $valueLength <= $allowedLength['max']);
  94. }
  95. }
  96. protected function getJavascriptCallback()
  97. {
  98. $allowedLength = $this->getConfig();
  99. if (is_scalar($allowedLength)) {
  100. $check = "length == {$allowedLength}";
  101. } else {
  102. $checks = array();
  103. if (!empty($allowedLength['min'])) {
  104. $checks[] = "length >= {$allowedLength['min']}";
  105. }
  106. if (!empty($allowedLength['max'])) {
  107. $checks[] = "length <= {$allowedLength['max']}";
  108. }
  109. $check = implode(' && ', $checks);
  110. }
  111. return "function() { var length = " . $this->owner->getJavascriptValue() .
  112. ".length; if (0 == length) { return true; } else { return {$check}; } }";
  113. }
  114. /**
  115. * Adds the 'min' and 'max' fields from one array to the other
  116. *
  117. * @param array Rule configuration, array with 'min' and 'max' keys
  118. * @param array Additional configuration, fields will be added to
  119. * $length if it doesn't contain such a key already
  120. * @return array
  121. */
  122. protected static function mergeMinMaxLength($length, $config)
  123. {
  124. if (array_key_exists('min', $config) || array_key_exists('max', $config)) {
  125. if (!array_key_exists('min', $length) && array_key_exists('min', $config)) {
  126. $length['min'] = $config['min'];
  127. }
  128. if (!array_key_exists('max', $length) && array_key_exists('max', $config)) {
  129. $length['max'] = $config['max'];
  130. }
  131. } else {
  132. if (!array_key_exists('min', $length)) {
  133. $length['min'] = reset($config);
  134. }
  135. if (!array_key_exists('max', $length)) {
  136. $length['max'] = end($config);
  137. }
  138. }
  139. return $length;
  140. }
  141. /**
  142. * Merges length limits given on rule creation with those given to registerRule()
  143. *
  144. * "Global" length limits may be passed to
  145. * {@link HTML_QuickForm2_Factory::registerRule()} in either of the
  146. * following formats
  147. * - scalar (exact length)
  148. * - array(minlength, maxlength)
  149. * - array(['min' => minlength, ]['max' => maxlength])
  150. *
  151. * "Local" length limits may be passed to the constructor in either of
  152. * the following formats
  153. * - scalar (if global config is unset then it is treated as an exact
  154. * length, if 'min' or 'max' is in global config then it is treated
  155. * as 'max' or 'min', respectively)
  156. * - array(minlength, maxlength)
  157. * - array(['min' => minlength, ]['max' => maxlength])
  158. *
  159. * As usual, global configuration overrides local one.
  160. *
  161. * @param int|array Local length limits
  162. * @param int|array Global length limits, usually provided to {@link HTML_QuickForm2_Factory::registerRule()}
  163. * @return int|array Merged length limits
  164. */
  165. public static function mergeConfig($localConfig, $globalConfig)
  166. {
  167. if (!isset($globalConfig)) {
  168. $length = $localConfig;
  169. } elseif (!is_array($globalConfig)) {
  170. $length = $globalConfig;
  171. } else {
  172. $length = self::mergeMinMaxLength(array(), $globalConfig);
  173. if (isset($localConfig)) {
  174. $length = self::mergeMinMaxLength(
  175. $length, is_array($localConfig)? $localConfig: array($localConfig)
  176. );
  177. }
  178. }
  179. return $length;
  180. }
  181. /**
  182. * Sets the allowed length limits
  183. *
  184. * $config can be either of the following
  185. * - integer (rule checks for exact length)
  186. * - array(minlength, maxlength)
  187. * - array(['min' => minlength, ]['max' => maxlength])
  188. *
  189. * @param int|array Length limits
  190. * @return HTML_QuickForm2_Rule
  191. * @throws HTML_QuickForm2_InvalidArgumentException if bogus length limits
  192. * were provided
  193. */
  194. public function setConfig($config)
  195. {
  196. if (is_array($config)) {
  197. $config = self::mergeMinMaxLength(array(), $config)
  198. + array('min' => 0, 'max' => 0);
  199. }
  200. if (is_array($config) && ($config['min'] < 0 || $config['max'] < 0) ||
  201. !is_array($config) && $config < 0)
  202. {
  203. throw new HTML_QuickForm2_InvalidArgumentException(
  204. 'Length Rule requires limits to be nonnegative, ' .
  205. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  206. );
  207. } elseif (is_array($config) && $config['min'] == 0 && $config['max'] == 0 ||
  208. !is_array($config) && 0 == $config)
  209. {
  210. throw new HTML_QuickForm2_InvalidArgumentException(
  211. 'Length Rule requires at least one non-zero limit, ' .
  212. preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
  213. );
  214. }
  215. if (!empty($config['min']) && !empty($config['max'])) {
  216. if ($config['min'] > $config['max']) {
  217. list($config['min'], $config['max']) = array($config['max'], $config['min']);
  218. } elseif ($config['min'] == $config['max']) {
  219. $config = $config['min'];
  220. }
  221. }
  222. return parent::setConfig($config);
  223. }
  224. }
  225. ?>