|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Core; |
|
4
|
|
|
use Closure; |
|
5
|
|
|
|
|
6
|
|
|
class RowValidator |
|
7
|
|
|
{ |
|
8
|
|
|
protected $fieldValidator = []; |
|
9
|
|
|
|
|
10
|
|
|
const REQUIRED="required"; |
|
11
|
|
|
const NUMBER="number"; |
|
12
|
|
|
const REGEX="regex"; |
|
13
|
|
|
const CUSTOM="custom"; |
|
14
|
|
|
|
|
15
|
4 |
|
protected function setProperty($fieldList, $property, $value) |
|
16
|
|
|
{ |
|
17
|
4 |
|
foreach ((array)$fieldList as $field) { |
|
18
|
4 |
|
if (!isset($this->fieldValidator[$field])) { |
|
19
|
4 |
|
$this->fieldValidator[$field] = []; |
|
20
|
|
|
} |
|
21
|
4 |
|
$this->fieldValidator[$field] = [ $property => $value ]; |
|
22
|
|
|
} |
|
23
|
4 |
|
} |
|
24
|
|
|
|
|
25
|
4 |
|
public static function getInstance() |
|
26
|
|
|
{ |
|
27
|
4 |
|
return new RowValidator(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function validate(Row $row) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$errors = []; |
|
33
|
4 |
|
foreach ($this->fieldValidator as $field => $properties) { |
|
34
|
4 |
|
$errors[] = $this->checkRequired($field, $properties, $row->get($field)); |
|
35
|
4 |
|
$errors[] = $this->checkNumber($field, $properties, $row->get($field)); |
|
36
|
4 |
|
$errors[] = $this->checkRegex($field, $properties, $row->get($field)); |
|
37
|
4 |
|
$errors[] = $this->checkCustom($field, $properties, $row->get($field)); |
|
38
|
|
|
} |
|
39
|
|
|
return array_values(array_filter($errors, function($value) { return !empty($value); })); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
protected function checkRequired($field, $properties, $value) |
|
43
|
|
|
{ |
|
44
|
4 |
|
if (isset($properties[self::REQUIRED]) && $properties[self::REQUIRED] && empty($value)) { |
|
45
|
1 |
|
return "$field is required"; |
|
46
|
|
|
} |
|
47
|
4 |
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
protected function checkNumber($field, $properties, $value) |
|
51
|
|
|
{ |
|
52
|
4 |
|
if (isset($properties[self::NUMBER]) && $properties[self::NUMBER] && !is_numeric($value)) { |
|
53
|
1 |
|
return "$field needs to be a number"; |
|
54
|
|
|
} |
|
55
|
4 |
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
protected function checkRegex($field, $properties, $value) |
|
59
|
|
|
{ |
|
60
|
4 |
|
if (isset($properties[self::REGEX]) && !empty($properties[self::REGEX]) && !preg_match($properties[self::REGEX], $value)) { |
|
61
|
1 |
|
return "Regex expression for $field doesn't match"; |
|
62
|
|
|
} |
|
63
|
4 |
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
protected function checkCustom($field, $properties, $value) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
4 |
|
$result = null; |
|
69
|
4 |
|
if (isset($properties[self::CUSTOM]) && $properties[self::CUSTOM] instanceof Closure) { |
|
70
|
1 |
|
$result = $properties[self::CUSTOM]($value); |
|
71
|
|
|
} |
|
72
|
4 |
|
return empty($result) ? null : $result; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function requiredField($field) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->requiredFields([$field]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function requiredFields($fieldList) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->setProperty($fieldList, self::REQUIRED, true); |
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function numericFields($fieldList) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->setProperty($fieldList, self::NUMBER, true); |
|
89
|
1 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function regexValidation($field, $regEx) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->setProperty($field, self::REGEX, $regEx); |
|
95
|
1 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function customValidation($field, $closure) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->setProperty($field, self::CUSTOM, $closure); |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.