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    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    Forms
  32. 32:  */
  33. 33: final class NRules extends NObject implements IteratorAggregate
  34. 34: {
  35. 35:     const VALIDATE_PREFIX = 'validate';
  36. 36:  
  37. 37:     /** @var array */
  38. 38:     public static $defaultMessages array(
  39. 39:     );
  40. 40:  
  41. 41:     /** @var array of NRule */
  42. 42:     private $rules array();
  43. 43:  
  44. 44:     /** @var NRules */
  45. 45:     private $parent;
  46. 46:  
  47. 47:     /** @var array */
  48. 48:     private $toggles array();
  49. 49:  
  50. 50:     /** @var IFormControl */
  51. 51:     private $control;
  52. 52:  
  53. 53:  
  54. 54:  
  55. 55:     public function __construct(IFormControl $control)
  56. 56:     {
  57. 57:         $this->control $control;
  58. 58:     }
  59. 59:  
  60. 60:  
  61. 61:  
  62. 62:     /**
  63. 63:      * Adds a validation rule for the current control.
  64. 64:      * @param  mixed      rule type
  65. 65:      * @param  string     message to display for invalid data
  66. 66:      * @param  mixed      optional rule arguments
  67. 67:      * @return NRules      provides a fluent interface
  68. 68:      */
  69. 69:     public function addRule($operation$message NULL$arg NULL)
  70. 70:     {
  71. 71:         $rule new NRule;
  72. 72:         $rule->control $this->control;
  73. 73:         $rule->operation $operation;
  74. 74:         $this->adjustOperation($rule);
  75. 75:         $rule->arg $arg;
  76. 76:         $rule->type NRule::VALIDATOR;
  77. 77:         if ($message === NULL && isset(self::$defaultMessages[$rule->operation])) {
  78. 78:             $rule->message self::$defaultMessages[$rule->operation];
  79. 79:         else {
  80. 80:             $rule->message $message;
  81. 81:         }
  82. 82:  
  83. 83:         if ($this->parent === NULL{
  84. 84:             // notify only direct rules
  85. 85:             $this->control->notifyRule($rule);
  86. 86:         }
  87. 87:         $this->rules[$rule;
  88. 88:         return $this;
  89. 89:     }
  90. 90:  
  91. 91:  
  92. 92:  
  93. 93:     /**
  94. 94:      * Adds a validation condition a returns new branch.
  95. 95:      * @param  mixed      condition type
  96. 96:      * @param  mixed      optional condition arguments
  97. 97:      * @return NRules      new branch
  98. 98:      */
  99. 99:     public function addCondition($operation$arg NULL)
  100. 100:     {
  101. 101:         return $this->addConditionOn($this->control$operation$arg);
  102. 102:     }
  103. 103:  
  104. 104:  
  105. 105:  
  106. 106:     /**
  107. 107:      * Adds a validation condition on specified control a returns new branch.
  108. 108:      * @param  IFormControl form control
  109. 109:      * @param  mixed      condition type
  110. 110:      * @param  mixed      optional condition arguments
  111. 111:      * @return NRules      new branch
  112. 112:      */
  113. 113:     public function addConditionOn(IFormControl $control$operation$arg NULL)
  114. 114:     {
  115. 115:         $rule new NRule;
  116. 116:         $rule->control $control;
  117. 117:         $rule->operation $operation;
  118. 118:         $this->adjustOperation($rule);
  119. 119:         $rule->arg $arg;
  120. 120:         $rule->type NRule::CONDITION;
  121. 121:         $rule->subRules new self($this->control);
  122. 122:         $rule->subRules->parent $this;
  123. 123:  
  124. 124:         $this->rules[$rule;
  125. 125:         return $rule->subRules;
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Adds a else statement.
  132. 132:      * @return NRules      else branch
  133. 133:      */
  134. 134:     public function elseCondition()
  135. 135:     {
  136. 136:         $rule clone end($this->parent->rules);
  137. 137:         $rule->isNegative !$rule->isNegative;
  138. 138:         $rule->subRules new self($this->parent->control);
  139. 139:         $rule->subRules->parent $this->parent;
  140. 140:         $this->parent->rules[$rule;
  141. 141:         return $rule->subRules;
  142. 142:     }
  143. 143:  
  144. 144:  
  145. 145:  
  146. 146:     /**
  147. 147:      * Ends current validation condition.
  148. 148:      * @return NRules      parent branch
  149. 149:      */
  150. 150:     public function endCondition()
  151. 151:     {
  152. 152:         return $this->parent;
  153. 153:     }
  154. 154:  
  155. 155:  
  156. 156:  
  157. 157:     /**
  158. 158:      * Toggles HTML elememnt visibility.
  159. 159:      * @param  string     element id
  160. 160:      * @param  bool       hide element?
  161. 161:      * @return NRules      provides a fluent interface
  162. 162:      */
  163. 163:     public function toggle($id$hide TRUE)
  164. 164:     {
  165. 165:         $this->toggles[$id$hide;
  166. 166:         return $this;
  167. 167:     }
  168. 168:  
  169. 169:  
  170. 170:  
  171. 171:     /**
  172. 172:      * Validates against ruleset.
  173. 173:      * @param  bool    stop before first error?
  174. 174:      * @return bool    is valid?
  175. 175:      */
  176. 176:     public function validate($onlyCheck FALSE)
  177. 177:     {
  178. 178:         $valid TRUE;
  179. 179:         foreach ($this->rules as $rule)
  180. 180:         {
  181. 181:             if ($rule->control->isDisabled()) continue;
  182. 182:  
  183. 183:             $success ($rule->isNegative xor call_user_func($this->getCallback($rule)$rule->control$rule->arg));
  184. 184:  
  185. 185:             if ($rule->type === NRule::CONDITION && $success{
  186. 186:                 $success $rule->subRules->validate($onlyCheck);
  187. 187:                 $valid $valid && $success;
  188. 188:  
  189. 189:             elseif ($rule->type === NRule::VALIDATOR && !$success{
  190. 190:                 if ($onlyCheck{
  191. 191:                     return FALSE;
  192. 192:                 }
  193. 193:                 $rule->control->addError(vsprintf($rule->control->translate($rule->message)(array) $rule->arg));
  194. 194:                 $valid FALSE;
  195. 195:                 if ($rule->breakOnFailure{
  196. 196:                     break;
  197. 197:                 }
  198. 198:             }
  199. 199:         }
  200. 200:         return $valid;
  201. 201:     }
  202. 202:  
  203. 203:  
  204. 204:  
  205. 205:     /**
  206. 206:      * Iterates over ruleset.
  207. 207:      * @return ArrayIterator 
  208. 208:      */
  209. 209:     final public function getIterator()
  210. 210:     {
  211. 211:         return new ArrayIterator($this->rules);
  212. 212:     }
  213. 213:  
  214. 214:  
  215. 215:  
  216. 216:     /**
  217. 217:      * @return array 
  218. 218:      */
  219. 219:     final public function getToggles()
  220. 220:     {
  221. 221:         return $this->toggles;
  222. 222:     }
  223. 223:  
  224. 224:  
  225. 225:  
  226. 226:     /**
  227. 227:      * Process 'operation' string.
  228. 228:      * @param  NRule 
  229. 229:      * @return void 
  230. 230:      */
  231. 231:     private function adjustOperation($rule)
  232. 232:     {
  233. 233:         if (is_string($rule->operation&& ord($rule->operation[0]127{
  234. 234:             $rule->isNegative TRUE;
  235. 235:             $rule->operation = ~$rule->operation;
  236. 236:         }
  237. 237:  
  238. 238:         // check callback
  239. 239:         if (!is_callable($this->getCallback($rule))) {
  240. 240:             $operation is_scalar($rule->operation" '$rule->operation''';
  241. 241:             throw new InvalidArgumentException("Unknown operation$operation for control '{$rule->control->name}'.");
  242. 242:         }
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     private function getCallback($rule)
  248. 248:     {
  249. 249:         $op $rule->operation;
  250. 250:         if (is_string($op&& strncmp($op':'1=== 0{
  251. 251:             return array($rule->control->getClass()self::VALIDATE_PREFIX ltrim($op':'));
  252. 252:  
  253. 253:         else {
  254. 254:             fixCallback($op);
  255. 255:             return $op;
  256. 256:         }
  257. 257:     }
  258. 258: