-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotation.php
More file actions
90 lines (80 loc) · 2.43 KB
/
Annotation.php
File metadata and controls
90 lines (80 loc) · 2.43 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
<?php
declare(strict_types=1);
namespace OpenClassrooms\ServiceProxy\Annotation;
abstract class Annotation
{
/**
* @var array<string>|null
*/
protected array|string|null $handler = null;
/**
* @param array<string, mixed> $data Key-value for properties to be defined in this class.
*/
public function __construct(array $data = [])
{
foreach ($data as $key => $value) {
if (method_exists($this, 'set' . ucfirst($key))) {
$setter = 'set' . ucfirst($key);
// @phpstan-ignore-next-line
$this->{$setter}($value);
} else {
// @phpstan-ignore-next-line
$this->{$key} = $value;
}
}
}
/**
* @throws \BadMethodCallException
*/
final public function __get(string $name): void
{
throw new \BadMethodCallException(
\sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
);
}
/**
* @throws \BadMethodCallException
*/
final public function __isset(string $name): bool
{
throw new \BadMethodCallException(
\sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
);
}
/**
* @throws \BadMethodCallException
*/
final public function __set(string $name, mixed $value): void
{
throw new \BadMethodCallException(
\sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class)
);
}
/**
* @return array<string>
*/
final public function getHandlers(): array
{
return (array) ($this->handler ?? []);
}
/**
* @param array<string, string>|string|null $handlers
* @param array<string, array<string>|string|null> $aliases
*/
final protected function setHandlers(array|string|null $handlers = null, array $aliases = []): void
{
if (\count($aliases) > 0) {
$values = array_values($aliases);
$keys = array_keys($aliases);
if ($values[0] !== null && $values[1] !== null) {
throw new \RuntimeException(
"Argument '{$keys[1]}' is an alias for '{$keys[0]}'.
You can only define one of the two arguments."
);
}
$this->handler = $values[0] ?? $values[1];
} else {
$this->handler = $handlers;
}
}
}