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