Skip to content

Commit 9cbf915

Browse files
committed
Adding conditionable when to DataType
1 parent 147b977 commit 9cbf915

File tree

7 files changed

+114
-26
lines changed

7 files changed

+114
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
99
- `Bytes` class replace the `ByteSequence` class.
1010
- `Ietf` enum.
1111
- methods `fromRFC9651`, `fromRfc8941`, `toRFC9651`, `toRfc8941`, to ease parsing/serializing Structured Fields.
12-
- method `equals` to allow comparison of Structured Fields values.
12+
- method `equals`, to allow comparison of Structured Fields DataType.
13+
- method `when` to allow conditional addition to Structured Field DataType.
1314
- methods `map`, `reduce`, `filter`, `sort` to all containers classes.
1415
- methods `isEmpty`, `isNotEmpty` to all containers classes.
1516
- methods `getByIndex`, `hasIndices`, `indices` to all containers classes.

src/Dictionary.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
99
use CallbackFilterIterator;
10+
use Closure;
1011
use Countable;
1112
use DateTimeInterface;
1213
use Iterator;
@@ -239,6 +240,25 @@ public function equals(mixed $other): bool
239240
return $other instanceof self && $other->toHttpValue() === $this->toHttpValue();
240241
}
241242

243+
/**
244+
* @template TWhenParameter
245+
*
246+
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
247+
* @param callable($this, TWhenParameter): ($this|null) $callback
248+
*/
249+
public function when($value, callable $callback): self
250+
{
251+
if ($value instanceof Closure) {
252+
$value = $value($this);
253+
}
254+
255+
if (!$value) {
256+
return $this;
257+
}
258+
259+
return $callback($this, $value) ?? $this;
260+
}
261+
242262
public function count(): int
243263
{
244264
return count($this->members);
@@ -580,16 +600,13 @@ public function removeByNames(string ...$names): self
580600
* This method MUST retain the state of the current instance, and return
581601
* an instance that contains the specified changes.
582602
*
583-
* @param SfMemberInput|null $member
603+
* @param SfMemberInput $member
584604
* @throws SyntaxError If the string name is not a valid
585605
*/
586606
public function append(
587607
string $name,
588-
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
608+
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
589609
): self {
590-
if (null === $member) {
591-
return $this;
592-
}
593610
$members = $this->members;
594611
unset($members[$name]);
595612

@@ -602,17 +619,14 @@ public function append(
602619
* This method MUST retain the state of the current instance, and return
603620
* an instance that contains the specified changes.
604621
*
605-
* @param SfMemberInput|null $member
622+
* @param SfMemberInput $member
606623
*
607624
* @throws SyntaxError If the string name is not a valid
608625
*/
609626
public function prepend(
610627
string $name,
611-
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
628+
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
612629
): self {
613-
if (null === $member) {
614-
return $this;
615-
}
616630
$members = $this->members;
617631
unset($members[$name]);
618632

src/InnerList.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
9+
use Closure;
910
use Countable;
1011
use DateTimeInterface;
1112
use Iterator;
@@ -181,6 +182,25 @@ public function equals(mixed $other): bool
181182
return $other instanceof self && $other->toHttpValue() === $this->toHttpValue();
182183
}
183184

185+
/**
186+
* @template TWhenParameter
187+
*
188+
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
189+
* @param callable($this, TWhenParameter): ($this|null) $callback
190+
*/
191+
public function when($value, callable $callback): self
192+
{
193+
if ($value instanceof Closure) {
194+
$value = $value($this);
195+
}
196+
197+
if (!$value) {
198+
return $this;
199+
}
200+
201+
return $callback($this, $value) ?? $this;
202+
}
203+
184204
/**
185205
* @return array{0:list<Item>, 1:Parameters}
186206
*/

src/Item.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Bakame\Http\StructuredFields\Validation\Violation;
8+
use Closure;
89
use DateTimeImmutable;
910
use DateTimeInterface;
1011
use DateTimeZone;
@@ -366,6 +367,25 @@ public function equals(mixed $other): bool
366367
return $other instanceof self && $other->toHttpValue() === $this->toHttpValue();
367368
}
368369

370+
/**
371+
* @template TWhenParameter
372+
*
373+
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
374+
* @param callable($this, TWhenParameter): ($this|null) $callback
375+
*/
376+
public function when($value, callable $callback): self
377+
{
378+
if ($value instanceof Closure) {
379+
$value = $value($this);
380+
}
381+
382+
if (!$value) {
383+
return $this;
384+
}
385+
386+
return $callback($this, $value) ?? $this;
387+
}
388+
369389
/**
370390
* Returns a new instance with the newly associated value.
371391
*

src/OuterList.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
9+
use Closure;
910
use Countable;
1011
use DateTimeInterface;
1112
use Iterator;
@@ -187,6 +188,25 @@ public function equals(mixed $other): bool
187188
return $other instanceof self && $other->toHttpValue() === $this->toHttpValue();
188189
}
189190

191+
/**
192+
* @template TWhenParameter
193+
*
194+
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
195+
* @param callable($this, TWhenParameter): ($this|null) $callback
196+
*/
197+
public function when($value, callable $callback): self
198+
{
199+
if ($value instanceof Closure) {
200+
$value = $value($this);
201+
}
202+
203+
if (!$value) {
204+
return $this;
205+
}
206+
207+
return $callback($this, $value) ?? $this;
208+
}
209+
190210
public function getIterator(): Iterator
191211
{
192212
yield from $this->members;

src/ParameterAccess.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ public function addParameter(
9393
* This method MUST retain the state of the current instance, and return
9494
* an instance that contains the specified parameter change.
9595
*
96-
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
96+
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
9797
*
9898
* @throws SyntaxError If the string name is not a valid
9999
*/
100100
public function prependParameter(
101101
string $name,
102-
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
102+
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
103103
): static {
104104
return $this->withParameters($this->parameters()->prepend($name, $member));
105105
}
@@ -110,13 +110,13 @@ public function prependParameter(
110110
* This method MUST retain the state of the current instance, and return
111111
* an instance that contains the specified parameter change.
112112
*
113-
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
113+
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
114114
*
115115
* @throws SyntaxError If the string name is not a valid
116116
*/
117117
public function appendParameter(
118118
string $name,
119-
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
119+
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
120120
): static {
121121
return $this->withParameters($this->parameters()->append($name, $member));
122122
}

src/Parameters.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
99
use CallbackFilterIterator;
10+
use Closure;
1011
use Countable;
1112
use DateTimeImmutable;
1213
use DateTimeInterface;
@@ -180,6 +181,25 @@ public function equals(mixed $other): bool
180181
return $other instanceof self && $other->toHttpValue() === $this->toHttpValue();
181182
}
182183

184+
/**
185+
* @template TWhenParameter
186+
*
187+
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
188+
* @param callable($this, TWhenParameter): ($this|null) $callback
189+
*/
190+
public function when($value, callable $callback): self
191+
{
192+
if ($value instanceof Closure) {
193+
$value = $value($this);
194+
}
195+
196+
if (!$value) {
197+
return $this;
198+
}
199+
200+
return $callback($this, $value) ?? $this;
201+
}
202+
183203
public function count(): int
184204
{
185205
return count($this->members);
@@ -543,32 +563,25 @@ public function removeByKeys(string ...$names): self
543563
}
544564

545565
/**
546-
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
566+
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
547567
*/
548568
public function append(
549569
string $name,
550-
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
570+
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
551571
): self {
552-
if (null === $member) {
553-
return $this;
554-
}
555-
556572
$members = $this->members;
557573
unset($members[$name]);
558574

559575
return $this->newInstance([...$members, MapKey::from($name)->value => self::filterMember($member)]);
560576
}
561577

562578
/**
563-
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
579+
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType $member
564580
*/
565581
public function prepend(
566582
string $name,
567-
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool|null $member
583+
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|Bytes|DisplayString|DateTimeInterface|string|int|float|bool $member
568584
): self {
569-
if (null === $member) {
570-
return $this;
571-
}
572585
$members = $this->members;
573586
unset($members[$name]);
574587

0 commit comments

Comments
 (0)