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 http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Object.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * List of validation & condition rules.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette\Forms
  32. 32:  */
  33. 33: final class Rules extends Object implements IteratorAggregate
  34. 34: {
  35. 35:     /** @ignore internal */
  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:      * Adds a validation condition a returns new branch.
  96. 96:      * @param  mixed      condition type
  97. 97:      * @param  mixed      optional condition arguments
  98. 98:      * @return Rules      new branch
  99. 99:      */
  100. 100:     public function addCondition($operation$arg NULL)
  101. 101:     {
  102. 102:         return $this->addConditionOn($this->control$operation$arg);
  103. 103:     }
  104. 104:  
  105. 105:  
  106. 106:  
  107. 107:     /**
  108. 108:      * Adds a validation condition on specified control a returns new branch.
  109. 109:      * @param  IFormControl form control
  110. 110:      * @param  mixed      condition type
  111. 111:      * @param  mixed      optional condition arguments
  112. 112:      * @return Rules      new branch
  113. 113:      */
  114. 114:     public function addConditionOn(IFormControl $control$operation$arg NULL)
  115. 115:     {
  116. 116:         $rule new Rule;
  117. 117:         $rule->control $control;
  118. 118:         $rule->operation $operation;
  119. 119:         $this->adjustOperation($rule);
  120. 120:         $rule->arg $arg;
  121. 121:         $rule->type Rule::CONDITION;
  122. 122:         $rule->subRules new self($this->control);
  123. 123:         $rule->subRules->parent $this;
  124. 124:  
  125. 125:         $this->rules[$rule;
  126. 126:         return $rule->subRules;
  127. 127:     }
  128. 128:  
  129. 129:  
  130. 130:  
  131. 131:     /**
  132. 132:      * Adds a else statement.
  133. 133:      * @return Rules      else branch
  134. 134:      */
  135. 135:     public function elseCondition()
  136. 136:     {
  137. 137:         $rule clone end($this->parent->rules);
  138. 138:         $rule->isNegative !$rule->isNegative;
  139. 139:         $rule->subRules new self($this->parent->control);
  140. 140:         $rule->subRules->parent $this->parent;
  141. 141:         $this->parent->rules[$rule;
  142. 142:         return $rule->subRules;
  143. 143:     }
  144. 144:  
  145. 145:  
  146. 146:  
  147. 147:     /**
  148. 148:      * Ends current validation condition.
  149. 149:      * @return Rules      parent branch
  150. 150:      */
  151. 151:     public function endCondition()
  152. 152:     {
  153. 153:         return $this->parent;
  154. 154:     }
  155. 155:  
  156. 156:  
  157. 157:  
  158. 158:     /**
  159. 159:      * Toggles HTML elememnt visibility.
  160. 160:      * @param  string     element id
  161. 161:      * @param  bool       hide element?
  162. 162:      * @return Rules      provides a fluent interface
  163. 163:      */
  164. 164:     public function toggle($id$hide TRUE)
  165. 165:     {
  166. 166:         $this->toggles[$id$hide;
  167. 167:         return $this;
  168. 168:     }
  169. 169:  
  170. 170:  
  171. 171:  
  172. 172:     /**
  173. 173:      * Validates against ruleset.
  174. 174:      * @param  bool    stop before first error?
  175. 175:      * @return bool    is valid?
  176. 176:      */
  177. 177:     public function validate($onlyCheck FALSE)
  178. 178:     {
  179. 179:         $valid TRUE;
  180. 180:         foreach ($this->rules as $rule)
  181. 181:         {
  182. 182:             if ($rule->control->isDisabled()) continue;
  183. 183:  
  184. 184:             $success ($rule->isNegative xor call_user_func($this->getCallback($rule)$rule->control$rule->arg));
  185. 185:  
  186. 186:             if ($rule->type === Rule::CONDITION && $success{
  187. 187:                 $success $rule->subRules->validate($onlyCheck);
  188. 188:                 $valid $valid && $success;
  189. 189:  
  190. 190:             elseif ($rule->type === Rule::VALIDATOR && !$success{
  191. 191:                 if ($onlyCheck{
  192. 192:                     return FALSE;
  193. 193:                 }
  194. 194:                 $rule->control->addError(vsprintf($rule->control->translate($rule->message)(array) $rule->arg));
  195. 195:                 $valid FALSE;
  196. 196:                 if ($rule->breakOnFailure{
  197. 197:                     break;
  198. 198:                 }
  199. 199:             }
  200. 200:         }
  201. 201:         return $valid;
  202. 202:     }
  203. 203:  
  204. 204:  
  205. 205:  
  206. 206:     /**
  207. 207:      * Iterates over ruleset.
  208. 208:      * @return ArrayIterator 
  209. 209:      */
  210. 210:     final public function getIterator()
  211. 211:     {
  212. 212:         return new ArrayIterator($this->rules);
  213. 213:     }
  214. 214:  
  215. 215:  
  216. 216:  
  217. 217:     /**
  218. 218:      * @return array 
  219. 219:      */
  220. 220:     final public function getToggles()
  221. 221:     {
  222. 222:         return $this->toggles;
  223. 223:     }
  224. 224:  
  225. 225:  
  226. 226:  
  227. 227:     /**
  228. 228:      * Process 'operation' string.
  229. 229:      * @param  Rule 
  230. 230:      * @return void 
  231. 231:      */
  232. 232:     private function adjustOperation($rule)
  233. 233:     {
  234. 234:         if (is_string($rule->operation&& ord($rule->operation[0]127{
  235. 235:             $rule->isNegative TRUE;
  236. 236:             $rule->operation = ~$rule->operation;
  237. 237:         }
  238. 238:  
  239. 239:         // check callback
  240. 240:         if (!is_callable($this->getCallback($rule))) {
  241. 241:             $operation is_scalar($rule->operation" '$rule->operation''';
  242. 242:             throw new InvalidArgumentException("Unknown operation$operation for control '{$rule->control->name}'.");
  243. 243:         }
  244. 244:     }
  245. 245:  
  246. 246:  
  247. 247:  
  248. 248:     private function getCallback($rule)
  249. 249:     {
  250. 250:         $op $rule->operation;
  251. 251:         if (is_string($op&& strncmp($op':'1=== 0{
  252. 252:             return array($rule->control->getClass()self::VALIDATE_PREFIX ltrim($op':'));
  253. 253:  
  254. 254:         else {
  255. 255:             fixCallback($op);
  256. 256:             return $op;
  257. 257:         }
  258. 258:     }
  259. 259: