1 <?php
2
3 /**
4 * The MIT License
5 *
6 * Copyright 2014 George Marques <george at georgemarques.com.br>.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 namespace Flikore\Validator;
28
29 /**
30 * Combines two or more validator objects into one. This validates with all
31 * the inserted validators and stops at the first error, return the message
32 * of the validator that went wrong.
33 *
34 * @author George Marques <george at georgemarques.com.br>
35 * @version 0.5.2
36 * @since 0.2
37 * @license http://opensource.org/licenses/MIT MIT
38 * @copyright (c) 2014, George Marques
39 * @package Flikore\Validator
40 */
41 class ValidationCombo extends Validator
42 {
43
44 /**
45 * The error message for this validator.
46 * @var string The error message for this validator.
47 */
48 protected $message = null;
49
50 /**
51 * A collection of all validators.
52 * @var Validator[] A collection of all validators.
53 */
54 protected $validators = array();
55
56 /**
57 * Creates a new Validation Combo. You can pass as many validators as you want.
58 *
59 * @param array|Validator $... The validators to check (list as arguments or in an array).
60 */
61 public function __construct()
62 {
63 $validators = func_get_args();
64
65 if(count($validators) == 1 and is_array($validators[0]))
66 {
67 $validators = $validators[0];
68 }
69
70 foreach ($validators as $arg)
71 {
72 if (!$arg instanceof Validator)
73 {
74 throw new \InvalidArgumentException(Intl\GetText::_d('Flikore.Validator', 'The arguments must be intances of validators'));
75 }
76 array_push($this->validators, $arg);
77 }
78 }
79
80 /**
81 * Adds a new validator to the combo.
82 * @param Validator $validator The validator to add.
83 */
84 public function addValidator(Validator $validator)
85 {
86 $validator->addKeyValue('key', $this->values['key']);
87 array_push($this->validators, $validator);
88 }
89
90 /**
91 * Executes the real validation so it can be reused.
92 * @param mixed $value The value to validate.
93 * @return boolean Whether the value pass the validation.
94 */
95 protected function doValidate($value)
96 {
97 foreach ($this->validators as $rule)
98 {
99 if (!$rule->validate($value))
100 {
101 $this->setErrorMessage($this->message === null ? $rule->getErrorMessage() : $this->message);
102 return false;
103 }
104 }
105 return true;
106 }
107
108 /**
109 * Adds a new key-value pair to be replaced by the templating engine.
110 * This does not check if it's replacing a specific validator value.
111 * @param string $key The key to replace (in the template as "%key%")
112 * @param string $value The value to be inserted instead of the key.
113 */
114 public function addKeyValue($key, $value)
115 {
116 foreach ($this->validators as $v)
117 {
118 $v->addKeyValue($key, $value);
119 }
120 }
121
122 }
123