1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: App::uses('Validation', 'Utility');
24:
25: 26: 27: 28: 29: 30: 31:
32: class CakeValidationRule {
33:
34: 35: 36: 37: 38:
39: protected $_valid = true;
40:
41: 42: 43: 44: 45:
46: protected $_recordExists = false;
47:
48: 49: 50: 51: 52:
53: protected $_rule = null;
54:
55: 56: 57: 58: 59:
60: protected $_ruleParams = array();
61:
62: 63: 64: 65: 66:
67: protected $_passedOptions = array();
68:
69: 70: 71: 72: 73:
74: public $rule = 'blank';
75:
76: 77: 78: 79: 80:
81: public $required = null;
82:
83: 84: 85: 86: 87:
88: public $allowEmpty = null;
89:
90: 91: 92: 93: 94:
95: public $on = null;
96:
97: 98: 99: 100: 101:
102: public $last = true;
103:
104: 105: 106: 107: 108:
109: public $message = null;
110:
111: 112: 113: 114: 115:
116: public function __construct($validator = array()) {
117: $this->_addValidatorProps($validator);
118: }
119:
120: 121: 122: 123: 124:
125: public function isValid() {
126: if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
127: return false;
128: }
129:
130: return true;
131: }
132:
133: 134: 135: 136: 137:
138: public function isEmptyAllowed() {
139: return $this->skip() || $this->allowEmpty === true;
140: }
141:
142: 143: 144: 145: 146:
147: public function isRequired() {
148: if (in_array($this->required, array('create', 'update'), true)) {
149: if ($this->required === 'create' && !$this->isUpdate() || $this->required === 'update' && $this->isUpdate()) {
150: return true;
151: }
152: return false;
153: }
154:
155: return $this->required;
156: }
157:
158: 159: 160: 161: 162: 163: 164:
165: public function checkRequired($field, &$data) {
166: return (
167: (!array_key_exists($field, $data) && $this->isRequired() === true) ||
168: (
169: array_key_exists($field, $data) && (empty($data[$field]) &&
170: !is_numeric($data[$field])) && $this->allowEmpty === false
171: )
172: );
173: }
174:
175: 176: 177: 178: 179: 180: 181:
182: public function checkEmpty($field, &$data) {
183: if (empty($data[$field]) && $data[$field] != '0' && $this->allowEmpty === true) {
184: return true;
185: }
186: return false;
187: }
188:
189: 190: 191: 192: 193:
194: public function skip() {
195: if (!empty($this->on)) {
196: if ($this->on === 'create' && $this->isUpdate() || $this->on === 'update' && !$this->isUpdate()) {
197: return true;
198: }
199: }
200: return false;
201: }
202:
203: 204: 205: 206: 207: 208:
209: public function isLast() {
210: return (bool)$this->last;
211: }
212:
213: 214: 215: 216: 217:
218: public function getValidationResult() {
219: return $this->_valid;
220: }
221:
222: 223: 224: 225: 226:
227: protected function _getPropertiesArray() {
228: $rule = $this->rule;
229: if (!is_string($rule)) {
230: unset($rule[0]);
231: }
232: return array(
233: 'rule' => $rule,
234: 'required' => $this->required,
235: 'allowEmpty' => $this->allowEmpty,
236: 'on' => $this->on,
237: 'last' => $this->last,
238: 'message' => $this->message
239: );
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252:
253: public function isUpdate($exists = null) {
254: if ($exists === null) {
255: return $this->_recordExists;
256: }
257: return $this->_recordExists = $exists;
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267:
268: public function process($field, &$data, &$methods) {
269: $this->_valid = true;
270: $this->_parseRule($field, $data);
271:
272: $validator = $this->_getPropertiesArray();
273: $rule = strtolower($this->_rule);
274: if (isset($methods[$rule])) {
275: $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
276: $this->_ruleParams[0] = array($field => $this->_ruleParams[0]);
277: $this->_valid = call_user_func_array($methods[$rule], $this->_ruleParams);
278: } elseif (class_exists('Validation') && method_exists('Validation', $this->_rule)) {
279: $this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
280: } elseif (is_string($validator['rule'])) {
281: $this->_valid = preg_match($this->_rule, $data[$field]);
282: } else {
283: trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $field), E_USER_WARNING);
284: return false;
285: }
286:
287: return true;
288: }
289:
290: 291: 292: 293: 294: 295:
296: public function reset() {
297: $this->_valid = true;
298: $this->_recordExists = false;
299: }
300:
301: 302: 303: 304: 305: 306:
307: public function getOptions($key) {
308: if (!isset($this->_passedOptions[$key])) {
309: return null;
310: }
311: return $this->_passedOptions[$key];
312: }
313:
314: 315: 316: 317: 318: 319:
320: protected function _addValidatorProps($validator = array()) {
321: if (!is_array($validator)) {
322: $validator = array('rule' => $validator);
323: }
324: foreach ($validator as $key => $value) {
325: if (isset($value) || !empty($value)) {
326: if (in_array($key, array('rule', 'required', 'allowEmpty', 'on', 'message', 'last'))) {
327: $this->{$key} = $validator[$key];
328: } else {
329: $this->_passedOptions[$key] = $value;
330: }
331: }
332: }
333: }
334:
335: 336: 337: 338: 339: 340: 341:
342: protected function _parseRule($field, &$data) {
343: if (is_array($this->rule)) {
344: $this->_rule = $this->rule[0];
345: $this->_ruleParams = array_merge(array($data[$field]), array_values(array_slice($this->rule, 1)));
346: } else {
347: $this->_rule = $this->rule;
348: $this->_ruleParams = array($data[$field]);
349: }
350: }
351:
352: }
353: