Skip to content

Commit 5f72921

Browse files
committed
rename create into new named constructor
1 parent 2b0030e commit 5f72921

File tree

10 files changed

+60
-46
lines changed

10 files changed

+60
-46
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1212
- `Item` implements the `ValueAccess` interface;
1313
- `Item::toPair` to complement `Item::fromPair`;
1414
- `Item::fromDate` to improve and complete the Item Date public API;
15+
- `Item::new` to improve Item public API;
1516
- `Item::fromAssociative` to improve Item public API;
1617
- `Item::fromString` to improve Item public API;
1718
- `Value` internal class to improve Item public API;
1819
- `Token::toString` to return the string representation of the token.
1920
- `Parameters::new` and `Dictionary::new` to return a new and empty instance
21+
- `InnerList::new` and `InnerList::new` to return a new instance
2022

2123
### Fixed
2224

@@ -32,9 +34,10 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
3234
- **[BC Break]** `InnerList::fromAssociativeParameters` use `InnerList::fromAssociative` instead.
3335
- **[BC Break]** `Value` interface use a combination of `ValueAccess` **and** `ParameterAccess` instead.
3436
- **[BC Break]** `Token::value` is no longer public use `Token::toString` instead.
35-
- **[BC Break]** `Item::from` is removed use `Item::fromAssociative` instead.
37+
- **[BC Break]** `Item::from` is removed use `Item::fromAssociative` or `Item::new` instead.
3638
- **[BC Break]** `Parameters::create` is removed use `Parameters::new` instead.
37-
- **[BC Break]** `Dictionary::create` is removed use `Dictionary::new` instead.
39+
- **[BC Break]** `InnerList::from` is removed use `InnerList::new` instead.
40+
- **[BC Break]** `OuterList::create` is removed use `OuterList::new` instead.
3841

3942
## [0.8.0] - 2023-03-12
4043

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ bare items (ie: item without parameters attached to them).
218218
```php
219219
use Bakame\Http\StructuredFields\Item;
220220

221+
Item::new(mixed $value): self;
221222
Item::fromDecodedByteSequence(Stringable|string $value): self;
222223
Item::fromEncodedByteSequence(Stringable|string $value): self;
223224
Item::fromToken(Stringable|string $value): self;
@@ -307,13 +308,13 @@ $map->remove(string ...$key): static;
307308

308309
#### Lists
309310

310-
To Create `OuterList` and `InnerList` instances you can use the `from` named constructor:
311+
To Create `OuterList` and `InnerList` instances you can use the `new` named constructor:
311312

312313
```php
313314
use Bakame\Http\StructuredFields\InnerList;
314315
use Bakame\Http\StructuredFields\Item;
315316

316-
$list = InnerList::from(
317+
$list = InnerList::new(
317318
Item::fromDecodedByteSequence('Hello World'),
318319
42.0,
319320
42
@@ -329,7 +330,7 @@ Once again, builder methods exist on both classes to ease container construction
329330
use Bakame\Http\StructuredFields\InnerList;
330331
use Bakame\Http\StructuredFields\Item;
331332

332-
$list = InnerList::from()
333+
$list = InnerList::new()
333334
->unshift('42')
334335
->push(42)
335336
->insert(1, 42.0)

src/Dictionary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static function filterMember(mixed $member): object
5353
return match (true) {
5454
$member instanceof ParameterAccess &&
5555
($member instanceof MemberList || $member instanceof ValueAccess) => $member,
56-
is_iterable($member) => InnerList::from(...$member),
56+
is_iterable($member) => InnerList::new(...$member),
5757
default => Item::fromAssociative($member),
5858
};
5959
}

src/InnerList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function fromHttpValue(Stringable|string $httpValue): self
6363
/**
6464
* Returns a new instance.
6565
*/
66-
public static function from(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
66+
public static function new(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
6767
{
6868
return new self(Parameters::new(), $members);
6969
}

src/InnerListTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function it_can_add_or_remove_members(): void
3131
{
3232
$stringItem = Item::fromAssociative('helloWorld');
3333
$booleanItem = Item::true();
34-
$instance = InnerList::from($stringItem, $booleanItem);
34+
$instance = InnerList::new($stringItem, $booleanItem);
3535

3636
self::assertCount(2, $instance);
3737
self::assertTrue($instance->has(1));
@@ -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-
$container = InnerList::from()
64+
$container = InnerList::new()
6565
->unshift('42')
6666
->push(42)
6767
->insert(1, 42.0)
@@ -76,7 +76,7 @@ public function it_can_unshift_insert_and_replace(): void
7676
#[Test]
7777
public function it_returns_the_same_object_if_nothing_is_changed(): void
7878
{
79-
$container = InnerList::from(42, 'forty-two');
79+
$container = InnerList::new(42, 'forty-two');
8080

8181
$sameContainer = $container
8282
->unshift()
@@ -91,21 +91,21 @@ public function it_fails_to_replace_invalid_index(): void
9191
{
9292
$this->expectException(InvalidOffset::class);
9393

94-
InnerList::from()->replace(0, ByteSequence::fromDecoded('Hello World'));
94+
InnerList::new()->replace(0, ByteSequence::fromDecoded('Hello World'));
9595
}
9696

9797
#[Test]
9898
public function it_fails_to_insert_at_an_invalid_index(): void
9999
{
100100
$this->expectException(InvalidOffset::class);
101101

102-
InnerList::from()->insert(3, ByteSequence::fromDecoded('Hello World'));
102+
InnerList::new()->insert(3, ByteSequence::fromDecoded('Hello World'));
103103
}
104104

105105
#[Test]
106106
public function it_fails_to_return_an_member_with_invalid_index(): void
107107
{
108-
$instance = InnerList::from();
108+
$instance = InnerList::new();
109109

110110
self::assertFalse($instance->has(3));
111111

@@ -128,7 +128,7 @@ public function it_fails_to_access_unknown_parameter_values(): void
128128
{
129129
$this->expectException(StructuredFieldError::class);
130130

131-
InnerList::from(false)->parameters()->get('bar')->value();
131+
InnerList::new(false)->parameters()->get('bar')->value();
132132
}
133133

134134
#[Test]
@@ -159,29 +159,29 @@ public function it_fails_to_insert_unknown_index_via_the_array_access_interface(
159159
{
160160
$this->expectException(StructuredFieldError::class);
161161

162-
InnerList::from()->insert(0, Item::fromAssociative(42.0));
162+
InnerList::new()->insert(0, Item::fromAssociative(42.0));
163163
}
164164

165165
#[Test]
166166
public function it_returns_the_same_object_if_no_member_is_removed(): void
167167
{
168-
self::assertCount(0, InnerList::from()->remove(0));
168+
self::assertCount(0, InnerList::new()->remove(0));
169169
}
170170

171171
#[Test]
172172
public function it_fails_to_fetch_an_value_using_an_integer(): void
173173
{
174174
$this->expectException(InvalidOffset::class);
175175

176-
InnerList::from()->get('zero');
176+
InnerList::new()->get('zero');
177177
}
178178

179179
#[Test]
180180
public function it_can_access_the_item_value(): void
181181
{
182182
$token = Token::fromString('token');
183183
$input = ['foobar', 0, false, $token];
184-
$structuredField = InnerList::from(...$input);
184+
$structuredField = InnerList::new(...$input);
185185

186186
self::assertFalse($structuredField->get(2)->value());
187187
self::assertEquals($token, $structuredField->get(-1)->value());
@@ -260,7 +260,7 @@ public function it_can_create_via_parameters_access_methods_a_new_object(): void
260260
#[Test]
261261
public function it_implements_the_array_access_interface(): void
262262
{
263-
$structuredField = InnerList::from('foobar', 'foobar', 'zero', 0);
263+
$structuredField = InnerList::new('foobar', 'foobar', 'zero', 0);
264264

265265
self::assertInstanceOf(Item::class, $structuredField->get(0));
266266
self::assertInstanceOf(Item::class, $structuredField[0]);
@@ -273,22 +273,22 @@ public function it_forbids_removing_members_using_the_array_access_interface():
273273
{
274274
$this->expectException(LogicException::class);
275275

276-
unset(InnerList::from('foobar', 'foobar', 'zero', 0)[0]);
276+
unset(InnerList::new('foobar', 'foobar', 'zero', 0)[0]);
277277
}
278278

279279
#[Test]
280280
public function it_forbids_adding_members_using_the_array_access_interface(): void
281281
{
282282
$this->expectException(LogicException::class);
283283

284-
InnerList::from('foobar', 'foobar', 'zero', 0)[0] = Item::fromAssociative(false);
284+
InnerList::new('foobar', 'foobar', 'zero', 0)[0] = Item::fromAssociative(false);
285285
}
286286

287287

288288
#[Test]
289289
public function it_can_returns_the_container_member_keys(): void
290290
{
291-
$instance = InnerList::from();
291+
$instance = InnerList::new();
292292

293293
self::assertSame([], $instance->keys());
294294

@@ -297,7 +297,7 @@ public function it_can_returns_the_container_member_keys(): void
297297

298298
self::assertSame([0, 1], $newInstance->keys());
299299

300-
$container = InnerList::from()
300+
$container = InnerList::new()
301301
->unshift('42')
302302
->push(42)
303303
->insert(1, 42.0)

src/Item.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public static function fromPair(array $pair): self
8989
return new self(new Value($pair[0]), $pair[1]);
9090
}
9191

92+
/**
93+
* Returns a new bare instance from value.
94+
*
95+
* @throws SyntaxError If the value is not valid to create a Bare Item.
96+
*/
97+
public static function new(mixed $value): self
98+
{
99+
return new self(new Value($value), Parameters::new());
100+
}
101+
92102
/**
93103
* Returns a new bare instance from value.
94104
*/

src/ItemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function provideInvalidArguments(): iterable
7777
#[DataProvider('provideFrom1stArgument')]
7878
public function it_instantiate_many_types(ValueAccess|ByteSequence|Token|DateTimeInterface|Stringable|string|int|float|bool $value, string $expected): void
7979
{
80-
self::assertSame($expected, Item::fromAssociative($value)->toHttpValue());
80+
self::assertSame($expected, Item::new($value)->toHttpValue());
8181
}
8282

8383
#[Test]

src/OuterList.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ private static function filterMember(mixed $member): object
4646
{
4747
return match (true) {
4848
$member instanceof ParameterAccess && ($member instanceof MemberList || $member instanceof ValueAccess) => $member,
49-
is_iterable($member) => InnerList::from(...$member),
49+
is_iterable($member) => InnerList::new(...$member),
5050
default => Item::fromAssociative($member),
5151
};
5252
}
5353

5454
/**
5555
* @param SfMember|SfMemberInput ...$members
5656
*/
57-
public static function from(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
57+
public static function new(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
5858
{
5959
return new self(...$members);
6060
}
@@ -66,7 +66,7 @@ public static function from(iterable|StructuredField|Token|ByteSequence|DateTime
6666
*/
6767
public static function fromHttpValue(Stringable|string $httpValue): self
6868
{
69-
return self::from(...array_map(
69+
return self::new(...array_map(
7070
fn (mixed $value) => is_array($value) ? InnerList::fromAssociative($value[0], $value[1]) : $value,
7171
Parser::parseList($httpValue)
7272
));

0 commit comments

Comments
 (0)