Skip to content

Commit e32800e

Browse files
committed
Improve parsing and package docblocks
1 parent 754fac9 commit e32800e

13 files changed

+269
-251
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ use Bakame\Http\StructuredFields\InnerList;
224224
use Bakame\Http\StructuredFields\OrderedList;
225225
use Bakame\Http\StructuredFields\Token;
226226

227-
$innerList = InnerList::fromElements([42, 42.0, "42"], ["a" => true]);
227+
$innerList = InnerList::fromMembers([42, 42.0, "42"], ["a" => true]);
228228
$innerList->has(2); //return true
229229
$innerList->has(42); //return false
230230
$innerList->push(new Token('forty-two'));

src/Dictionary.php

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Dictionary implements Countable, IteratorAggregate, StructuredField
1515
{
1616
private function __construct(
1717
/** @var array<string, Item|InnerList> */
18-
private array $elements = []
18+
private array $members = []
1919
) {
2020
}
2121

@@ -25,13 +25,13 @@ private function __construct(
2525
* its keys represent the dictionary entry key
2626
* its values represent the dictionary entry value
2727
*
28-
* @param iterable<string, InnerList|Item|ByteSequence|Token|bool|int|float|string> $elements
28+
* @param iterable<string, InnerList|Item|ByteSequence|Token|bool|int|float|string> $members
2929
*/
30-
public static function fromAssociative(iterable $elements = []): self
30+
public static function fromAssociative(iterable $members = []): self
3131
{
3232
$instance = new self();
33-
foreach ($elements as $index => $element) {
34-
$instance->set($index, $element);
33+
foreach ($members as $index => $member) {
34+
$instance->set($index, $member);
3535
}
3636

3737
return $instance;
@@ -40,17 +40,17 @@ public static function fromAssociative(iterable $elements = []): self
4040
/**
4141
* Returns a new instance from a pair iterable construct.
4242
*
43-
* Each element is composed of an array with two elements
44-
* the first element represents the instance entry key
45-
* the second element represents the instance entry value
43+
* Each member is composed of an array with two elements
44+
* the first member represents the instance entry key
45+
* the second member represents the instance entry value
4646
*
4747
* @param iterable<array{0:string, 1:InnerList|Item|ByteSequence|Token|bool|int|float|string}> $pairs
4848
*/
4949
public static function fromPairs(iterable $pairs = []): self
5050
{
5151
$instance = new self();
52-
foreach ($pairs as [$key, $element]) {
53-
$instance->set($key, $element);
52+
foreach ($pairs as [$key, $member]) {
53+
$instance->set($key, $member);
5454
}
5555

5656
return $instance;
@@ -69,10 +69,10 @@ public static function fromHttpValue(string $httpValue): self
6969
public function toHttpValue(): string
7070
{
7171
$returnValue = [];
72-
foreach ($this->elements as $key => $element) {
72+
foreach ($this->members as $key => $member) {
7373
$returnValue[] = match (true) {
74-
$element instanceof Item && true === $element->value() => $key.$element->parameters()->toHttpValue(),
75-
default => $key.'='.$element->toHttpValue(),
74+
$member instanceof Item && true === $member->value() => $key.$member->parameters()->toHttpValue(),
75+
default => $key.'='.$member->toHttpValue(),
7676
};
7777
}
7878

@@ -81,21 +81,21 @@ public function toHttpValue(): string
8181

8282
public function count(): int
8383
{
84-
return count($this->elements);
84+
return count($this->members);
8585
}
8686

8787
public function isEmpty(): bool
8888
{
89-
return [] === $this->elements;
89+
return [] === $this->members;
9090
}
9191

9292
/**
9393
* @return Iterator<string, Item|InnerList>
9494
*/
9595
public function getIterator(): Iterator
9696
{
97-
foreach ($this->elements as $index => $element) {
98-
yield $index => $element;
97+
foreach ($this->members as $index => $member) {
98+
yield $index => $member;
9999
}
100100
}
101101

@@ -106,8 +106,8 @@ public function getIterator(): Iterator
106106
*/
107107
public function toPairs(): Iterator
108108
{
109-
foreach ($this->elements as $index => $element) {
110-
yield [$index, $element];
109+
foreach ($this->members as $index => $member) {
110+
yield [$index, $member];
111111
}
112112
}
113113

@@ -116,21 +116,21 @@ public function toPairs(): Iterator
116116
*/
117117
public function keys(): array
118118
{
119-
return array_keys($this->elements);
119+
return array_keys($this->members);
120120
}
121121

122122
public function has(string $key): bool
123123
{
124-
return array_key_exists($key, $this->elements);
124+
return array_key_exists($key, $this->members);
125125
}
126126

127127
public function get(string $key): Item|InnerList
128128
{
129-
if (!array_key_exists($key, $this->elements)) {
129+
if (!array_key_exists($key, $this->members)) {
130130
throw InvalidOffset::dueToKeyNotFound($key);
131131
}
132132

133-
return $this->elements[$key];
133+
return $this->members[$key];
134134
}
135135

136136
public function hasPair(int $index): bool
@@ -140,10 +140,10 @@ public function hasPair(int $index): bool
140140

141141
private function filterIndex(int $index): int|null
142142
{
143-
$max = count($this->elements);
143+
$max = count($this->members);
144144

145145
return match (true) {
146-
[] === $this->elements, 0 > $max + $index, 0 > $max - $index - 1 => null,
146+
[] === $this->members, 0 > $max + $index, 0 > $max - $index - 1 => null,
147147
0 > $index => $max + $index,
148148
default => $index,
149149
};
@@ -160,19 +160,19 @@ public function pair(int $index): array
160160
}
161161

162162
return [
163-
array_keys($this->elements)[$offset],
164-
array_values($this->elements)[$offset],
163+
array_keys($this->members)[$offset],
164+
array_values($this->members)[$offset],
165165
];
166166
}
167167

168168
/**
169-
* Add an element at the end of the instance if the key is new otherwise update the value associated with the key.
169+
* Add a member at the end of the instance if the key is new otherwise update the value associated with the key.
170170
*/
171-
public function set(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $element): void
171+
public function set(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $member): void
172172
{
173173
self::validateKey($key);
174174

175-
$this->elements[$key] = self::filterElement($element);
175+
$this->members[$key] = self::filterMember($member);
176176
}
177177

178178
/**
@@ -185,54 +185,54 @@ private static function validateKey(string $key): void
185185
}
186186
}
187187

188-
private static function filterElement(InnerList|Item|ByteSequence|Token|bool|int|float|string $element): InnerList|Item
188+
private static function filterMember(InnerList|Item|ByteSequence|Token|bool|int|float|string $member): InnerList|Item
189189
{
190190
return match (true) {
191-
$element instanceof InnerList, $element instanceof Item => $element,
192-
default => Item::from($element),
191+
$member instanceof InnerList, $member instanceof Item => $member,
192+
default => Item::from($member),
193193
};
194194
}
195195

196196
/**
197-
* Delete elements associated with the list of submitted keys.
197+
* Delete members associated with the list of submitted keys.
198198
*/
199199
public function delete(string ...$keys): void
200200
{
201201
foreach ($keys as $key) {
202-
unset($this->elements[$key]);
202+
unset($this->members[$key]);
203203
}
204204
}
205205

206206
/**
207-
* Remove all elements from the instance.
207+
* Remove all members from the instance.
208208
*/
209209
public function clear(): void
210210
{
211-
$this->elements = [];
211+
$this->members = [];
212212
}
213213

214214
/**
215-
* Add an element at the end of the instance if the key is new delete any previous reference to the key.
215+
* Add a member at the end of the instance if the key is new delete any previous reference to the key.
216216
*/
217-
public function append(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $element): void
217+
public function append(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $member): void
218218
{
219219
self::validateKey($key);
220220

221-
unset($this->elements[$key]);
221+
unset($this->members[$key]);
222222

223-
$this->elements[$key] = self::filterElement($element);
223+
$this->members[$key] = self::filterMember($member);
224224
}
225225

226226
/**
227-
* Add an element at the beginning of the instance if the key is new delete any previous reference to the key.
227+
* Add a member at the beginning of the instance if the key is new delete any previous reference to the key.
228228
*/
229-
public function prepend(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $element): void
229+
public function prepend(string $key, InnerList|Item|ByteSequence|Token|bool|int|float|string $member): void
230230
{
231231
self::validateKey($key);
232232

233-
unset($this->elements[$key]);
233+
unset($this->members[$key]);
234234

235-
$this->elements = [...[$key => self::filterElement($element)], ...$this->elements];
235+
$this->members = [...[$key => self::filterMember($member)], ...$this->members];
236236
}
237237

238238
/**
@@ -241,7 +241,7 @@ public function prepend(string $key, InnerList|Item|ByteSequence|Token|bool|int|
241241
public function merge(self ...$others): void
242242
{
243243
foreach ($others as $other) {
244-
$this->elements = [...$this->elements, ...$other->elements];
244+
$this->members = [...$this->members, ...$other->members];
245245
}
246246
}
247247
}

src/DictionaryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function test_it_can_be_instantiated_with_key_value_pairs(): void
5656
/**
5757
* @test
5858
*/
59-
public function it_can_add_or_remove_elements(): void
59+
public function it_can_add_or_remove_members(): void
6060
{
6161
$stringItem = Item::from('helloWorld');
6262
$booleanItem = Item::from(true);
@@ -122,7 +122,7 @@ public function it_fails_to_add_an_item_with_wrong_key(): void
122122
/**
123123
* @test
124124
*/
125-
public function it_can_prepend_an_element(): void
125+
public function it_can_prepend_a_new_member(): void
126126
{
127127
$instance = Dictionary::fromAssociative();
128128
$instance->append('a', Item::from(false));
@@ -134,7 +134,7 @@ public function it_can_prepend_an_element(): void
134134
/**
135135
* @test
136136
*/
137-
public function it_can_returns_the_container_element_keys(): void
137+
public function it_can_returns_the_container_member_keys(): void
138138
{
139139
$instance = Dictionary::fromAssociative();
140140
self::assertSame([], $instance->keys());

0 commit comments

Comments
 (0)