Skip to content

Commit ce13570

Browse files
committed
Update internal codebase for Ordered Map
1 parent bc2ee40 commit ce13570

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/Dictionary.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public function has(string $key): bool
140140

141141
public function get(string $key): Item|InnerList
142142
{
143+
self::validateKey($key);
144+
143145
if (!array_key_exists($key, $this->members)) {
144146
throw InvalidOffset::dueToKeyNotFound($key);
145147
}

src/OrderedList.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public static function fromMembers(iterable $members = []): self
4747
return new self(...$newMembers);
4848
}
4949

50+
private static function filterMember(InnerList|Item|ByteSequence|Token|bool|int|float|string $member): InnerList|Item
51+
{
52+
return match (true) {
53+
$member instanceof InnerList, $member instanceof Item => $member,
54+
default => Item::from($member),
55+
};
56+
}
57+
5058
/**
5159
* Returns an instance from an HTTP textual representation.
5260
*
@@ -116,14 +124,6 @@ public function unshift(InnerList|Item|ByteSequence|Token|bool|int|float|string
116124
$this->members = [...array_map(self::filterMember(...), $members), ...$this->members];
117125
}
118126

119-
private static function filterMember(InnerList|Item|ByteSequence|Token|bool|int|float|string $member): InnerList|Item
120-
{
121-
return match (true) {
122-
$member instanceof InnerList, $member instanceof Item => $member,
123-
default => Item::from($member),
124-
};
125-
}
126-
127127
/**
128128
* Insert members at the end of the list.
129129
*/
@@ -157,25 +157,30 @@ public function insert(
157157
*/
158158
public function replace(int $index, InnerList|Item|ByteSequence|Token|bool|int|float|string $member): void
159159
{
160-
if (!$this->has($index)) {
160+
if (null === ($offset = $this->filterIndex($index))) {
161161
throw InvalidOffset::dueToIndexNotFound($index);
162162
}
163163

164-
$this->members[$this->filterIndex($index)] = self::filterMember($member);
164+
$this->members[$offset] = self::filterMember($member);
165165
}
166166

167167
/**
168168
* Delete members associated with the list of instance indexes.
169169
*/
170170
public function remove(int ...$indexes): void
171171
{
172-
foreach (array_map(fn (int $index): int|null => $this->filterIndex($index), $indexes) as $index) {
173-
if (null !== $index) {
174-
unset($this->members[$index]);
175-
}
172+
$offsets = array_filter(
173+
array_map(fn (int $index): int|null => $this->filterIndex($index), $indexes),
174+
fn (int|null $index): bool => null !== $index
175+
);
176+
177+
foreach ($offsets as $offset) {
178+
unset($this->members[$offset]);
176179
}
177180

178-
$this->members = array_values($this->members);
181+
if ([] !== $offsets) {
182+
$this->members = array_values($this->members);
183+
}
179184
}
180185

181186
/**

src/Parameters.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public function has(string $key): bool
167167

168168
public function get(string $key): Item
169169
{
170+
self::validateKey($key);
171+
170172
if (!array_key_exists($key, $this->members)) {
171173
throw InvalidOffset::dueToKeyNotFound($key);
172174
}
@@ -225,11 +227,16 @@ private static function filterMember(Item|ByteSequence|Token|bool|int|float|stri
225227
};
226228
}
227229

228-
private static function validate(string $key, Item $item): void
230+
private static function validateKey(string $key): void
229231
{
230232
if (1 !== preg_match('/^[a-z*][a-z0-9.*_-]*$/', $key)) {
231233
throw new SyntaxError("The Parameters key `$key` contains invalid characters.");
232234
}
235+
}
236+
237+
private static function validate(string $key, Item $item): void
238+
{
239+
self::validateKey($key);
233240

234241
if (!$item->parameters()->isEmpty()) {
235242
throw new SyntaxError('Parameters instances can not contain parameterized Items.');

0 commit comments

Comments
 (0)