1 | <?php |
||
2 | /** |
||
3 | * KumbiaPHP web & app Framework |
||
4 | * |
||
5 | * LICENSE |
||
6 | * |
||
7 | * This source file is subject to the new BSD license that is bundled |
||
8 | * with this package in the file LICENSE. |
||
9 | * |
||
10 | * @category Kumbia |
||
11 | * @package Validate |
||
12 | * |
||
13 | * @copyright Copyright (c) 2005 - 2023 KumbiaPHP Team (http://www.kumbiaphp.com) |
||
14 | * @license https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE New BSD License |
||
15 | */ |
||
16 | |||
17 | /** |
||
18 | * Validate es una Clase que realiza validaciones Lógicas |
||
19 | * |
||
20 | * @category KumbiaPHP |
||
21 | * @package validate |
||
22 | */ |
||
23 | require __DIR__.'/validations.php'; |
||
24 | class Validate |
||
25 | { |
||
26 | /** |
||
27 | * Objeto a validar |
||
28 | * @var Object |
||
29 | */ |
||
30 | protected $obj = null; |
||
31 | |||
32 | /** |
||
33 | * Mensajes de error almacenados |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $messages = array(); |
||
37 | |||
38 | /** |
||
39 | * Reglas a a seguir para la validación |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $rules = array(); |
||
43 | /** |
||
44 | * Contructor |
||
45 | * @param Object $obj Objeto a validar |
||
46 | */ |
||
47 | |||
48 | /** |
||
49 | * Almacena si la variable a validar es un objeto antes de convertirlo |
||
50 | * @var boolean |
||
51 | */ |
||
52 | protected $is_obj = false; |
||
53 | |||
54 | /** |
||
55 | * El parametro $rules debe contener esta forma |
||
56 | * array( |
||
57 | * 'user' => //este es el nombre del campo |
||
58 | * array( |
||
59 | * 'alpha' => //nombre del filtro |
||
60 | * null, //parametros pasados (en array o null si no se requiere) |
||
61 | * 'lenght' => array('min'=>4, 'max'=>10) |
||
62 | * ) |
||
63 | * ) |
||
64 | * @param mixed $obj Objecto o Array a validar |
||
65 | * @param array $rules Aray de reglas a validar |
||
66 | */ |
||
67 | public function __construct($obj, Array $rules){ |
||
68 | $this->is_obj = is_object($obj); |
||
69 | $this->obj = (object)$obj; |
||
70 | $this->rules = $rules; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Ejecuta las validaciones |
||
75 | * @return bool Devuelve true si todo es válido |
||
76 | */ |
||
77 | public function exec(){ |
||
78 | /*Recorrido por todos los campos*/ |
||
79 | foreach ($this->rules as $field => $fRule){ |
||
80 | $value = self::getValue($this->obj, $field); |
||
81 | /*Regla individual para cada campo*/ |
||
82 | foreach ($fRule as $ruleName => $param) { |
||
83 | $ruleName = self::getRuleName($ruleName, $param); |
||
84 | $param = self::getParams($param); |
||
85 | /*Ignore the rule is starts with "#"*/ |
||
86 | if($ruleName[0] == '#') continue; |
||
87 | /*Es una validación de modelo*/ |
||
88 | if($ruleName[0] == '@'){ |
||
89 | $this->modelRule($ruleName, $param, $field); |
||
90 | }elseif(!Validations::$ruleName($value, $param, $this->obj)){ |
||
91 | $this->addError($param, $field, $ruleName); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | /*Si no hay errores devuelve true*/ |
||
96 | return empty($this->messages); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Ejecuta una validación de modelo |
||
101 | * @param string $rule nombre de la regla |
||
102 | * @param array $param |
||
103 | * @param string $field Nombre del campo |
||
104 | * @return bool |
||
105 | */ |
||
106 | protected function modelRule($rule, $param, $field){ |
||
107 | if(!$this->is_obj){ |
||
108 | trigger_error('No se puede ejecutar una validacion de modelo en un array', E_USER_WARNING); |
||
109 | return false; |
||
110 | } |
||
111 | $ruleName = ltrim($rule, '@'); |
||
112 | $obj = $this->obj; |
||
113 | if(!method_exists($obj, $ruleName)){ |
||
114 | trigger_error('El metodo para la validacion no existe', E_USER_WARNING); |
||
115 | return false; |
||
116 | } |
||
117 | if(!$obj->$ruleName($field, $param)){ |
||
118 | $this->addError($param, $field, $ruleName); |
||
119 | } |
||
120 | return true; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Agrega un nuevo error |
||
125 | * @param Array $param parametros |
||
126 | * @param string $field Nombre del campo |
||
127 | * @param string $rule Nombre de la regla |
||
128 | */ |
||
129 | protected function addError(Array $param, $field, $rule){ |
||
130 | $this->messages[$field][] = $param['error'] ?? Validations::getMessage($rule); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Devuelve el nombre de la regla |
||
135 | * @param string $ruleName |
||
136 | * @param mixed $param |
||
137 | * @return string |
||
138 | */ |
||
139 | protected static function getRuleName($ruleName, $param){ |
||
140 | /*Evita tener que colocar un null cuando no se pasan parametros*/ |
||
141 | return is_integer($ruleName) && is_string($param)?$param:$ruleName; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Devuelve los parametros para la regla |
||
146 | * @param mixed $param |
||
147 | * @return array |
||
148 | */ |
||
149 | protected static function getParams($param){ |
||
150 | return is_array($param)?$param:array(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Devuelve el valor de un campo |
||
155 | * @param object $obj |
||
156 | * @param string $field |
||
157 | * @return mixed |
||
158 | */ |
||
159 | protected static function getValue($obj, $field){ |
||
160 | return !empty($obj->$field)?$obj->$field:null;//obtengo el valor del campo |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Devuelve los mensajes de error |
||
165 | * |
||
166 | */ |
||
167 | public function getMessages(){ |
||
168 | return $this->messages; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Version de instancias para flush error |
||
173 | */ |
||
174 | public function flash(){ |
||
175 | self::errorToFlash($this->getMessages()); |
||
176 | } |
||
177 | |||
178 | public static function fail($obj, Array $rules){ |
||
179 | $val = new self($obj, $rules); |
||
180 | return $val->exec() ? false:$val->getMessages(); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Envia los mensajes de error via flash |
||
185 | * @param Array $error |
||
186 | */ |
||
187 | public static function errorToFlash(Array $error){ |
||
188 | foreach ($error as $value) |
||
189 | Flash::error($value); |
||
190 | } |
||
191 | } |
||
192 |