Skip to content

Commit 6183baf

Browse files
committed
Improve codebase and remove MapKey from public API
1 parent 476e529 commit 6183baf

10 files changed

+62
-88
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1414
- `Item::fromDate` to improve and complete the Item Date public API;
1515
- `Value` internal class to improve Item public API;
1616
- `Token::toString` to return the string representation of the token.
17-
- Adding support for `MapKey` object to access container members.
1817

1918
### Fixed
2019

src/Dictionary.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ final class Dictionary implements MemberOrderedMap
3131
private readonly array $members;
3232

3333
/**
34-
* @param iterable<MapKey|string, SfMember|SfMemberInput> $members
34+
* @param iterable<string, SfMember|SfMemberInput> $members
3535
*/
3636
private function __construct(iterable $members = [])
3737
{
3838
$filteredMembers = [];
3939
foreach ($members as $key => $member) {
40-
$offset = $key instanceof MapKey ? $key->value : MapKey::from($key)->value;
41-
$filteredMembers[$offset] = self::filterMember($member);
40+
$filteredMembers[MapKey::from($key)->value] = self::filterMember($member);
4241
}
4342

4443
$this->members = $filteredMembers;
@@ -171,13 +170,9 @@ public function keys(): array
171170
return array_keys($this->members);
172171
}
173172

174-
public function has(MapKey|string|int ...$keys): bool
173+
public function has(string|int ...$keys): bool
175174
{
176175
foreach ($keys as $key) {
177-
if ($key instanceof MapKey) {
178-
$key = $key->value;
179-
}
180-
181176
if (!is_string($key) || !array_key_exists($key, $this->members)) {
182177
return false;
183178
}
@@ -190,16 +185,12 @@ public function has(MapKey|string|int ...$keys): bool
190185
* @throws SyntaxError If the key is invalid
191186
* @throws InvalidOffset If the key is not found
192187
*/
193-
public function get(MapKey|string|int $key): StructuredField
188+
public function get(string|int $key): StructuredField
194189
{
195190
if (!$this->has($key)) {
196191
throw InvalidOffset::dueToKeyNotFound($key);
197192
}
198193

199-
if ($key instanceof MapKey) {
200-
$key = $key->value;
201-
}
202-
203194
return $this->members[$key];
204195
}
205196

@@ -248,11 +239,11 @@ public function add(string $key, StructuredField|Token|ByteSequence|DateTimeInte
248239
return new self($members);
249240
}
250241

251-
public function remove(MapKey|string|int ...$keys): static
242+
public function remove(string|int ...$keys): static
252243
{
253244
$members = $this->members;
254-
foreach (array_filter($keys, static fn (MapKey|string|int $key): bool => !is_int($key)) as $key) {
255-
unset($members[$key instanceof MapKey ? $key->value : $key]);
245+
foreach (array_filter($keys, static fn (string|int $key): bool => !is_int($key)) as $key) {
246+
unset($members[$key]);
256247
}
257248

258249
if ($members === $this->members) {

src/DictionaryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public function it_can_be_instantiated_with_an_collection_of_item_or_inner_list(
2222
$arrayParams = ['string' => $stringItem, 'boolean' => $booleanItem];
2323
$instance = Dictionary::fromAssociative($arrayParams);
2424

25+
2526
self::assertSame(['string', $stringItem], $instance->pair(0));
2627
self::assertSame($stringItem, $instance->get('string'));
27-
self::assertTrue($instance->has('string', MapKey::from('string')));
28-
self::assertFalse($instance->has(MapKey::from('string'), 'no-present'));
28+
self::assertTrue($instance->has('string', 'string'));
29+
self::assertFalse($instance->has('string', 'no-present'));
2930
self::assertEquals([['string', $stringItem], ['boolean', $booleanItem]], [...$instance->toPairs()]);
3031
self::assertEquals($arrayParams, [...$instance]);
3132
}
@@ -156,7 +157,6 @@ public function it_can_merge_one_or_more_instances_using_associative(): void
156157

157158
self::assertCount(2, $instance4);
158159
self::assertEquals(Item::from(42), $instance4->get('a'));
159-
self::assertEquals(Item::from(42), $instance4->get(MapKey::from('a')));
160160
self::assertEquals(Item::from(true), $instance4->get('b'));
161161
}
162162

src/InnerList.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function fromPair(array $pair): self
7373
}
7474

7575
if (2 !== count($pair)) { /* @phpstan-ignore-line */
76-
throw new SyntaxError('The pair first value should be the member list and the optional second value the inner list parameters.');
76+
throw new SyntaxError('The pair first member must be the member list and the optional second member the inner list parameters.');
7777
}
7878

7979
if (!$pair[1] instanceof Parameters) {
@@ -112,10 +112,10 @@ public function parameters(): Parameters
112112
return $this->parameters;
113113
}
114114

115-
public function parameter(MapKey|string $key): mixed
115+
public function parameter(string $key): mixed
116116
{
117117
try {
118-
return $this->parameters->get($key instanceof MapKey ? $key->value : $key)->value();
118+
return $this->parameters->get($key)->value();
119119
} catch (StructuredFieldError) {
120120
return null;
121121
}
@@ -130,26 +130,24 @@ public function withParameters(Parameters $parameters): static
130130
return new static($parameters, $this->members);
131131
}
132132

133-
public function addParameter(MapKey|string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
133+
public function addParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
134134
{
135-
return $this->withParameters($this->parameters()->add($key instanceof MapKey ? $key->value : $key, $member));
135+
return $this->withParameters($this->parameters()->add($key, $member));
136136
}
137137

138-
public function prependParameter(MapKey|string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
138+
public function prependParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
139139
{
140-
return $this->withParameters($this->parameters()->prepend($key instanceof MapKey ? $key->value : $key, $member));
140+
return $this->withParameters($this->parameters()->prepend($key, $member));
141141
}
142142

143-
public function appendParameter(MapKey|string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
143+
public function appendParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
144144
{
145-
return $this->withParameters($this->parameters()->append($key instanceof MapKey ? $key->value : $key, $member));
145+
return $this->withParameters($this->parameters()->append($key, $member));
146146
}
147147

148-
public function withoutParameter(MapKey|string ...$keys): static
148+
public function withoutParameter(string ...$keys): static
149149
{
150-
return $this->withParameters($this->parameters()->remove(
151-
...array_map(fn (MapKey|string $key): string => $key instanceof MapKey ? $key->value : $key, $keys)
152-
));
150+
return $this->withParameters($this->parameters()->remove(...$keys));
153151
}
154152

155153
public function withoutAnyParameter(): static
@@ -203,7 +201,7 @@ public function getIterator(): Iterator
203201
yield from $this->members;
204202
}
205203

206-
public function has(MapKey|string|int ...$keys): bool
204+
public function has(string|int ...$keys): bool
207205
{
208206
foreach ($keys as $offset) {
209207
if (null === $this->filterIndex($offset)) {
@@ -214,7 +212,7 @@ public function has(MapKey|string|int ...$keys): bool
214212
return [] !== $keys;
215213
}
216214

217-
private function filterIndex(MapKey|string|int $index): int|null
215+
private function filterIndex(string|int $index): int|null
218216
{
219217
if (!is_int($index)) {
220218
return null;
@@ -234,7 +232,7 @@ private function filterIndex(MapKey|string|int $index): int|null
234232
/**
235233
* @return SfItem
236234
*/
237-
public function get(MapKey|string|int $key): StructuredField
235+
public function get(string|int $key): StructuredField
238236
{
239237
$index = $this->filterIndex($key);
240238
if (null === $index) {
@@ -341,12 +339,12 @@ public function replace(int $key, StructuredField|Token|ByteSequence|DateTimeInt
341339
/**
342340
* Deletes members associated with the list of instance indexes.
343341
*/
344-
public function remove(MapKey|string|int ...$keys): static
342+
public function remove(string|int ...$keys): static
345343
{
346344
$offsets = array_filter(
347345
array_map(
348346
fn (int $index): int|null => $this->filterIndex($index),
349-
array_filter($keys, static fn (MapKey|string|int $key): bool => is_int($key))
347+
array_filter($keys, static fn (string|int $key): bool => is_int($key))
350348
),
351349
fn (int|null $index): bool => null !== $index
352350
);

src/InvalidOffset.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ private function __construct(string $message)
1313
parent::__construct($message);
1414
}
1515

16-
public static function dueToIndexNotFound(MapKey|string|int $index): self
16+
public static function dueToIndexNotFound(string|int $index): self
1717
{
1818
if (is_string($index)) {
1919
return new self('The member index can not be the string "'.$index.'".');
2020
}
2121

22-
return new self('No member exists with the index "'.($index instanceof MapKey ? $index->value : $index).'".');
22+
return new self('No member exists with the index "'.$index.'".');
2323
}
2424

25-
public static function dueToKeyNotFound(MapKey|string|int $key): self
25+
public static function dueToKeyNotFound(string|int $key): self
2626
{
2727
if (is_int($key)) {
28-
return new self('The member key can not be the integer "'.$key.'".');
28+
return new self('The member key can not be the integer "'.$key.'".');
2929
}
3030

31-
return new self('No member exists with the key "'.($key instanceof MapKey ? $key->value : $key).'".');
31+
return new self('No member exists with the key "'.$key.'".');
3232
}
3333
}

src/Item.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function fromPair(array $pair): self
5858
}
5959

6060
if (2 !== count($pair)) { /* @phpstan-ignore-line */
61-
throw new SyntaxError('The pair first value should be the item value and the optional second value the item parameters.');
61+
throw new SyntaxError('The pair first member must be the item value and the optional second member the item parameters.');
6262
}
6363

6464
if (!$pair[1] instanceof Parameters) {
@@ -194,10 +194,10 @@ public function parameters(): Parameters
194194
return $this->parameters;
195195
}
196196

197-
public function parameter(MapKey|string $key): mixed
197+
public function parameter(string $key): mixed
198198
{
199199
try {
200-
return $this->parameters->get($key instanceof MapKey ? $key->value : $key)->value();
200+
return $this->parameters->get($key)->value();
201201
} catch (StructuredFieldError) {
202202
return null;
203203
}
@@ -241,26 +241,24 @@ public function withParameters(Parameters $parameters): static
241241
return $this->parameters->toHttpValue() === $parameters->toHttpValue() ? $this : new static($this->value, $parameters);
242242
}
243243

244-
public function addParameter(MapKey|string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
244+
public function addParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
245245
{
246-
return $this->withParameters($this->parameters()->add($key instanceof MapKey ? $key->value : $key, $member));
246+
return $this->withParameters($this->parameters()->add($key, $member));
247247
}
248248

249-
public function prependParameter(MapKey|string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
249+
public function prependParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
250250
{
251-
return $this->withParameters($this->parameters()->prepend($key instanceof MapKey ? $key->value : $key, $member));
251+
return $this->withParameters($this->parameters()->prepend($key, $member));
252252
}
253253

254-
public function appendParameter(MapKey|string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
254+
public function appendParameter(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
255255
{
256-
return $this->withParameters($this->parameters()->append($key instanceof MapKey ? $key->value : $key, $member));
256+
return $this->withParameters($this->parameters()->append($key, $member));
257257
}
258258

259-
public function withoutParameter(MapKey|string ...$keys): static
259+
public function withoutParameter(string ...$keys): static
260260
{
261-
return $this->withParameters($this->parameters()->remove(
262-
...array_map(fn (MapKey|string $key): string => $key instanceof MapKey ? $key->value : $key, $keys)
263-
));
261+
return $this->withParameters($this->parameters()->remove(...$keys));
264262
}
265263

266264
public function withoutAnyParameter(): static

src/MemberContainer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ public function keys(): array;
3636
/**
3737
* @return TValue
3838
*/
39-
public function get(MapKey|string|int $key): StructuredField;
39+
public function get(string|int $key): StructuredField;
4040

4141
/**
4242
* Tells whether the instance contain a members at the specified offsets.
4343
*/
44-
public function has(MapKey|string|int ...$keys): bool;
44+
public function has(string|int ...$keys): bool;
4545

4646
/**
4747
* Deletes members associated with the list of submitted keys.
4848
*
4949
* This method MUST retain the state of the current instance, and return
5050
* an instance that contains the specified changes.
5151
*/
52-
public function remove(MapKey|string|int ...$keys): static;
52+
public function remove(string|int ...$keys): static;
5353
}

src/OuterList.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function getIterator(): Iterator
110110
yield from $this->members;
111111
}
112112

113-
public function has(MapKey|string|int ...$keys): bool
113+
public function has(string|int ...$keys): bool
114114
{
115115
foreach ($keys as $offset) {
116116
if (null === $this->filterIndex($offset)) {
@@ -121,7 +121,7 @@ public function has(MapKey|string|int ...$keys): bool
121121
return [] !== $keys;
122122
}
123123

124-
private function filterIndex(MapKey|string|int $index): int|null
124+
private function filterIndex(string|int $index): int|null
125125
{
126126
if (!is_int($index)) {
127127
return null;
@@ -141,7 +141,7 @@ private function filterIndex(MapKey|string|int $index): int|null
141141
/**
142142
* @return SfMember
143143
*/
144-
public function get(MapKey|string|int $key): StructuredField
144+
public function get(string|int $key): StructuredField
145145
{
146146
$index = $this->filterIndex($key);
147147
if (null === $index) {
@@ -248,12 +248,12 @@ public function replace(int $key, StructuredField|Token|ByteSequence|DateTimeInt
248248
/**
249249
* Deletes members associated with the list of instance indexes.
250250
*/
251-
public function remove(MapKey|string|int ...$keys): static
251+
public function remove(string|int ...$keys): static
252252
{
253253
$offsets = array_filter(
254254
array_map(
255255
fn (int $index): int|null => $this->filterIndex($index),
256-
array_filter($keys, static fn (MapKey|string|int $key): bool => is_int($key))
256+
array_filter($keys, static fn (string|int $key): bool => is_int($key))
257257
),
258258
fn (int|null $index): bool => null !== $index
259259
);

0 commit comments

Comments
 (0)