Skip to content

Commit ea25a65

Browse files
committed
Adding Date related named constructors
1 parent 77afe49 commit ea25a65

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

CHANGELOG.md

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

77
### Added
88

9+
- `Item::fromTimestamp`, `Item::fromDateFormat` to improve item instantiation with dates.
910
- `ParameterAccess::parameter` to ease parameter members value access.
1011
- **[BC Break]** `OrderedList` is renamed `OuterList`.
1112
- **[BC Break]** `MemberContainer::remove` methods get added to the interface.

src/InnerList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function withParameters(Parameters $parameters): static
115115
return $this;
116116
}
117117

118-
return new self($parameters, $this->members);
118+
return new static($parameters, $this->members);
119119
}
120120

121121
public function toHttpValue(): string

src/Item.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ public static function fromToken(Stringable|string $value, iterable $parameters
9393
return self::from(Token::fromString($value), $parameters);
9494
}
9595

96+
/**
97+
* Returns a new instance from a timestamp and an iterable of key-value parameters.
98+
*
99+
* @param iterable<string,Value|DataType> $parameters
100+
*/
101+
public static function fromTimestamp(int $timestamp, iterable $parameters = []): self
102+
{
103+
return self::from((new DateTimeImmutable())->setTimestamp($timestamp), $parameters);
104+
}
105+
106+
/**
107+
* Returns a new instance from a date format its date string representation and an iterable of key-value parameters.
108+
*
109+
* @param iterable<string,Value|DataType> $parameters
110+
*
111+
* @throws SyntaxError if the fornat is
112+
*/
113+
public static function fromDateFormat(string $format, string $dateString, iterable $parameters = []): self
114+
{
115+
$date = DateTimeImmutable::createFromFormat($format, $dateString);
116+
if (false === $date) {
117+
throw new SyntaxError('The date notation `'.$dateString.'` is incompatible with the date format `'.$format.'`.');
118+
}
119+
120+
return self::from($date, $parameters);
121+
}
122+
96123
/**
97124
* @param array{
98125
* 0:DataType,
@@ -251,7 +278,7 @@ public function withoutParameter(string ...$keys): static
251278

252279
public function withParameters(Parameters $parameters): static
253280
{
254-
return $this->parameters->toHttpValue() === $parameters->toHttpValue() ? $this : new self($this->value, $parameters);
281+
return $this->parameters->toHttpValue() === $parameters->toHttpValue() ? $this : new static($this->value, $parameters);
255282
}
256283

257284
/**

src/ItemTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArrayObject;
88
use DateTime;
99
use DateTimeImmutable;
10+
use DateTimeInterface;
1011
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\Attributes\Test;
1213
use Stringable;
@@ -60,7 +61,7 @@ public function it_updates_item(mixed $value, string $expected): void
6061

6162
self::assertSame(
6263
$expected.$parameters->toHttpValue(),
63-
Item::from('hello-world', $parameters)->withValue($value)->toHttpValue()
64+
(string) Item::from('hello-world', $parameters)->withValue($value)
6465
);
6566
}
6667

@@ -118,6 +119,8 @@ public function it_instantiates_a_date(): void
118119
$item = Item::fromHttpValue('@1583349795');
119120
self::assertEquals($item, Item::from(new DateTimeImmutable('2020-03-04 19:23:15')));
120121
self::assertEquals($item, Item::from(new DateTime('2020-03-04 19:23:15')));
122+
self::assertEquals($item, Item::fromTimestamp(1583349795));
123+
self::assertEquals($item, Item::fromDateFormat(DateTimeInterface::RFC822, 'Wed, 04 Mar 20 19:23:15 +0000'));
121124
self::assertSame('@1583349795', $item->toHttpValue());
122125
}
123126

@@ -137,6 +140,14 @@ public function it_fails_to_instantiate_an_out_of_range_date_in_the_future(): vo
137140
Item::from(new DateTime('@'. 1_000_000_000_000_000));
138141
}
139142

143+
#[Test]
144+
public function it_fails_to_instantiate_an_out_of_range_timestamp_in_the_future(): void
145+
{
146+
$this->expectException(SyntaxError::class);
147+
148+
Item::fromTimestamp(1_000_000_000_000_000);
149+
}
150+
140151
#[Test]
141152
public function it_fails_to_instantiate_an_out_of_range_date_in_the_past(): void
142153
{
@@ -145,6 +156,22 @@ public function it_fails_to_instantiate_an_out_of_range_date_in_the_past(): void
145156
Item::from(new DateTime('@'.-1_000_000_000_000_000));
146157
}
147158

159+
#[Test]
160+
public function it_fails_to_instantiate_an_out_of_range_timestamp_in_the_past(): void
161+
{
162+
$this->expectException(SyntaxError::class);
163+
164+
Item::fromTimestamp(-1_000_000_000_000_000);
165+
}
166+
167+
#[Test]
168+
public function it_fails_to_instantiate_an_invalid_date_format_string(): void
169+
{
170+
$this->expectException(SyntaxError::class);
171+
172+
Item::fromDateFormat(DateTimeInterface::ATOM, '2012-02-03');
173+
}
174+
148175
#[Test]
149176
public function it_instantiates_a_binary(): void
150177
{

0 commit comments

Comments
 (0)