1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Xervice\Validator\Communication\Plugin\FieldHandler; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface; |
8
|
|
|
use Xervice\Core\Plugin\AbstractBusinessPlugin; |
9
|
|
|
|
10
|
|
|
class FieldHandlerPlugin extends AbstractBusinessPlugin implements FieldHandlerPluginInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Xervice\Validator\Business\Dependency\ValidatorTypePluginInterface[] |
14
|
|
|
*/ |
15
|
|
|
protected $validatorTypes; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* FieldHandlerPlugin constructor. |
19
|
|
|
* |
20
|
|
|
* @param \Xervice\Validator\Business\Dependency\ValidatorTypePluginInterface[] $validatorTypes |
21
|
|
|
*/ |
22
|
5 |
|
public function __construct(array $validatorTypes) |
23
|
|
|
{ |
24
|
5 |
|
$this->validatorTypes = $validatorTypes; |
25
|
5 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $data |
29
|
|
|
* @param string $fieldName |
30
|
|
|
* @param string $config |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
5 |
|
public function handleSimpleConfig(array $data, string $fieldName, string $config): array |
35
|
|
|
{ |
36
|
5 |
|
foreach ($this->validatorTypes as $validatorType) { |
37
|
5 |
|
if ($validatorType->isResponsible($config)) { |
38
|
5 |
|
$validatorType->validate($data, $fieldName, $config); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
5 |
|
return $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $data |
47
|
|
|
* @param string $fieldName |
48
|
|
|
* @param array $config |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
4 |
|
public function handleNestedConfig(array $data, string $fieldName, array $config): array |
53
|
|
|
{ |
54
|
4 |
|
foreach ($this->validatorTypes as $validatorType) { |
55
|
4 |
|
if ($validatorType->isResponsible($config)) { |
56
|
4 |
|
$validatorType->validate($data, $fieldName, $config); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
return $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $data |
65
|
|
|
* @param string $fieldName |
66
|
|
|
* @param callable $config |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
3 |
|
public function handleCallableConfig(array $data, string $fieldName, callable $config): array |
71
|
|
|
{ |
72
|
3 |
|
foreach ($this->validatorTypes as $validatorType) { |
73
|
3 |
|
if ($validatorType->isResponsible($config)) { |
74
|
3 |
|
$validatorType->validate($data, $fieldName, $config); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return $data; |
79
|
|
|
} |
80
|
|
|
} |