Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 67 |
| Validator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
2070.00 | |
0.00% |
0 / 67 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| cleanUp | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 12 |
|||
| validate | |
0.00% |
0 / 1 |
992.00 | |
0.00% |
0 / 41 |
|||
| validateNumber | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 4 |
|||
| validateIn | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 6 |
|||
| addErrorMessage | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| <?php | |
| namespace ia\Lib; | |
| use ia\Date\YearMonth; | |
| use ia\Util\Str; | |
| class Validator { | |
| public $errorMsg = []; | |
| private $rules; | |
| /** | |
| * Validator constructor. | |
| * @param array $rules = [ | |
| * 'fieldName'=>[ | |
| * 'fieldNameDisplay' => true/not exists nice label, false fieldName, string label to use | |
| * 'nostrim' => true/false not exists false strim done | |
| * 'required' => true/false not exists: false | |
| * 'NOT_NULL' => true/false not exists: true | |
| * 'numeric' => true/false not exists: false | |
| * 'type' => date, dateTime,... | |
| * 'specialType' => yearMonth, email, url, localurl | |
| * 'min' | |
| * 'max' | |
| * 'min_length' | |
| * 'max_length' | |
| * 'in' => [value1, value2, ...] //caseInsensitive values are not objects | |
| * 'exists' => ['table','column',[extraWhere]] //caseInsensitive | |
| * 'unique' => ['table',['pkFieldName1','pkFieldName2', ...] //caseInsensitive | |
| * 'callable' => ['functionName', [extraParams] ] | |
| * ], ... | |
| * ] | |
| */ | |
| public function __construct(array $rules) { | |
| $this->rules = $rules; | |
| } | |
| public function cleanUp(array $values) { | |
| foreach($this->rules as $fieldName => $rule) { | |
| if(!array_key_exists($fieldName, $values)) { | |
| continue; | |
| } | |
| $value = $values[$fieldName]; | |
| if($value === null) { | |
| continue; | |
| } | |
| if(!empty($rule['nostrim'])) { | |
| $value = $values[$fieldName] = Str::strim($value); | |
| } | |
| if(!empty($rule['numeric'])) { | |
| $values[$fieldName] = str_replace(['$', ','], '', $value); | |
| continue; | |
| } | |
| } | |
| return $values; | |
| } | |
| /** | |
| * @param array $values ['fieldName'=>value, ... ], preforms strim, | |
| * @return bool true ok, false has errors in ->errorMsg | |
| * | |
| */ | |
| public function validate($values) { | |
| $this->errorMsg = []; | |
| foreach($this->rules as $fieldName => $rule) { | |
| if(!array_key_exists($fieldName, $values)) { | |
| if(!empty($rule['required'])) { | |
| $this->addErrorMessage($fieldName, '', "Required", $rule); | |
| } | |
| continue; | |
| } | |
| $value = $values[$fieldName]; | |
| if(($value === '' || $value === null) && !empty($rule['required'])) { | |
| $this->addErrorMessage($fieldName, $value, "Required", $rule); | |
| continue; | |
| } | |
| if($value === null ) { | |
| if(!empty($rule['NOT_NULL'])) { | |
| $this->addErrorMessage($fieldName, $value, "Not Null", $rule); | |
| } | |
| continue; | |
| } | |
| if(!empty($rule['in']) && $this->validateIn($fieldName, $value, $rule) === false ) { | |
| continue; | |
| } | |
| if(!empty($rule['numeric']) && $this->validateNumber($fieldName, $value, $rule) === false ) { | |
| continue; | |
| } | |
| //dateTime | |
| if( | |
| (isset($rule['min']) && $value < $rule['min']) || | |
| (isset($rule['max']) && $value > $rule['max']) | |
| ) { | |
| $validRange = 'Valid values: ' . isset($rule['min_length']) ? 'de ' . $rule['min_length']. ' ' : ''; | |
| if(isset($rule['max'])) { | |
| $validRange = 'hasta ' . $rule['max']. ' '; | |
| } | |
| $this->addErrorMessage($fieldName, $value, $validRange, $rule); | |
| continue; | |
| } | |
| if( | |
| (isset($rule['min_length']) && mb_strlen($value) < $rule['min_length']) || | |
| (isset($rule['max_length']) && mb_strlen($value) > $rule['max_length']) | |
| ) { | |
| $validLength = isset($rule['min_length']) ? 'de ' . $rule['min_length']. ' ' : ''; | |
| if(isset($rule['max_length'])) { | |
| $validLength = 'hasta ' . $rule['max_length']. ' '; | |
| } | |
| $this->addErrorMessage($fieldName, $value, "Debe ser ", $rule, $validLength . 'caracteres'); | |
| continue; | |
| } | |
| //specialType: url, localurl, tel, hastag, ... | |
| if(isset($rule['specialType']) && $rule['specialType'] === 'email' && | |
| filter_var($value, FILTER_VALIDATE_EMAIL) === false ) { | |
| $this->addErrorMessage($fieldName, $value, "Invalid email", $rule); | |
| continue; | |
| } | |
| if(isset($rule['specialType']) && $rule['specialType'] === 'yearMonth' && | |
| !YearMonth::valid($value) ) { | |
| $this->addErrorMessage($fieldName, $value, "Invalid year month", $rule); | |
| continue; | |
| } | |
| //callable | |
| } | |
| return empty($this->errorMsg); | |
| } | |
| private function validateNumber(string $fieldName, $value, array $rule) { | |
| if(!is_numeric($value) || $value === '') { | |
| $this->addErrorMessage($fieldName, $value, 'Invalid number', $rule); | |
| return false; | |
| } | |
| return true; | |
| } | |
| private function validateIn(string $fieldName, $value, array $rule) { | |
| if(iaArray::in_array_case_insensitive($value, $rule['in'])) { | |
| return true; | |
| } | |
| $this->addErrorMessage($fieldName, $value, | |
| count($rule['in']) > 7 ? 'Invalid value' : 'Valid values: ' . iaPalabra::comaY($rule['in'], 'รณ'), | |
| $rule); | |
| return false; | |
| } | |
| private function addErrorMessage(string $fieldName, $value, string $message, array $rule, $vsValue = null) { | |
| $this->errorMsg[$fieldName] = $message; | |
| } | |
| /* substutions: | |
| %label | |
| %value | |
| %validList value must be one of, this is the list of valid valuse | |
| %table_name must be in Products | |
| %fromValue | |
| %uptoValue | |
| */ | |
| /* translator sira | |
| return [ | |
| 'required' => '', | |
| 'not null' => '', | |
| 'must agree' => '', | |
| 'must be checked' => '', | |
| 'inactive value' => '', | |
| 'value must be in table' => '', | |
| 'value must be one of' => '', | |
| 'value must be one, or more, of' => '', | |
| 'valid values from upto' => '', | |
| 'valid values from' => '', | |
| 'valid values upto' => '', | |
| 'length from upto' => '', | |
| 'length from' => '', | |
| 'length upto' => '', | |
| 'Invalid email' => '', | |
| 'Invalid number' => '', | |
| 'Invalid year month' => '', | |
| 'Invalid date' => '', | |
| 'Invalid datetime' => '', | |
| ]; | |
| */ | |
| } |