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 use Flikore\Validator\Validator;
30
31 /**
32 * Validates that the value pass in at least one of the given validators.
33 *
34 * @customKey <i>%vN%</i> The message of the Nth validator (1-index).
35 *
36 * @author George Marques <george at georgemarques.com.br>
37 * @version 0.5.2
38 * @since 0.5.0
39 * @license http://opensource.org/licenses/MIT MIT
40 * @copyright (c) 2014, George Marques
41 * @package Flikore\Validator
42 */
43 class ValidationChoice extends Validator
44 {
45
46 /**
47 * An array of validators to test.
48 * @var Validator[] An array of validators to test.
49 */
50 protected $validators = array();
51
52 /**
53 * The error message for this validator.
54 * @var string The error message for this validator.
55 */
56 protected $message = 'The %key% must match one of the validators.';
57
58 /**
59 * Creates a new Validation Choice. You can pass as many validators as you want.
60 *
61 * @param array|Validator $... The validators to check (list as arguments or in an array).
62 */
63 public function __construct()
64 {
65 $validators = func_get_args();
66
67 if(count($validators) == 1 and is_array($validators[0]))
68 {
69 $validators = $validators[0];
70 }
71
72 $i = 1;
73 foreach ($validators as $arg)
74 {
75 if (!$arg instanceof Validator)
76 {
77 throw new \InvalidArgumentException(Intl\GetText::_d('Flikore.Validator', 'The arguments must be intances of validators'));
78 }
79 $this->validators[] = $arg;
80 $this->addKeyValue('v' . $i++, $arg->getErrorMessage());
81 }
82 }
83
84 /**
85 * Adds a new validator to the combo.
86 * @param Validator $validator The validator to add.
87 */
88 public function addValidator(Validator $validator)
89 {
90 foreach ($this->values as $key => $value)
91 {
92 if (preg_match('/^v[0-9]+$/', $key) == 0)
93 {
94 $validator->addKeyValue($key, $value);
95 }
96 }
97
98 array_push($this->validators, $validator);
99 }
100
101 /**
102 * Adds a new key-value pair to be replaced by the templating engine.
103 * This does not check if it's replacing a specific validator value.
104 *
105 * @param string $key The key to replace (in the template as "%key%")
106 * @param string $value The value to be inserted instead of the key.
107 */
108 public function addKeyValue($key, $value)
109 {
110 parent::addKeyValue($key, $value);
111
112 if (preg_match('/^v[0-9]+$/', $key) == 0)
113 {
114 foreach ($this->validators as $v)
115 {
116 $v->addKeyValue($key, $value);
117 }
118 }
119 }
120
121 /**
122 * Applies the template message to a formed one.
123 * @return string The formed message.
124 */
125 protected function applyTemplate()
126 {
127 $i = 1;
128 foreach ($this->validators as $v)
129 {
130 $this->addKeyValue('v' . $i++, $v->getErrorMessage());
131 }
132 return parent::applyTemplate();
133 }
134
135 /**
136 * Executes the real validation so it can be reused.
137 * @param mixed $value The value to validate.
138 * @return boolean Whether the value pass the validation.
139 */
140 protected function doValidate($value)
141 {
142 if(empty($this->validators))
143 {
144 return true;
145 }
146 foreach ($this->validators as $v)
147 {
148 if($v->validate($value))
149 {
150 return true;
151 }
152 }
153 return false;
154 }
155
156 }
157