|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Micro framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Micro\Library\DTO\Preparation\Processor\Property\Assert; |
|
15
|
|
|
|
|
16
|
|
|
use Micro\Library\DTO\ClassDef\ClassDefinition; |
|
17
|
|
|
use Micro\Library\DTO\ClassDef\PropertyDefinition; |
|
18
|
|
|
use Micro\Library\DTO\Preparation\Processor\Property\PropertyProcessorInterface; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractConstraintStrategy implements PropertyProcessorInterface |
|
21
|
|
|
{ |
|
22
|
1 |
|
public function process(PropertyDefinition $propertyDefinition, ClassDefinition $classDefinition, array $propertyData, array $classList): void |
|
23
|
|
|
{ |
|
24
|
1 |
|
$validatorProperty = $this->getValidatorProperty(); |
|
25
|
1 |
|
if (!isset($propertyData[$validatorProperty])) { |
|
26
|
1 |
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
$validatorData = $propertyData[$validatorProperty] ?? []; |
|
30
|
|
|
|
|
31
|
1 |
|
$this->addAttribute($propertyDefinition, $this->generateArguments($validatorData)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
protected function stringToBool(string $boolValue): bool |
|
35
|
|
|
{ |
|
36
|
1 |
|
return 'true' === mb_strtolower($boolValue); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $string |
|
41
|
|
|
* @param string $separator |
|
42
|
|
|
* |
|
43
|
|
|
* @return array<string> |
|
44
|
|
|
*/ |
|
45
|
1 |
|
protected function explodeString(string $string, string $separator = ','): array |
|
46
|
|
|
{ |
|
47
|
1 |
|
if (!$separator) { |
|
48
|
|
|
$separator = ','; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$exploded = explode($separator, $string); |
|
52
|
|
|
|
|
53
|
1 |
|
return array_filter(array_map('trim', $exploded)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array<string|mixed> $arguments |
|
58
|
|
|
*/ |
|
59
|
1 |
|
protected function addAttribute(PropertyDefinition $propertyDefinition, array $arguments): void |
|
60
|
|
|
{ |
|
61
|
1 |
|
$propertyDefinition->addAttribute($this->getAttributeClassName(), $arguments); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array<string|mixed> $config |
|
66
|
|
|
* |
|
67
|
|
|
* @return array<string, mixed> |
|
68
|
|
|
*/ |
|
69
|
1 |
|
protected function generateArguments(array $config): array |
|
70
|
|
|
{ |
|
71
|
1 |
|
return array_filter([ |
|
72
|
1 |
|
'message' => $config['message'] ?? null, |
|
73
|
1 |
|
'groups' => $this->explodeString($config['groups'] ?? 'Default'), |
|
74
|
1 |
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param mixed $value |
|
79
|
|
|
* |
|
80
|
|
|
* @return string|int|bool|float|null |
|
81
|
|
|
*/ |
|
82
|
1 |
|
protected function sanitize(mixed $value): string|int|bool|float|null |
|
83
|
|
|
{ |
|
84
|
1 |
|
if ('string' !== \gettype($value)) { |
|
85
|
1 |
|
return $value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$boolValues = ['true', 'false']; |
|
89
|
1 |
|
$valueLowercase = mb_strtolower($value); |
|
90
|
1 |
|
if (\in_array($valueLowercase, $boolValues)) { |
|
91
|
|
|
return 'true' === $valueLowercase; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
if (is_numeric($value)) { |
|
95
|
1 |
|
$double = (float) $value; |
|
96
|
1 |
|
$int = (int) $value; |
|
97
|
|
|
|
|
98
|
1 |
|
return $int == $double ? $int : $double; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
abstract protected function getValidatorProperty(): string; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return class-string |
|
|
|
|
|
|
108
|
|
|
*/ |
|
109
|
|
|
abstract protected function getAttributeClassName(): string; |
|
110
|
|
|
} |
|
111
|
|
|
|