1 | <?php |
||
2 | |||
3 | namespace TheCodingMachine\TDBM\Utils\Annotation; |
||
4 | |||
5 | use TheCodingMachine\TDBM\TDBMException; |
||
6 | |||
7 | use function explode; |
||
8 | use function preg_match; |
||
9 | use function strpos; |
||
10 | |||
11 | abstract class AbstractTraitAnnotation |
||
12 | { |
||
13 | /** |
||
14 | * The PHP trait that is used by the Bean. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | private $name; |
||
19 | |||
20 | /** |
||
21 | * @var array<string, string> |
||
22 | */ |
||
23 | private $insteadOf = []; |
||
24 | |||
25 | /** |
||
26 | * @var array<non-empty-string, non-empty-string> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
27 | */ |
||
28 | private $as = []; |
||
29 | |||
30 | /** |
||
31 | * @param array<string, mixed> $values |
||
32 | * |
||
33 | * @throws \BadMethodCallException |
||
34 | */ |
||
35 | public function __construct(array $values) |
||
36 | { |
||
37 | if (!isset($values['value']) && !isset($values['name'])) { |
||
38 | throw new \BadMethodCallException('The @AddTrait annotation must be passed a trait to use. For instance: \'@AddTrait("Foo\\BarTrait")\''); |
||
39 | } |
||
40 | $this->name = $values['value'] ?? $values['name']; |
||
41 | |||
42 | $modifiers = $values['modifiers'] ?? []; |
||
43 | |||
44 | foreach ($modifiers as $modifier) { |
||
45 | if (preg_match('/(.*?)\s*insteadof\s(.*)/', $modifier, $matches) === 1) { |
||
46 | $this->insteadOf[$matches[1]] = $matches[2]; |
||
47 | } elseif (preg_match('/(.+?)\s*as\s(.+)/', $modifier, $matches) === 1) { |
||
48 | $this->as[$matches[1]] = $matches[2]; |
||
49 | } else { |
||
50 | throw new TDBMException('In annotation @AddTrait, the modifiers parameter must be passed either a "insteadof" or a "as" clause. For instance: \'@AddTrait("Foo\\A", modifiers={"A::foo insteadof B", "A::bar as baz"})\''); |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public function getName(): string |
||
56 | { |
||
57 | return '\\'.ltrim($this->name, '\\'); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * A list of "insteadof" clauses. Key: method name to be use, Value: trait to replace |
||
62 | * |
||
63 | * @return array<string,string> |
||
64 | */ |
||
65 | public function getInsteadOf(): array |
||
66 | { |
||
67 | return $this->insteadOf; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * A list of "as" clauses. Key: method name to be renamed, Value: method name |
||
72 | * |
||
73 | * @return array<non-empty-string,non-empty-string> |
||
0 ignored issues
–
show
|
|||
74 | */ |
||
75 | public function getAs(): array |
||
76 | { |
||
77 | return $this->as; |
||
78 | } |
||
79 | } |
||
80 |