Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
18 / 22 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CmsValidationCheck | |
81.82% |
18 / 22 |
|
50.00% |
2 / 4 |
9.49 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromStaticValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| toArray | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
5.01 | |||
| sanitize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Attributes; |
| 3 | |
| 4 | use Apie\Core\RegexUtils; |
| 5 | use Apie\Core\ValueObjects\Utils; |
| 6 | use Attribute; |
| 7 | use ReflectionClass; |
| 8 | use ReflectionProperty; |
| 9 | |
| 10 | #[Attribute(Attribute::IS_REPEATABLE|Attribute::TARGET_CLASS|Attribute::TARGET_METHOD|Attribute::TARGET_PROPERTY|Attribute::TARGET_PARAMETER)] |
| 11 | final class CmsValidationCheck |
| 12 | { |
| 13 | private mixed $exactMatch; |
| 14 | |
| 15 | public function __construct( |
| 16 | public ?string $message = null, |
| 17 | public bool $inverseCheck = false, |
| 18 | public readonly ?string $pattern = null, |
| 19 | public readonly ?string $patternMethod = null, |
| 20 | public readonly ?string $minLengthMethod = null, |
| 21 | public readonly ?string $maxLengthMethod = null, |
| 22 | ) { |
| 23 | } |
| 24 | |
| 25 | public static function createFromStaticValue(string $message, mixed $value): self |
| 26 | { |
| 27 | $res = new self(message: $message); |
| 28 | $res->exactMatch = $value; |
| 29 | return $res; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param ReflectionClass<object> $class |
| 34 | * @return array<string, mixed> |
| 35 | */ |
| 36 | public function toArray(ReflectionClass $class): array |
| 37 | { |
| 38 | $res = [ |
| 39 | 'message' => $this->message, |
| 40 | 'inverseCheck' => $this->inverseCheck, |
| 41 | 'pattern' => $this->pattern, |
| 42 | ]; |
| 43 | if ((new ReflectionProperty($this, 'exactMatch'))->isInitialized($this)) { |
| 44 | $res['exactMatch'] = $this->exactMatch; |
| 45 | } |
| 46 | foreach (get_object_vars($this) as $propertyName => $propertyValue) { |
| 47 | if (str_ends_with($propertyName, 'Method') && is_string($propertyValue)) { |
| 48 | $method = $class->getMethod($propertyValue); |
| 49 | $res[preg_replace('/Method$/', '', $propertyName)] = $this->sanitize( |
| 50 | $propertyName, |
| 51 | $method->invoke(null) |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | return $res; |
| 56 | } |
| 57 | |
| 58 | private function sanitize(string $propertyName, mixed $value): mixed |
| 59 | { |
| 60 | if ($propertyName === 'patternMethod') { |
| 61 | return RegexUtils::removeDelimiters(Utils::toString($value)); |
| 62 | } |
| 63 | return $value; |
| 64 | } |
| 65 | } |