Skip to content

Commit 9c1e3eb

Browse files
committed
Improve Public API around Collection instantiation
1 parent ea97030 commit 9c1e3eb

File tree

10 files changed

+44
-47
lines changed

10 files changed

+44
-47
lines changed

CHANGELOG.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,23 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
77
### Added
88

99
- `ParameterAccess` interface updated with 3 new methods to ease parameter members modification.
10-
- Support for `Stringable` instance to `Item::from`, the instances will be converted to the string data type.
11-
- `Parameter::new` named constructor to create a new instance without any parameter.
12-
- `Dictionnary::new` named constructor to create a new instance without any parameter.
13-
- `InnerList::new` named constructor to create a new instance with members as variadic arguments.
14-
- `OrderedList::new` named constructor to create a new instance with members as variadic arguments.
10+
- Support for `Stringable` instance added to `Item::from`, the instances will be converted to the string data type.
11+
- `Parameter::create` named constructor to create a new instance without any parameter.
12+
- `Dictionnary::create` named constructor to create a new instance without any parameter.
1513

1614
### Fixed
1715

1816
- **[BC Break]** `::fromAssociative`, `::fromList`, `::fromPairs` methods require iterable arguments without default value.
1917
- **[BC Break]** `Item::value` method returns the Item (returns value can be `float|int|string|bool|ByteSequence|Token`).
20-
- `InnerList::parameters` is no longer accessible as a public readonly property.
18+
- **[BC Break]** `InnerList::parameters` is no longer accessible as a public readonly property.
2119

2220
### Deprecated
2321

2422
- None
2523

2624
### Removed
2725

28-
- **[BC Break]** `InnerList::from` named constructor use `InnerList::new` method instead.
29-
- **[BC Break]** `OrderedList::from` named constructor use `OrderedList::new` method instead.
26+
- None
3027

3128
## [0.6.0] - 2022-11-12
3229

docs/lists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $innerList->push(Token::fromString('forty-two'));
4646
$innerList->remove(0, 2);
4747
echo $innerList->toHttpValue(); //returns '(42.0 forty-two);a'
4848

49-
$orderedList = OrderedList::new(
49+
$orderedList = OrderedList::from(
5050
Item::from("42", ["foo" => "bar"]),
5151
$innerList
5252
);

src/Dictionary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private function __construct()
2828
/**
2929
* Returns a new instance.
3030
*/
31-
public static function new(): self
31+
public static function create(): self
3232
{
3333
return new self();
3434
}

src/DictionaryTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function it_can_add_or_remove_members(): void
9191
/** @test */
9292
public function it_fails_to_return_an_member_with_invalid_key(): void
9393
{
94-
$instance = Dictionary::new();
94+
$instance = Dictionary::create();
9595

9696
self::assertFalse($instance->has('foobar'));
9797

@@ -103,7 +103,7 @@ public function it_fails_to_return_an_member_with_invalid_key(): void
103103
/** @test */
104104
public function it_fails_to_return_an_member_with_invalid_index(): void
105105
{
106-
$instance = Dictionary::new();
106+
$instance = Dictionary::create();
107107

108108
self::assertFalse($instance->hasPair(3));
109109

@@ -123,7 +123,7 @@ public function it_fails_to_add_an_item_with_wrong_key(): void
123123
/** @test */
124124
public function it_can_prepend_a_new_member(): void
125125
{
126-
$instance = Dictionary::new();
126+
$instance = Dictionary::create();
127127
$instance->append('a', Item::from(false));
128128
$instance->prepend('b', Item::from(true));
129129

@@ -133,7 +133,7 @@ public function it_can_prepend_a_new_member(): void
133133
/** @test */
134134
public function it_can_returns_the_container_member_keys(): void
135135
{
136-
$instance = Dictionary::new();
136+
$instance = Dictionary::create();
137137

138138
self::assertSame([], $instance->keys());
139139

@@ -227,13 +227,13 @@ public function it_fails_to_add_an_integer_via_array_access(): void
227227
{
228228
$this->expectException(StructuredFieldError::class);
229229

230-
Dictionary::new()[0] = 23; // @phpstan-ignore-line
230+
Dictionary::create()[0] = 23; // @phpstan-ignore-line
231231
}
232232

233233
/** @test */
234234
public function it_can_delete_a_member_via_array_access(): void
235235
{
236-
$structuredField = Dictionary::new();
236+
$structuredField = Dictionary::create();
237237
$structuredField['foo'] = 'bar';
238238

239239
self::assertTrue($structuredField->hasMembers());
@@ -248,7 +248,7 @@ public function it_fails_to_fetch_an_value_using_an_integer(): void
248248
{
249249
$this->expectException(StructuredFieldError::class);
250250

251-
Dictionary::new()->get(0);
251+
Dictionary::create()->get(0);
252252
}
253253

254254
/** @test */

src/InnerList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private function __construct(private readonly Parameters $parameters)
2727
/**
2828
* Returns a new instance.
2929
*/
30-
public static function new(Item|ByteSequence|Token|Stringable|bool|int|float|string ...$members): self
30+
public static function from(Item|ByteSequence|Token|Stringable|bool|int|float|string ...$members): self
3131
{
3232
return self::fromList($members);
3333
}

src/InnerListTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function it_can_add_or_remove_members(): void
6565
/** @test */
6666
public function it_can_unshift_insert_and_replace(): void
6767
{
68-
$container = InnerList::new();
68+
$container = InnerList::from();
6969
$container->unshift('42');
7070
$container->push(42);
7171
$container->insert(1, 42.0);
@@ -81,21 +81,21 @@ public function it_fails_to_replace_invalid_index(): void
8181
{
8282
$this->expectException(InvalidOffset::class);
8383

84-
InnerList::new()->replace(0, ByteSequence::fromDecoded('Hello World'));
84+
InnerList::from()->replace(0, ByteSequence::fromDecoded('Hello World'));
8585
}
8686

8787
/** @test */
8888
public function it_fails_to_insert_at_an_invalid_index(): void
8989
{
9090
$this->expectException(InvalidOffset::class);
9191

92-
InnerList::new()->insert(3, ByteSequence::fromDecoded('Hello World'));
92+
InnerList::from()->insert(3, ByteSequence::fromDecoded('Hello World'));
9393
}
9494

9595
/** @test */
9696
public function it_fails_to_return_an_member_with_invalid_index(): void
9797
{
98-
$instance = InnerList::new();
98+
$instance = InnerList::from();
9999

100100
self::assertFalse($instance->has(3));
101101

@@ -146,7 +146,7 @@ public function it_successfully_parse_a_http_field_with_optional_white_spaces_in
146146
/** @test */
147147
public function it_implements_the_array_access_interface(): void
148148
{
149-
$sequence = InnerList::new();
149+
$sequence = InnerList::from();
150150
$sequence[] = 42;
151151

152152
self::assertTrue(isset($sequence[0]));
@@ -165,13 +165,13 @@ public function it_fails_to_insert_unknown_index_via_the_array_access_interface(
165165
{
166166
$this->expectException(StructuredFieldError::class);
167167

168-
InnerList::new()[0] = Item::from(42.0);
168+
InnerList::from()[0] = Item::from(42.0);
169169
}
170170

171171
/** @test */
172172
public function testArrayAccessThrowsInvalidIndex2(): void
173173
{
174-
$sequence = InnerList::new();
174+
$sequence = InnerList::from();
175175
unset($sequence[0]);
176176

177177
self::assertCount(0, $sequence);
@@ -182,7 +182,7 @@ public function it_fails_to_fetch_an_value_using_an_integer(): void
182182
{
183183
$this->expectException(InvalidOffset::class);
184184

185-
InnerList::new()->get('zero');
185+
InnerList::from()->get('zero');
186186
}
187187

188188
/** @test */

src/OrderedList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private function __construct()
3131
*
3232
* @return static
3333
*/
34-
public static function new(InnerList|Item|ByteSequence|Token|Stringable|bool|int|float|string ...$members): self
34+
public static function from(InnerList|Item|ByteSequence|Token|Stringable|bool|int|float|string ...$members): self
3535
{
3636
return self::fromList($members);
3737
}
@@ -65,7 +65,7 @@ private static function filterMember(StructuredField|ByteSequence|Token|Stringab
6565
*/
6666
public static function fromHttpValue(Stringable|string $httpValue): self
6767
{
68-
return self::new()
68+
return self::from()
6969
->push(...array_map(
7070
fn ($value) => is_array($value) ? InnerList::fromList(...$value) : $value,
7171
Parser::parseList($httpValue)

src/OrderedListTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function it_can_add_or_remove_members(): void
6161
/** @test */
6262
public function it_can_unshift_insert_and_replace(): void
6363
{
64-
$instance = OrderedList::new();
64+
$instance = OrderedList::from();
6565
$instance->unshift(Item::from('42'));
6666
$instance->push(Item::from(42));
6767
$instance->insert(1, Item::from(42.0));
@@ -80,21 +80,21 @@ public function it_fails_to_replace_invalid_index(): void
8080
{
8181
$this->expectException(InvalidOffset::class);
8282

83-
OrderedList::new()->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
83+
OrderedList::from()->replace(0, Item::from(ByteSequence::fromDecoded('Hello World')));
8484
}
8585

8686
/** @test */
8787
public function it_fails_to_insert_at_an_invalid_index(): void
8888
{
8989
$this->expectException(InvalidOffset::class);
9090

91-
OrderedList::new()->insert(3, Item::from(ByteSequence::fromDecoded('Hello World')));
91+
OrderedList::from()->insert(3, Item::from(ByteSequence::fromDecoded('Hello World')));
9292
}
9393

9494
/** @test */
9595
public function it_fails_to_return_an_member_with_invalid_index(): void
9696
{
97-
$instance = OrderedList::new();
97+
$instance = OrderedList::from();
9898

9999
self::assertFalse($instance->has(3));
100100

@@ -121,8 +121,8 @@ public function test_it_can_generate_the_same_value(): void
121121
/** @test */
122122
public function it_implements_the_array_access_interface(): void
123123
{
124-
$sequence = OrderedList::new();
125-
$sequence->push(InnerList::new(42, 69));
124+
$sequence = OrderedList::from();
125+
$sequence->push(InnerList::from(42, 69));
126126

127127
self::assertTrue(isset($sequence[0]));
128128
self::assertInstanceOf(InnerList::class, $sequence[0]);
@@ -142,13 +142,13 @@ public function it_fails_to_insert_unknown_index_via_the_array_access_interface(
142142
{
143143
$this->expectException(StructuredFieldError::class);
144144

145-
OrderedList::new()[0] = Item::from(42.0);
145+
OrderedList::from()[0] = Item::from(42.0);
146146
}
147147

148148
/** @test */
149149
public function testArrayAccessThrowsInvalidIndex2(): void
150150
{
151-
$sequence = OrderedList::new();
151+
$sequence = OrderedList::from();
152152
unset($sequence[0]);
153153

154154
self::assertCount(0, $sequence);
@@ -159,14 +159,14 @@ public function it_fails_to_fetch_an_value_using_an_integer(): void
159159
{
160160
$this->expectException(InvalidOffset::class);
161161

162-
OrderedList::new()->get('zero');
162+
OrderedList::from()->get('zero');
163163
}
164164

165165
/** @test */
166166
public function it_can_access_the_item_value(): void
167167
{
168168
$token = Token::fromString('token');
169-
$innerList = InnerList::new('test');
169+
$innerList = InnerList::from('test');
170170
$input = ['foobar', 0, false, $token, $innerList];
171171
$structuredField = OrderedList::fromList($input);
172172

src/Parameters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private function __construct()
3131
/**
3232
* Returns a new instance.
3333
*/
34-
public static function new(): self
34+
public static function create(): self
3535
{
3636
return new self();
3737
}

src/ParametersTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function it_fails_to_return_an_member_with_invalid_key(): void
116116
{
117117
$this->expectException(InvalidOffset::class);
118118

119-
$instance = Parameters::new();
119+
$instance = Parameters::create();
120120
self::assertFalse($instance->has('foobar'));
121121

122122
$instance->get('foobar');
@@ -125,7 +125,7 @@ public function it_fails_to_return_an_member_with_invalid_key(): void
125125
/** @test */
126126
public function it_fails_to_return_an_member_with_invalid_index(): void
127127
{
128-
$instance = Parameters::new();
128+
$instance = Parameters::create();
129129

130130
self::assertFalse($instance->hasPair(3));
131131

@@ -137,7 +137,7 @@ public function it_fails_to_return_an_member_with_invalid_index(): void
137137
/** @test */
138138
public function it_can_prepend_a_new_member(): void
139139
{
140-
$instance = Parameters::new();
140+
$instance = Parameters::create();
141141
$instance->append('a', Item::from(false));
142142
$instance->prepend('b', Item::from(true));
143143

@@ -147,7 +147,7 @@ public function it_can_prepend_a_new_member(): void
147147
/** @test */
148148
public function it_can_returns_the_container_member_keys(): void
149149
{
150-
$instance = Parameters::new();
150+
$instance = Parameters::create();
151151

152152
self::assertSame([], $instance->keys());
153153

@@ -259,13 +259,13 @@ public function it_fails_to_add_an_integer_via_array_access(): void
259259
{
260260
$this->expectException(StructuredFieldError::class);
261261

262-
Parameters::new()[0] = 23; // @phpstan-ignore-line
262+
Parameters::create()[0] = 23; // @phpstan-ignore-line
263263
}
264264

265265
/** @test */
266266
public function it_can_delete_a_member_via_array_access(): void
267267
{
268-
$structuredField = Parameters::new();
268+
$structuredField = Parameters::create();
269269
$structuredField['foo'] = 'bar';
270270

271271
self::assertTrue($structuredField->hasMembers());
@@ -280,14 +280,14 @@ public function it_fails_to_fetch_an_value_using_an_integer(): void
280280
{
281281
$this->expectException(InvalidOffset::class);
282282

283-
Parameters::new()->get(0);
283+
Parameters::create()->get(0);
284284
}
285285

286286
/** @test */
287287
public function it_throws_if_the_structured_field_is_not_supported(): void
288288
{
289289
$this->expectException(InvalidArgument::class);
290290

291-
Parameters::fromPairs([['foo', InnerList::new(42)]]); // @phpstan-ignore-line
291+
Parameters::fromPairs([['foo', InnerList::from(42)]]); // @phpstan-ignore-line
292292
}
293293
}

0 commit comments

Comments
 (0)