Skip to content

Commit 87ee36b

Browse files
committed
Improve filtering index for Ordered Map objects
1 parent 5d05f5f commit 87ee36b

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All Notable changes to `bakame/http-strucured-fields` will be documented in this file
44

5-
## [Next] - TBD
5+
## [0.4.0] - 2022-03-27
66

77
### Added
88

@@ -91,7 +91,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
9191

9292
**Initial release!**
9393

94-
[Next]: https://github.com/bakame-php/http-structured-fields/compare/0.3.0...master
94+
[Next]: https://github.com/bakame-php/http-structured-fields/compare/0.4.0...master
95+
[0.4.0]: https://github.com/bakame-php/http-structured-fields/compare/0.3.0...0.4.0
9596
[0.3.0]: https://github.com/bakame-php/http-structured-fields/compare/0.2.0...0.3.0
9697
[0.2.0]: https://github.com/bakame-php/http-structured-fields/compare/0.1.0...0.2.0
9798
[0.1.0]: https://github.com/bakame-php/http-structured-fields/releases/tag/0.1.0

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ key to its members as such they expose the following methods:
188188
- `prepend` always add an element at the beginning of the container, if already present the previous value is removed;
189189
- `delete` to remove elements based on their associated keys;
190190
- `keys` to list all existing keys for the ordered maps as an array list;
191-
- `merge` merge multiple instances of iterable structure as associative constructs;
191+
- `mergeAssociative` merge multiple instances of iterable structure as associative constructs;
192+
- `mergePairs` merge multiple instances of iterable structure as pairs constructs;
192193

193194
```php
194195
use Bakame\Http\StructuredFields\Dictionary;

src/Dictionary.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,24 @@ public function get(string $key): Item|InnerList
174174
*/
175175
public function hasPair(int $index): bool
176176
{
177-
return null !== $this->filterIndex($index);
177+
try {
178+
$this->filterIndex($index);
179+
180+
return true;
181+
} catch (InvalidOffset) {
182+
return false;
183+
}
178184
}
179185

180186
/**
181187
* Validate and Format the submitted index position.
182188
*/
183-
private function filterIndex(int $index): int|null
189+
private function filterIndex(int $index): int
184190
{
185191
$max = count($this->members);
186192

187193
return match (true) {
188-
[] === $this->members, 0 > $max + $index, 0 > $max - $index - 1 => null,
194+
[] === $this->members, 0 > $max + $index, 0 > $max - $index - 1 => throw InvalidOffset::dueToIndexNotFound($index),
189195
0 > $index => $max + $index,
190196
default => $index,
191197
};
@@ -202,9 +208,6 @@ private function filterIndex(int $index): int|null
202208
public function pair(int $index): array
203209
{
204210
$offset = $this->filterIndex($index);
205-
if (null === $offset) {
206-
throw InvalidOffset::dueToIndexNotFound($index);
207-
}
208211

209212
foreach ($this->toPairs() as $k => $pair) {
210213
if ($k === $offset) {

src/DictionaryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function it_can_add_or_remove_members(): void
7575
self::assertFalse($instance->hasPair(1));
7676

7777
$instance->append('foobar', Item::from('BarBaz'));
78+
self::assertTrue($instance->hasPair(1));
79+
7880
/** @var array{0:string, 1:Item} $foundItem */
7981
$foundItem = $instance->pair(1);
8082

src/Parameters.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,24 @@ public function value(string $key): Token|ByteSequence|float|int|bool|string|nul
267267
*/
268268
public function hasPair(int $index): bool
269269
{
270-
return null !== $this->formatIndex($index);
270+
try {
271+
$this->filterIndex($index);
272+
273+
return true;
274+
} catch (InvalidOffset) {
275+
return false;
276+
}
271277
}
272278

273279
/**
274280
* Filter and format instance index.
275281
*/
276-
private function formatIndex(int $index): int|null
282+
private function filterIndex(int $index): int
277283
{
278284
$max = count($this->members);
279285

280286
return match (true) {
281-
[] === $this->members, 0 > $max + $index, 0 > $max - $index - 1 => null,
287+
[] === $this->members, 0 > $max + $index, 0 > $max - $index - 1 => throw InvalidOffset::dueToIndexNotFound($index),
282288
0 > $index => $max + $index,
283289
default => $index,
284290
};
@@ -294,10 +300,7 @@ private function formatIndex(int $index): int|null
294300
*/
295301
public function pair(int $index): array
296302
{
297-
$offset = $this->formatIndex($index);
298-
if (null === $offset) {
299-
throw InvalidOffset::dueToIndexNotFound($index);
300-
}
303+
$offset = $this->filterIndex($index);
301304

302305
$i = 0;
303306
foreach ($this->members as $key => $member) {

src/ParametersTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function it_can_add_or_remove_members(): void
9696
self::assertFalse($instance->hasPair(1));
9797

9898
$instance->append('foobar', Item::from('BarBaz'));
99+
self::assertTrue($instance->hasPair(1));
99100
$foundItem = $instance->pair(1);
100101

101102
self::assertCount(2, $instance);

0 commit comments

Comments
 (0)