Skip to content

Commit 46812ca

Browse files
committed
Adding ::hasNoMember method
1 parent d249610 commit 46812ca

11 files changed

+49
-51
lines changed

src/Dictionary.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function count(): int
105105
return count($this->members);
106106
}
107107

108+
public function hasNoMembers(): bool
109+
{
110+
return !$this->hasMembers();
111+
}
112+
108113
public function hasMembers(): bool
109114
{
110115
return [] !== $this->members;

src/DictionaryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function it_can_be_instantiated_with_an_collection_of_item_or_inner_list(
3434
$instance->clear();
3535

3636
self::assertFalse($instance->hasMembers());
37+
self::assertTrue($instance->hasNoMembers());
3738
}
3839

3940
/** @test */
@@ -65,6 +66,7 @@ public function it_can_add_or_remove_members(): void
6566

6667
self::assertCount(2, $instance);
6768
self::assertTrue($instance->hasMembers());
69+
self::assertFalse($instance->hasNoMembers());
6870

6971
$instance->delete('boolean');
7072

src/InnerList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function count(): int
105105
return count($this->members);
106106
}
107107

108+
public function hasNoMembers(): bool
109+
{
110+
return !$this->hasMembers();
111+
}
112+
108113
public function hasMembers(): bool
109114
{
110115
return [] !== $this->members;

src/InnerListTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public function it_can_be_instantiated_with_an_collection_of_item(): void
2222

2323
self::assertSame($stringItem, $instance->get(0));
2424
self::assertTrue($instance->hasMembers());
25+
self::assertFalse($instance->hasNoMembers());
2526
self::assertTrue($instance->parameters()->hasMembers());
2627
self::assertEquals($arrayParams, iterator_to_array($instance));
2728

2829
$instance->clear();
2930

3031
self::assertFalse($instance->hasMembers());
32+
self::assertTrue($instance->hasNoMembers());
3133
self::assertTrue($instance->parameters()->hasMembers());
3234
}
3335

src/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function fromHttpValue(Stringable|string $httpValue): self
4848
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
4949
}
5050

51-
return new self($value, Parameters::fromHttpValue(substr($itemString, $offset)));
51+
return self::from($value, Parameters::fromHttpValue(substr($itemString, $offset)));
5252
}
5353

5454
/**

src/MemberContainer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
interface MemberContainer extends Countable, ArrayAccess, IteratorAggregate, StructuredField
1919
{
20+
/**
21+
* Tells whether the instance contains no members.
22+
*/
23+
public function hasNoMembers(): bool;
24+
2025
/**
2126
* Tells whether the instance contains members.
2227
*/

src/OrderedList.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ private function __construct()
2727
{
2828
}
2929

30-
/**
31-
* @param InnerList<int, Item>|Item|DataType ...$members
32-
*
33-
* @return static
34-
*/
3530
public static function from(InnerList|Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3631
{
3732
return self::fromList($members);
@@ -82,6 +77,11 @@ public function count(): int
8277
return count($this->members);
8378
}
8479

80+
public function hasNoMembers(): bool
81+
{
82+
return !$this->hasMembers();
83+
}
84+
8585
public function hasMembers(): bool
8686
{
8787
return [] !== $this->members;
@@ -132,8 +132,6 @@ public function get(string|int $offset): Item|InnerList
132132

133133
/**
134134
* Inserts members at the beginning of the list.
135-
*
136-
* @param InnerList<int, Item>|Item|DataType ...$members
137135
*/
138136
public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
139137
{
@@ -144,8 +142,6 @@ public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|Str
144142

145143
/**
146144
* Inserts members at the end of the list.
147-
*
148-
* @param InnerList<int, Item>|Item|DataType ...$members
149145
*/
150146
public function push(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
151147
{
@@ -157,8 +153,6 @@ public function push(StructuredField|Token|ByteSequence|DateTimeInterface|String
157153
/**
158154
* Inserts members starting at the given index.
159155
*
160-
* @param InnerList<int, Item>|Item|DataType $members
161-
*
162156
* @throws InvalidOffset If the index does not exist
163157
*/
164158
public function insert(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
@@ -177,8 +171,6 @@ public function insert(int $index, StructuredField|Token|ByteSequence|DateTimeIn
177171
/**
178172
* Replaces the member associated with the index.
179173
*
180-
* @param InnerList<int, Item>|Item|DataType $member
181-
*
182174
* @throws InvalidOffset If the index does not exist
183175
*/
184176
public function replace(int $index, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self

src/OrderedListTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function it_can_be_instantiated_with_an_collection_of_item(): void
2525

2626
self::assertSame($stringItem, $instance->get(0));
2727
self::assertTrue($instance->hasMembers());
28+
self::assertFalse($instance->hasNoMembers());
2829
self::assertEquals($arrayParams, iterator_to_array($instance));
2930
}
3031

@@ -55,6 +56,7 @@ public function it_can_add_or_remove_members(): void
5556
$instance->remove(0, 1);
5657

5758
self::assertCount(0, $instance);
59+
self::assertTrue($instance->hasNoMembers());
5860
self::assertFalse($instance->hasMembers());
5961
}
6062

src/Parameters.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ public static function create(): self
4444
* its values represent the dictionary entry value
4545
*
4646
* @param iterable<array-key, Item|DataType> $members
47-
*
48-
* @throws SyntaxError If the string is not a valid
49-
* @throws ForbiddenStateError If the bare item contains parameters
5047
*/
5148
public static function fromAssociative(iterable $members): self
5249
{
@@ -66,8 +63,6 @@ public static function fromAssociative(iterable $members): self
6663
* the second member represents the instance entry value
6764
*
6865
* @param MemberOrderedMap<string, Item>|iterable<array{0:string, 1:Item|DataType}> $pairs
69-
*
70-
* @throws ForbiddenStateError If the bare item contains parameters
7166
*/
7267
public static function fromPairs(MemberOrderedMap|iterable $pairs): self
7368
{
@@ -88,7 +83,7 @@ public static function fromPairs(MemberOrderedMap|iterable $pairs): self
8883
*/
8984
private static function filterMember(Item $item): Item
9085
{
91-
if (!$item->parameters()->hasMembers()) {
86+
if ($item->parameters()->hasNoMembers()) {
9287
return $item;
9388
}
9489

@@ -133,9 +128,6 @@ public static function fromHttpValue(Stringable|string $httpValue): self
133128
return $instance;
134129
}
135130

136-
/**
137-
* @throws ForbiddenStateError if the bare item contains parameters itself
138-
*/
139131
public function toHttpValue(): string
140132
{
141133
$formatter = fn (Item $member, string $offset): string => match (true) {
@@ -151,14 +143,17 @@ public function count(): int
151143
return count($this->members);
152144
}
153145

146+
public function hasNoMembers(): bool
147+
{
148+
return !$this->hasMembers();
149+
}
150+
154151
public function hasMembers(): bool
155152
{
156153
return [] !== $this->members;
157154
}
158155

159156
/**
160-
* @throws ForbiddenStateError if the bare item contains parameters itself
161-
*
162157
* @return Iterator<array-key, Item>
163158
*/
164159
public function getIterator(): Iterator
@@ -169,14 +164,12 @@ public function getIterator(): Iterator
169164
/**
170165
* Returns an iterable construct of dictionary pairs.
171166
*
172-
* @throws ForbiddenStateError if the bare item contains parameters itself
173-
*
174167
* @return Iterator<array{0:string, 1:Item}>
175168
*/
176169
public function toPairs(): Iterator
177170
{
178171
foreach ($this->members as $index => $member) {
179-
yield [$index, self::filterMember($member)];
172+
yield [$index, $member];
180173
}
181174
}
182175

@@ -201,8 +194,7 @@ public function has(string|int $offset): bool
201194
/**
202195
* Returns the Item associated to the key.
203196
*
204-
* @throws InvalidOffset if the key is not found
205-
* @throws ForbiddenStateError if the bare item contains parameters itself
197+
* @throws InvalidOffset if the key is not found
206198
*/
207199
public function get(string|int $offset): Item
208200
{
@@ -244,8 +236,7 @@ private function filterIndex(int $index): int
244236
/**
245237
* Returns the key-item pair found at a given index.
246238
*
247-
* @throws InvalidOffset if the index is not found
248-
* @throws ForbiddenStateError if the found item is in invalid state
239+
* @throws InvalidOffset if the index is not found
249240
*
250241
* @return array{0:string, 1:Item}
251242
*/
@@ -268,9 +259,6 @@ public function pair(int $index): array
268259

269260
/**
270261
* Adds a member at the end of the instance otherwise updates the value associated with the key if already present.
271-
*
272-
* @throws SyntaxError If the string key is not a valid
273-
* @throws ForbiddenStateError if the found item is in invalid state
274262
*/
275263
public function set(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
276264
{
@@ -300,9 +288,6 @@ public function clear(): self
300288

301289
/**
302290
* Adds a member at the end of the instance and deletes any previous reference to the key if present.
303-
*
304-
* @throws SyntaxError If the string key is not a valid
305-
* @throws ForbiddenStateError if the found item is in invalid state
306291
*/
307292
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
308293
{
@@ -315,9 +300,6 @@ public function append(string $key, StructuredField|Token|ByteSequence|DateTimeI
315300

316301
/**
317302
* Adds a member at the beginning of the instance and deletes any previous reference to the key if present.
318-
*
319-
* @throws SyntaxError If the string key is not a valid
320-
* @throws ForbiddenStateError if the found item is in invalid state
321303
*/
322304
public function prepend(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): self
323305
{

src/ParametersTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function it_can_be_instantiated_with_an_collection_of_item(): void
3838

3939
self::assertFalse(isset($instance['foobar']));
4040
self::assertFalse($instance->hasMembers());
41+
self::assertTrue($instance->hasNoMembers());
4142
}
4243

4344
/** @test */
@@ -102,6 +103,7 @@ public function it_can_add_or_remove_members(): void
102103
$instance->delete('foobar', 'string');
103104
self::assertCount(0, $instance);
104105
self::assertFalse($instance->hasMembers());
106+
self::assertTrue($instance->hasNoMembers());
105107
}
106108

107109
/** @test */

0 commit comments

Comments
 (0)