-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPurgeOn.php
More file actions
102 lines (89 loc) · 3.88 KB
/
PurgeOn.php
File metadata and controls
102 lines (89 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace Sofascore\PurgatoryBundle\Attribute;
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\PropertyValues;
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\ValuesInterface;
use Sofascore\PurgatoryBundle\Attribute\Target\ForProperties;
use Sofascore\PurgatoryBundle\Attribute\Target\TargetInterface;
use Sofascore\PurgatoryBundle\Exception\LogicException;
use Sofascore\PurgatoryBundle\Listener\Enum\Action;
use Symfony\Component\ExpressionLanguage\Expression;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
final class PurgeOn
{
public readonly ?TargetInterface $target;
/** @var ?non-empty-array<string, ValuesInterface> */
public readonly ?array $routeParams;
public readonly \Closure|Expression|null $if;
/** @var ?non-empty-list<string> */
public readonly ?array $route;
/** @var ?non-empty-list<Action> */
public readonly ?array $actions;
/**
* @param class-string $class
* @param string|non-empty-list<string>|TargetInterface|null $target
* @param ?non-empty-array<string, string|non-empty-list<string>|ValuesInterface> $routeParams
* @param string|non-empty-list<string>|null $route
* @param value-of<Action>|non-empty-list<value-of<Action>|Action>|Action|null $actions
*/
public function __construct(
public readonly string $class,
string|array|TargetInterface|null $target = null,
?array $routeParams = null,
\Closure|string|Expression|null $if = null,
string|array|null $route = null,
string|array|Action|null $actions = null,
) {
$this->target = \is_array($target) || \is_string($target) ? new ForProperties($target) : $target;
$this->routeParams = null !== $routeParams ? self::normalizeRouteParams($routeParams) : null;
$this->if = \is_string($if) ? self::normalizeExpression($if) : $if;
$this->route = \is_string($route) ? [$route] : $route;
$this->actions = null !== $actions ? self::normalizeActions($actions) : null;
}
/**
* @param non-empty-array<string, string|non-empty-list<string>|ValuesInterface> $routeParams
*
* @return non-empty-array<string, ValuesInterface>
*/
private static function normalizeRouteParams(array $routeParams): array
{
/** @var array<string, ValuesInterface> $normalized */
$normalized = [];
foreach ($routeParams as $key => $value) {
$normalized[$key] = self::normalizeValue($value);
}
return $normalized;
}
/**
* @param string|non-empty-list<string>|ValuesInterface $value
*/
public static function normalizeValue(string|array|ValuesInterface $value): ValuesInterface
{
return !$value instanceof ValuesInterface
? new PropertyValues(...(\is_array($value) ? $value : [$value]))
: $value;
}
private static function normalizeExpression(string $if): Expression
{
if (!class_exists(Expression::class)) {
throw new LogicException('You cannot use the "if" attribute because the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
}
return new Expression($if);
}
/**
* @param value-of<Action>|non-empty-list<value-of<Action>|Action>|Action $actions
*
* @return non-empty-list<Action>
*/
private static function normalizeActions(string|array|Action $actions): array
{
if (!\is_array($actions)) {
$actions = [$actions];
}
$normalized = [];
foreach ($actions as $action) {
$normalized[] = $action instanceof Action ? $action : Action::from($action);
}
return $normalized;
}
}