1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cloudstek\SCIM\FilterParser\AST; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Attribute path. |
9
|
|
|
*/ |
10
|
|
|
class AttributePath implements \ArrayAccess, \IteratorAggregate, \Countable, Path |
11
|
|
|
{ |
12
|
|
|
private ?string $schema; |
13
|
|
|
|
14
|
|
|
/** @var string[] */ |
15
|
|
|
private array $names; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Attribute path. |
19
|
|
|
* |
20
|
|
|
* @param string $schema |
21
|
|
|
* @param string[] $names |
22
|
|
|
* |
23
|
|
|
* @throws \InvalidArgumentException On empty array of names. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(?string $schema, array $names) |
26
|
|
|
{ |
27
|
|
|
if (empty($names)) { |
28
|
|
|
throw new \InvalidArgumentException('Attribute path should contain at least one attribute name.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->schema = $schema; |
32
|
|
|
$this->names = $names; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get schema URI. |
37
|
|
|
* |
38
|
|
|
* @return string|null |
39
|
|
|
*/ |
40
|
|
|
public function getSchema(): ?string |
41
|
|
|
{ |
42
|
|
|
return $this->schema; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get names. |
47
|
|
|
* |
48
|
|
|
* @return string[] |
49
|
|
|
*/ |
50
|
|
|
public function getNames(): array |
51
|
|
|
{ |
52
|
|
|
return $this->names; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get attribute names as dotted path notation. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getPath(): string |
61
|
|
|
{ |
62
|
|
|
return implode('.', $this->names); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function offsetExists($offset): bool |
69
|
|
|
{ |
70
|
|
|
if (is_int($offset) === false) { |
71
|
|
|
throw new \InvalidArgumentException('Expected numeric offset.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return isset($this->names[$offset]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function offsetGet($offset): mixed |
81
|
|
|
{ |
82
|
|
|
if (is_int($offset) === false) { |
83
|
|
|
throw new \InvalidArgumentException('Expected numeric offset.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->names[$offset]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function offsetSet($offset, $value): void |
93
|
|
|
{ |
94
|
|
|
throw new \LogicException('Attribute path is read-only.'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
|
|
public function offsetUnset($offset): void |
101
|
|
|
{ |
102
|
|
|
throw new \LogicException('Attribute path is read-only.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function count(): int |
109
|
|
|
{ |
110
|
|
|
return count($this->names); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function getIterator(): \Traversable |
117
|
|
|
{ |
118
|
|
|
return new \ArrayIterator($this->names); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function __toString(): string |
122
|
|
|
{ |
123
|
|
|
return $this->getPath(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|