Skip to content

Commit 1e54f8d

Browse files
committed
Simplify public API
1 parent 59ac094 commit 1e54f8d

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
2828
### Removed
2929

3030
- **[BC Break]** `OrderedList` is remove, use `OuterList` instead.
31+
- **[BC Break]** remove the `$parameters` argument from all `Item` named constuctore except from `Item::from`.
3132

3233
## [0.7.0] - 2023-02-06
3334

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,14 @@ use Bakame\Http\StructuredFields\Item;
272272
use Bakame\Http\StructuredFields\Value;
273273

274274
Item::from($value, iterable<string, Value> $associativeParameters = []): self;
275-
Item::fromDecodedByteSequence(string $value, iterable<string, Value> $associativeParameters = []): self;
276-
Item::fromEncodedByteSequence(string $value, iterable<string, Value> $associativeParameters = []): self;
277-
Item::fromTimestamp(int $value, iterable<string, Value> $associativeParameters = []): self;
278-
Item::fromDateFormat(string $dateFormat, string $dateString, iterable<string, Value> $associativeParameters = []): self;
279-
Item::fromPair(array{0:mixed, 1:iterable<array{0:string, 1:StructuredField}>} $pair): self;
275+
Item::fromPair(array{0:mixed, 1:iterable<array{0:string, 1:Value}>} $pair): self;
276+
Item::fromDecodedByteSequence(string $value): self;
277+
Item::fromEncodedByteSequence(string $value): self;
278+
Item::fromToken(string $value): self;
279+
Item::fromDate(DateTimeInterface $value): self;
280+
Item::fromTimestamp(int $value): self;
281+
Item::fromDateFormat(string $dateFormat, string $dateString): self;
282+
Item::fromDateFormat(string $dateString, DateTimeZone|string|null $timezone): self;
280283
```
281284

282285
### Accessing members of Structured Fields Containers.

src/Item.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use DateTimeImmutable;
88
use DateTimeInterface;
9+
use DateTimeZone;
910
use Stringable;
11+
use Throwable;
1012
use function count;
1113
use function is_bool;
1214
use function is_float;
@@ -65,59 +67,68 @@ public static function fromHttpValue(Stringable|string $httpValue): self
6567

6668
/**
6769
* Returns a new instance from an encoded byte sequence and an iterable of key-value parameters.
68-
*
69-
* @param iterable<string,Value|DataType> $parameters
7070
*/
71-
public static function fromEncodedByteSequence(Stringable|string $value, iterable $parameters = []): self
71+
public static function fromEncodedByteSequence(Stringable|string $value): self
7272
{
73-
return self::from(ByteSequence::fromEncoded($value), $parameters);
73+
return self::from(ByteSequence::fromEncoded($value));
7474
}
7575

7676
/**
7777
* Returns a new instance from a decoded byte sequence and an iterable of key-value parameters.
78-
*
79-
* @param iterable<string,Value|DataType> $parameters
8078
*/
81-
public static function fromDecodedByteSequence(Stringable|string $value, iterable $parameters = []): self
79+
public static function fromDecodedByteSequence(Stringable|string $value): self
8280
{
83-
return self::from(ByteSequence::fromDecoded($value), $parameters);
81+
return self::from(ByteSequence::fromDecoded($value));
8482
}
8583

8684
/**
8785
* Returns a new instance from a Token and an iterable of key-value parameters.
88-
*
89-
* @param iterable<string,Value|DataType> $parameters
9086
*/
91-
public static function fromToken(Stringable|string $value, iterable $parameters = []): self
87+
public static function fromToken(Stringable|string $value): self
9288
{
93-
return self::from(Token::fromString($value), $parameters);
89+
return self::from(Token::fromString($value));
9490
}
9591

9692
/**
9793
* Returns a new instance from a timestamp and an iterable of key-value parameters.
98-
*
99-
* @param iterable<string,Value|DataType> $parameters
10094
*/
101-
public static function fromTimestamp(int $timestamp, iterable $parameters = []): self
95+
public static function fromTimestamp(int $timestamp): self
10296
{
103-
return self::from((new DateTimeImmutable())->setTimestamp($timestamp), $parameters);
97+
return self::from((new DateTimeImmutable())->setTimestamp($timestamp));
10498
}
10599

106100
/**
107101
* Returns a new instance from a date format its date string representation and an iterable of key-value parameters.
108102
*
109-
* @param iterable<string,Value|DataType> $parameters
110-
*
111-
* @throws SyntaxError if the fornat is
103+
* @throws SyntaxError if the format is invalid
112104
*/
113-
public static function fromDateFormat(string $format, string $dateString, iterable $parameters = []): self
105+
public static function fromDateFormat(string $format, string $dateString): self
114106
{
115-
$date = DateTimeImmutable::createFromFormat($format, $dateString);
116-
if (false === $date) {
107+
$value = DateTimeImmutable::createFromFormat($format, $dateString);
108+
if (false === $value) {
117109
throw new SyntaxError('The date notation `'.$dateString.'` is incompatible with the date format `'.$format.'`.');
118110
}
119111

120-
return self::from($date, $parameters);
112+
return self::from($value);
113+
}
114+
115+
/**
116+
* Returns a new instance from a string parsable by DateTimeImmutable constructor, an optional timezone and an iterable of key-value parameters.
117+
*
118+
* @throws SyntaxError if the format is invalid
119+
*/
120+
public static function fromDateString(string $dateString, DateTimeZone|string|null $timezone = null): self
121+
{
122+
$timezone ??= date_default_timezone_get();
123+
if (!$timezone instanceof DateTimeZone) {
124+
try {
125+
$timezone = new DateTimeZone($timezone);
126+
} catch (Throwable $exception) {
127+
throw new SyntaxError('The timezone could not be instantiated.', 0, $exception);
128+
}
129+
}
130+
131+
return self::from(new DateTimeImmutable($dateString, $timezone));
121132
}
122133

123134
/**

src/ItemTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function it_instantiates_a_date(): void
121121
self::assertEquals($item, Item::from(new DateTime('2020-03-04 19:23:15')));
122122
self::assertEquals($item, Item::fromTimestamp(1583349795));
123123
self::assertEquals($item, Item::fromDateFormat(DateTimeInterface::RFC822, 'Wed, 04 Mar 20 19:23:15 +0000'));
124+
self::assertTrue($item ->value() < Item::fromDateString('-1 year')->value());
124125
self::assertSame('@1583349795', $item->toHttpValue());
125126
}
126127

0 commit comments

Comments
 (0)