1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediadevs\Validator; |
4
|
|
|
|
5
|
|
|
use Mediadevs\Validator\Helpers\Arguments; |
6
|
|
|
use Mediadevs\Validator\Factories\FilterFactory; |
7
|
|
|
|
8
|
|
|
class Validator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The validation results will be stored in here. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $results = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Validating all the values and applying all the assigned filters. |
19
|
|
|
* |
20
|
|
|
* @param array $request |
21
|
|
|
* @param array $configuration |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public function validate(array $request, array $configuration = array()): array |
26
|
|
|
{ |
27
|
|
|
$factory = new FilterFactory(); |
28
|
|
|
$arguments = new Arguments(); |
29
|
|
|
|
30
|
|
|
// Parsing through all the fields |
31
|
|
|
foreach ($arguments->getArguments($configuration) as $config) { |
32
|
|
|
// Assigning the variables for the factory |
33
|
|
|
$filter = $config['filter']; |
34
|
|
|
$thresholds = $config['thresholds']; |
35
|
|
|
$field = $config['field']; |
36
|
|
|
|
37
|
|
|
// Making sure $values is an array |
38
|
|
|
$values = is_array($request[$field]) ? $request[$field] : array($request[$field]); |
39
|
|
|
|
40
|
|
|
// Getting the configuration for the filter |
41
|
|
|
$results = $factory->build($filter, $values, $thresholds); |
42
|
|
|
|
43
|
|
|
// Creating a validation config for the current operation |
44
|
|
|
$this->results[] = [ |
45
|
|
|
'valid' => (bool) $results, |
46
|
|
|
'field' => (string) $field, |
47
|
|
|
'filter' => (string) $filter, |
48
|
|
|
'values' => (array) $values, |
49
|
|
|
'thresholds' => (array) $thresholds, |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->results; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|