Source for file Rules.php

Documentation is available at Rules.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * List of validation & condition rules.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Forms
  33. 33:  */
  34. 34: final class Rules extends Object implements IteratorAggregate
  35. 35: {
  36. 36:     const VALIDATE_PREFIX = 'validate';
  37. 37:  
  38. 38:     /** @var array */
  39. 39:     public static $defaultMessages array(
  40. 40:     );
  41. 41:  
  42. 42:     /** @var array of Rule */
  43. 43:     private $rules array();
  44. 44:  
  45. 45:     /** @var Rules */
  46. 46:     private $parent;
  47. 47:  
  48. 48:     /** @var array */
  49. 49:     private $toggles array();
  50. 50:  
  51. 51:     /** @var IFormControl */
  52. 52:     private $control;
  53. 53:  
  54. 54:  
  55. 55:  
  56. 56:     public function __construct(IFormControl $control)
  57. 57:     {
  58. 58:         $this->control $control;
  59. 59:     }
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Adds a validation rule for the current control.
  65. 65:      * @param  mixed      rule type
  66. 66:      * @param  string     message to display for invalid data
  67. 67:      * @param  mixed      optional rule arguments
  68. 68:      * @return Rules      provides a fluent interface
  69. 69:      */
  70. 70:     public function addRule($operation$message NULL$arg NULL)
  71. 71:     {
  72. 72:         $rule new Rule;
  73. 73:         $rule->control $this->control;
  74. 74:         $rule->operation $operation;
  75. 75:         $this->adjustOperation($rule);
  76. 76:         $rule->arg $arg;
  77. 77:         $rule->type Rule::VALIDATOR;
  78. 78:         if ($message === NULL && isset(self::$defaultMessages[$rule->operation])) {
  79. 79:             $rule->message self::$defaultMessages[$rule->operation];
  80. 80:         else {
  81. 81:             $rule->message $message;
  82. 82:         }
  83. 83:  
  84. 84:         if ($this->parent === NULL{
  85. 85:             // notify only direct rules
  86. 86:             $this->control->notifyRule($rule);
  87. 87:         }
  88. 88:         $this->rules[$rule;
  89. 89:         return $this;
  90. 90:     }
  91. 91:  
  92. 92:  
  93. 93:  
  94. 94:     /**
  95. 95:      * @deprecated
  96. 96:      */
  97. 97:     public function addRuleFor()
  98. 98:     {
  99. 99:         throw new DeprecatedException('Method addRuleFor() is deprecated. Use addConditionOn() & addRule() construction.');
  100. 100:     }
  101. 101:  
  102. 102:  
  103. 103:  
  104. 104:     /**
  105. 105:      * Adds a validation condition a returns new branch.
  106. 106:      * @param  mixed      condition type
  107. 107:      * @param  mixed      optional condition arguments
  108. 108:      * @return Rules      new branch
  109. 109:      */
  110. 110:     public function addCondition($operation$arg NULL)
  111. 111:     {
  112. 112:         return $this->addConditionOn($this->control$operation$arg);
  113. 113:     }
  114. 114:  
  115. 115:  
  116. 116:  
  117. 117:     /**
  118. 118:      * Adds a validation condition on specified control a returns new branch.
  119. 119:      * @param  IFormControl form control
  120. 120:      * @param  mixed      condition type
  121. 121:      * @param  mixed      optional condition arguments
  122. 122:      * @return Rules      new branch
  123. 123:      */
  124. 124:     public function addConditionOn(IFormControl $control$operation$arg NULL)
  125. 125:     {
  126. 126:         $rule new Rule;
  127. 127:         $rule->control $control;
  128. 128:         $rule->operation $operation;
  129. 129:         $this->adjustOperation($rule);
  130. 130:         $rule->arg $arg;
  131. 131:         $rule->type Rule::CONDITION;
  132. 132:         $rule->subRules new self($this->control);
  133. 133:         $rule->subRules->parent $this;
  134. 134:  
  135. 135:         $this->rules[$rule;
  136. 136:         return $rule->subRules;
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /**
  142. 142:      * Adds a else statement.
  143. 143:      * @return Rules      else branch
  144. 144:      */
  145. 145:     public function elseCondition()
  146. 146:     {
  147. 147:         $rule clone end($this->parent->rules);
  148. 148:         $rule->isNegative !$rule->isNegative;
  149. 149:         $rule->subRules new self($this->parent->control);
  150. 150:         $rule->subRules->parent $this->parent;
  151. 151:         $this->parent->rules[$rule;
  152. 152:         return $rule->subRules;
  153. 153:     }
  154. 154:  
  155. 155:  
  156. 156:  
  157. 157:     /**
  158. 158:      * Ends current validation condition.
  159. 159:      * @return Rules      parent branch
  160. 160:      */
  161. 161:     public function endCondition()
  162. 162:     {
  163. 163:         return $this->parent;
  164. 164:     }
  165. 165:  
  166. 166:  
  167. 167:  
  168. 168:     /**
  169. 169:      * Toggles HTML elememnt visibility.
  170. 170:      * @param  string     element id
  171. 171:      * @param  bool       hide element?
  172. 172:      * @return Rules      provides a fluent interface
  173. 173:      */
  174. 174:     public function toggle($id$hide TRUE)
  175. 175:     {
  176. 176:         $this->toggles[$id$hide;
  177. 177:         return $this;
  178. 178:     }
  179. 179:  
  180. 180:  
  181. 181:  
  182. 182:     /**
  183. 183:      * Validates against ruleset.
  184. 184:      * @param  bool    stop before first error?
  185. 185:      * @return bool    is valid?
  186. 186:      */
  187. 187:     public function validate($onlyCheck FALSE)
  188. 188:     {
  189. 189:         $valid TRUE;
  190. 190:         foreach ($this->rules as $rule)
  191. 191:         {
  192. 192:             if ($rule->control->isDisabled()) continue;
  193. 193:  
  194. 194:             $success ($rule->isNegative xor call_user_func($this->getCallback($rule)$rule->control$rule->arg));
  195. 195:  
  196. 196:             if ($rule->type === Rule::CONDITION && $success{
  197. 197:                 $success $rule->subRules->validate($onlyCheck);
  198. 198:                 $valid $valid && $success;
  199. 199:  
  200. 200:             elseif ($rule->type === Rule::VALIDATOR && !$success{
  201. 201:                 if ($onlyCheck{
  202. 202:                     return FALSE;
  203. 203:                 }
  204. 204:                 $rule->control->addError(vsprintf($rule->control->translate($rule->message)(array) $rule->arg));
  205. 205:                 $valid FALSE;
  206. 206:                 if ($rule->breakOnFailure{
  207. 207:                     break;
  208. 208:                 }
  209. 209:             }
  210. 210:         }
  211. 211:         return $valid;
  212. 212:     }
  213. 213:  
  214. 214:  
  215. 215:  
  216. 216:     /**
  217. 217:      * Iterates over ruleset.
  218. 218:      * @return ArrayIterator 
  219. 219:      */
  220. 220:     final public function getIterator()
  221. 221:     {
  222. 222:         return new ArrayIterator($this->rules);
  223. 223:     }
  224. 224:  
  225. 225:  
  226. 226:  
  227. 227:     /**
  228. 228:      * @return array 
  229. 229:      */
  230. 230:     final public function getToggles()
  231. 231:     {
  232. 232:         return $this->toggles;
  233. 233:     }
  234. 234:  
  235. 235:  
  236. 236:  
  237. 237:     /**
  238. 238:      * Process 'operation' string.
  239. 239:      * @param  Rule 
  240. 240:      * @return void 
  241. 241:      */
  242. 242:     private function adjustOperation($rule)
  243. 243:     {
  244. 244:         if (is_string($rule->operation&& ord($rule->operation[0]127{
  245. 245:             $rule->isNegative TRUE;
  246. 246:             $rule->operation = ~$rule->operation;
  247. 247:         }
  248. 248:  
  249. 249:         // check callback
  250. 250:         if (!is_callable($this->getCallback($rule))) {
  251. 251:             $operation is_scalar($rule->operation" '$rule->operation''';
  252. 252:             throw new InvalidArgumentException("Unknown operation$operation for control '{$rule->control->name}'.");
  253. 253:         }
  254. 254:     }
  255. 255:  
  256. 256:  
  257. 257:  
  258. 258:     private function getCallback($rule)
  259. 259:     {
  260. 260:         $op $rule->operation;
  261. 261:         if (is_string($op&& strncmp($op':'1=== 0{
  262. 262:             return array($rule->control->getClass()self::VALIDATE_PREFIX ltrim($op':'));
  263. 263:  
  264. 264:         else {
  265. 265:             fixCallback($op);
  266. 266:             return $op;
  267. 267:         }
  268. 268:     }
  269. 269: