1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\PhpdocParser\Tag; |
6
|
|
|
|
7
|
|
|
use Jasny\PhpdocParser\TagInterface; |
8
|
|
|
use Jasny\PhpdocParser\PhpdocException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Tag can exist multiple times |
12
|
|
|
*/ |
13
|
|
|
class MultiTag implements TagInterface, ProxyTagInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $key; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var TagInterface |
22
|
|
|
*/ |
23
|
|
|
protected $tag; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $index; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* MultiTag constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $key Notation key |
35
|
|
|
* @param TagInterface $tag Representation of a single tag |
36
|
|
|
* @param string|null $index Item key to index by |
37
|
|
|
*/ |
38
|
8 |
|
public function __construct(string $key, TagInterface $tag, ?string $index = null) |
39
|
|
|
{ |
40
|
8 |
|
$this->key = $key; |
41
|
8 |
|
$this->tag = $tag; |
42
|
8 |
|
$this->index = $index; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the notation key. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
1 |
|
public function getKey() |
52
|
|
|
{ |
53
|
1 |
|
return $this->key; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the tag name |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
4 |
|
public function getName(): string |
62
|
|
|
{ |
63
|
4 |
|
return $this->tag->getName(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the representation of a single tag |
68
|
|
|
* |
69
|
|
|
* @return TagInterface |
70
|
|
|
*/ |
71
|
1 |
|
public function getTag(): TagInterface |
72
|
|
|
{ |
73
|
1 |
|
return $this->tag; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Process an notation. |
78
|
|
|
* |
79
|
|
|
* @param array $notations |
80
|
|
|
* @param string $value |
81
|
|
|
* @return array |
82
|
|
|
* @throws PhpdocException |
83
|
|
|
*/ |
84
|
5 |
|
public function process(array $notations, string $value): array |
85
|
|
|
{ |
86
|
5 |
|
$tagName = $this->tag->getName(); |
87
|
|
|
|
88
|
5 |
|
$tagNotations = $this->tag->process([], $value); |
89
|
|
|
|
90
|
5 |
|
if (count($tagNotations) !== 1) { |
91
|
1 |
|
throw new \LogicException("Unable to parse '@{$tagName}' tag: Multi tags must result in " |
92
|
1 |
|
. "exactly one notation per tag."); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
$this->addNotation($notations, $value, reset($tagNotations)); |
96
|
|
|
|
97
|
2 |
|
return $notations; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add notation. |
102
|
|
|
* |
103
|
|
|
* @param array $notations |
104
|
|
|
* @param string $value |
105
|
|
|
* @param mixed $item |
106
|
|
|
* @return void |
107
|
|
|
* @throws PhpdocException |
108
|
|
|
*/ |
109
|
4 |
|
protected function addNotation(array &$notations, string $value, $item): void |
110
|
|
|
{ |
111
|
4 |
|
if (!isset($this->index)) { |
112
|
1 |
|
$notations[$this->key][] = $item; |
113
|
1 |
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
$tagName = $this->getName(); |
117
|
|
|
|
118
|
3 |
|
if (!is_array($item) || !isset($item[$this->index])) { |
119
|
1 |
|
throw new PhpdocException("Unable to add '@{$tagName} $value' tag: No {$this->index}"); |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
$index = $item[$this->index]; |
123
|
|
|
|
124
|
2 |
|
if (isset($notations[$this->key][$index])) { |
125
|
1 |
|
throw new PhpdocException("Unable to add '@{$tagName} $value' tag: Duplicate {$this->index} " |
126
|
1 |
|
. "'$index'"); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
$notations[$this->key][$index] = $item; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|