Skip to content

Commit 3830872

Browse files
committed
Improve values method implementation
1 parent f2c995a commit 3830872

File tree

7 files changed

+37
-42
lines changed

7 files changed

+37
-42
lines changed

src/Dictionary.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Iterator;
88
use Throwable;
99
use TypeError;
10-
use function array_filter;
1110
use function array_key_exists;
1211
use function array_keys;
1312
use function array_map;
@@ -148,29 +147,27 @@ public function has(string|int $offset): bool
148147
/**
149148
* Returns all containers Item values.
150149
*
151-
* @return array<string, InnerList<int, Item>|float|int|bool|string>
150+
* @return array<string, array<int, float|int|bool|string>|float|int|bool|string>
152151
*/
153152
public function values(): array
154153
{
155-
$mapper = function (Item|InnerList $item): InnerList|float|int|bool|string|null {
156-
try {
157-
$member = self::filterForbiddenState($item);
158-
159-
return $member instanceof Item ? $member->value() : $member;
160-
} catch (Throwable) {
161-
return null;
154+
$result = [];
155+
foreach ($this->members as $offset => $item) {
156+
$value = $this->value($offset);
157+
if (null !== $value) {
158+
$result[$offset] = $value;
162159
}
163-
};
160+
}
164161

165-
return array_filter(array_map($mapper, $this->members), fn (mixed $value): bool => null !== $value);
162+
return $result;
166163
}
167164

168165
/**
169166
* Returns the Item value of a specific key if it exists and is valid otherwise returns null.
170167
*
171-
* @return InnerList<int, Item>|float|int|bool|string|null
168+
* @return array<int, float|int|bool|string>|float|int|bool|string|null
172169
*/
173-
public function value(string|int $offset): InnerList|float|int|bool|string|null
170+
public function value(string|int $offset): array|float|int|bool|string|null
174171
{
175172
try {
176173
$member = $this->get($offset);
@@ -182,7 +179,7 @@ public function value(string|int $offset): InnerList|float|int|bool|string|null
182179
return $member->value();
183180
}
184181

185-
return $member;
182+
return $member->values();
186183
}
187184

188185
/**

src/DictionaryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ public function it_will_strip_invalid_state_object_via_values_methods(): void
301301

302302
self::assertNull($structuredField->value('a'));
303303
self::assertArrayNotHasKey('a', $structuredField->values());
304-
self::assertEquals(['b' => false, 'c' => $innerList], $structuredField->values());
305-
self::assertSame($innerList, $structuredField->value('c'));
306-
self::assertSame($innerList, $structuredField->values()['c']);
304+
self::assertEquals(['b' => false, 'c' => [0 => 'foobar']], $structuredField->values());
305+
self::assertSame([0 => 'foobar'], $structuredField->value('c'));
306+
self::assertSame([0 => 'foobar'], $structuredField->values()['c']);
307307
}
308308
}

src/InnerList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ private function filterIndex(int $index): int|null
122122
/**
123123
* Returns all containers Item values.
124124
*
125-
* @return array<int, Token|ByteSequence|float|int|bool|string>
125+
* @return array<int, float|int|bool|string>
126126
*/
127127
public function values(): array
128128
{
129129
$result = [];
130130
foreach ($this->members as $offset => $item) {
131-
try {
132-
$result[$offset] = self::filterMember($item)->value();
133-
} catch (Throwable) {
131+
$value = $this->value($offset);
132+
if (null !== $value) {
133+
$result[$offset] = $value;
134134
}
135135
}
136136

src/MemberContainer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public function offsetSet(mixed $offset, mixed $value): void;
6868
public function offsetUnset(mixed $offset): void;
6969

7070
/**
71-
* @return array<TKey, MemberContainer<int,Item>|ByteSequence|Token|string|int|float|bool>
71+
* @return array<TKey, array<array-key, string|int|float|bool>|string|int|float|bool>
7272
*/
7373
public function values(): array;
7474

7575
/**
76-
* @return MemberContainer<int,Item>|string|int|float|bool|null
76+
* @return array<array-key, string|int|float|bool>|string|int|float|bool|null
7777
*/
78-
public function value(string|int $offset): MemberContainer|float|int|bool|string|null;
78+
public function value(string|int $offset): array|float|int|bool|string|null;
7979
}

src/OrderedList.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,29 +135,27 @@ private function filterIndex(int|string $index): int|null
135135
/**
136136
* Returns all containers Item values.
137137
*
138-
* @return array<int, InnerList<int, Item>|float|int|bool|string>
138+
* @return array<int, array<int, float|int|bool|string>|float|int|bool|string>
139139
*/
140140
public function values(): array
141141
{
142-
$mapper = function (Item|InnerList $item): InnerList|float|int|bool|string|null {
143-
try {
144-
$member = self::filterForbiddenState($item);
145-
146-
return $member instanceof Item ? $member->value() : $member;
147-
} catch (Throwable) {
148-
return null;
142+
$result = [];
143+
foreach ($this->members as $offset => $item) {
144+
$value = $this->value($offset);
145+
if (null !== $value) {
146+
$result[$offset] = $value;
149147
}
150-
};
148+
}
151149

152-
return array_filter(array_map($mapper, $this->members), fn (mixed $value): bool => null !== $value);
150+
return $result;
153151
}
154152

155153
/**
156154
* Returns the Item value of a specific key if it exists and is valid otherwise returns null.
157155
*
158-
* @return InnerList<int, Item>|float|int|bool|string|null
156+
* @return array<int, float|int|bool|string>|float|int|bool|string|null
159157
*/
160-
public function value(string|int $offset): InnerList|float|int|bool|string|null
158+
public function value(string|int $offset): array|float|int|bool|string|null
161159
{
162160
try {
163161
$member = $this->get($offset);
@@ -169,7 +167,7 @@ public function value(string|int $offset): InnerList|float|int|bool|string|null
169167
return $member->value();
170168
}
171169

172-
return $member;
170+
return $member->values();
173171
}
174172

175173
public function get(string|int $offset): Item|InnerList

src/OrderedListTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public function it_can_access_the_item_value(): void
189189
$input = ['foobar', 0, false, $token, $innerList];
190190
$structuredField = OrderedList::fromList($input);
191191

192-
self::assertSame(['foobar', 0, false, 'token', $innerList], $structuredField->values());
192+
self::assertSame(['foobar', 0, false, 'token', [0 => 'test']], $structuredField->values());
193193
self::assertFalse($structuredField->value(2));
194194
self::assertNull($structuredField->value(42));
195195
self::assertNull($structuredField->value('2'));
196-
self::assertSame($innerList, $structuredField->value(-1));
196+
self::assertSame([0 => 'test'], $structuredField->value(-1));
197197
}
198198

199199
/** @test */

src/Parameters.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ public function values(): array
206206
{
207207
$result = [];
208208
foreach ($this->members as $offset => $item) {
209-
try {
210-
$result[$offset] = self::filterMember($item, $offset)->value();
211-
} catch (Throwable) {
209+
$value = $this->value($offset);
210+
if (null !== $value) {
211+
$result[$offset] = $value;
212212
}
213213
}
214214

0 commit comments

Comments
 (0)