Skip to content

Commit 5cc190b

Browse files
committed
Adding Type Enum et Value Interface
1 parent 5817211 commit 5cc190b

15 files changed

+273
-188
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1313
- `ParameterAccess` interface updated with 4 new methods to ease parameter members modification.
1414
- `Parameter::create` named constructor to create a new instance without any parameter.
1515
- `Dictionnary::create` named constructor to create a new instance without any parameter.
16+
- `Type` Enum to list all possible Item Type supported.
17+
- `Value` Interface is introduced with `Item` being the only available implementation.
1618

1719
### Fixed
1820

@@ -29,6 +31,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
2931
### Removed
3032

3133
- **[BC Break]** `ForbiddenStateError` exception is removed, the `InvalidArgument` exception is used instead.
34+
- **[BC Break]** `Item::is*` methods are removed, the enum `Type` is used instead.
3235

3336
## [0.6.0] - 2022-11-12
3437

docs/item.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ Items can have different types that are translated to PHP using:
1010

1111
The table below summarizes the item value type.
1212

13-
| HTTP DataType | Package Data Type | validation method |
14-
|---------------|---------------------------|------------------------|
15-
| Integer | `int` | `Item::isInteger` |
16-
| Decimal | `float` | `Item::isDecimal` |
17-
| String | `string` | `Item::isString` |
18-
| Boolean | `bool` | `Item::isBoolean` |
19-
| Token | class `Token` | `Item::isToken` |
20-
| Byte Sequence | class `ByteSequence` | `Item::isByteSequence` |
21-
| Date | class `DateTimeImmutable` | `Item::isDate` |
13+
| HTTP DataType | Package Data Type | Package Enum Type |
14+
|---------------|---------------------------|----------------------|
15+
| Integer | `int` | `Type::Integer` |
16+
| Decimal | `float` | `Type::Decimal` |
17+
| String | `string` | `Tyoe::String` |
18+
| Boolean | `bool` | `Type::Boolean` |
19+
| Token | class `Token` | `Type::Token` |
20+
| Byte Sequence | class `ByteSequence` | `Type::ByteSequence` |
21+
| Date | class `DateTimeImmutable` | `Type::Date` |
2222

2323
### Token
2424

@@ -67,9 +67,8 @@ keys are strings and the value are bare items. Their public API is covered in th
6767
use Bakame\Http\StructuredFields\Item;
6868

6969
$item = Item::from("hello world", ["a" => true]);
70-
$item->value(); // returns "hello world"
71-
$item->isString(); // returns true
72-
$item->isToken(); // returns false
70+
$item->value(); // returns "hello world"
71+
$item->type(); // returns Type::String
7372
$item->parameters()['a']->value(); //returns true
7473
```
7574

@@ -97,9 +96,9 @@ $item = Item::fromPair([
9796
["a", ByteSequence::fromDecoded("Hello World")],
9897
]
9998
]);
100-
$item->value(); // returns "hello world"
101-
$item->isString(); // returns true
102-
$item->parameters()["a"]->isByteSequence(); // returns true
99+
$item->value(); // returns "hello world"
100+
$item->type(); // returns Type::String
101+
$item->parameters()["a"]->type(); // returns Type::ByteSequence
103102
$item->parameters()["a"]->value(); // returns StructuredFields\ByteSequence::fromDecoded('Hello World');
104103
echo $item->toHttpValue(); // returns "hello world";a=:SGVsbG8gV29ybGQ=:
105104
```
@@ -121,8 +120,8 @@ use Bakame\Http\StructuredFields\ByteSequence;
121120
use Bakame\Http\StructuredFields\Item;
122121

123122
$item = Item::from(ByteSequence::fromEncoded("SGVsbG8gV29ybGQ="));
124-
$item->isByteSequence(); // returns true
125-
echo $item->value(); // returns StructuredFields\ByteSequence::fromEncoded("SGVsbG8gV29ybGQ=");
123+
$item->type(); // returns Type::ByteSequence
124+
echo $item->value(); // returns StructuredFields\ByteSequence::fromEncoded("SGVsbG8gV29ybGQ=");
126125
```
127126

128127
**Of note: to instantiate a decimal number type a float MUST be used as the first argument of `Item::from`.**
@@ -131,10 +130,8 @@ echo $item->value(); // returns StructuredFields\ByteSequence::fromEncoded("
131130
use Bakame\Http\StructuredFields\Item;
132131

133132
$decimal = Item::from(42.0);
134-
$decimal->isDecimal(); //return true
135-
$decimal->isInteger(); //return false
133+
$decimal->type(); //Type::Decimal
136134

137-
$item = Item::fromPair([42]);
138-
$item->isDecimal(); //return false
139-
$item->isInteger(); //return true
135+
$integer = Item::fromPair([42]);
136+
$integer->type(); //return Type::Integer
140137
```

src/Dictionary.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
use function is_array;
1616

1717
/**
18-
* @implements MemberOrderedMap<string, Item|InnerList<int, Item>>
18+
* @implements MemberOrderedMap<string, Value|InnerList<int, Value>>
1919
* @phpstan-import-type DataType from Item
2020
*/
2121
final class Dictionary implements MemberOrderedMap
2222
{
23-
/** @var array<string, Item|InnerList<int, Item>> */
23+
/** @var array<string, Value|InnerList<int, Value>> */
2424
private array $members = [];
2525

2626
/**
27-
* @param iterable<string, InnerList<int, Item>|Item|DataType> $members
27+
* @param iterable<string, InnerList<int, Value>|Value|DataType> $members
2828
*/
2929
private function __construct(iterable $members = [])
3030
{
@@ -47,7 +47,7 @@ public static function create(): self
4747
* its keys represent the dictionary entry key
4848
* its values represent the dictionary entry value
4949
*
50-
* @param iterable<string, InnerList<int, Item>|Item|DataType> $members
50+
* @param iterable<string, InnerList<int, Value>|Value|DataType> $members
5151
*/
5252
public static function fromAssociative(iterable $members): self
5353
{
@@ -61,7 +61,7 @@ public static function fromAssociative(iterable $members): self
6161
* the first member represents the instance entry key
6262
* the second member represents the instance entry value
6363
*
64-
* @param MemberOrderedMap<string, Item|InnerList<int, Item>>|iterable<array{0:string, 1:InnerList<int, Item>|Item|DataType}> $pairs
64+
* @param MemberOrderedMap<string, Value|InnerList<int, Value>>|iterable<array{0:string, 1:InnerList<int, Value>|Value|DataType}> $pairs
6565
*/
6666
public static function fromPairs(MemberOrderedMap|iterable $pairs): self
6767
{
@@ -94,8 +94,8 @@ public static function fromHttpValue(Stringable|string $httpValue): self
9494

9595
public function toHttpValue(): string
9696
{
97-
$formatter = static fn (Item|InnerList $member, string $key): string => match (true) {
98-
$member instanceof Item && true === $member->value() => $key.$member->parameters()->toHttpValue(),
97+
$formatter = static fn (Value|InnerList $member, string $key): string => match (true) {
98+
$member instanceof Value && true === $member->value() => $key.$member->parameters()->toHttpValue(),
9999
default => $key.'='.$member->toHttpValue(),
100100
};
101101

@@ -118,15 +118,15 @@ public function hasMembers(): bool
118118
}
119119

120120
/**
121-
* @return Iterator<string, Item|InnerList<int, Item>>
121+
* @return Iterator<string, Value|InnerList<int, Value>>
122122
*/
123123
public function getIterator(): Iterator
124124
{
125125
yield from $this->members;
126126
}
127127

128128
/**
129-
* @return Iterator<array{0:string, 1:Item|InnerList<int, Item>}>
129+
* @return Iterator<array{0:string, 1:Value|InnerList<int, Value>}>
130130
*/
131131
public function toPairs(): Iterator
132132
{
@@ -152,7 +152,7 @@ public function has(string|int $offset): bool
152152
* @throws SyntaxError If the key is invalid
153153
* @throws InvalidOffset If the key is not found
154154
*/
155-
public function get(string|int $offset): Item|InnerList
155+
public function get(string|int $offset): Value|InnerList
156156
{
157157
if (is_int($offset) || !array_key_exists($offset, $this->members)) {
158158
throw InvalidOffset::dueToKeyNotFound($offset);
@@ -189,7 +189,7 @@ private function filterIndex(int $index): int
189189
/**
190190
* @throws InvalidOffset If the key is not found
191191
*
192-
* @return array{0:string, 1:Item|InnerList<int, Item>}
192+
* @return array{0:string, 1:Value|InnerList<int, Value>}
193193
*/
194194
public function pair(int $index): array
195195
{
@@ -216,11 +216,11 @@ public function set(string $key, StructuredField|Token|ByteSequence|DateTimeInte
216216
return $this;
217217
}
218218

219-
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Item
219+
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): InnerList|Value
220220
{
221221
return match (true) {
222-
$member instanceof InnerList, $member instanceof Item => $member,
223-
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Item::class.'" or a "'.InnerList::class.'" instance; received a "'.$member::class.'" instead.'),
222+
$member instanceof InnerList, $member instanceof Value => $member,
223+
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Value::class.'" or a "'.InnerList::class.'" instance; received a "'.$member::class.'" instead.'),
224224
default => Item::from($member),
225225
};
226226
}
@@ -258,7 +258,7 @@ public function prepend(string $key, StructuredField|Token|ByteSequence|DateTime
258258
}
259259

260260
/**
261-
* @param iterable<string, InnerList<int, Item>|Item|DataType> ...$others
261+
* @param iterable<string, InnerList<int, Value>|Value|DataType> ...$others
262262
*/
263263
public function mergeAssociative(iterable ...$others): self
264264
{
@@ -270,7 +270,7 @@ public function mergeAssociative(iterable ...$others): self
270270
}
271271

272272
/**
273-
* @param MemberOrderedMap<string, Item|InnerList<int, Item>>|iterable<array{0:string, 1:InnerList<int, Item>|Item|DataType}> ...$others
273+
* @param MemberOrderedMap<string, Value|InnerList<int, Value>>|iterable<array{0:string, 1:InnerList<int, Value>|Value|DataType}> ...$others
274274
*/
275275
public function mergePairs(MemberOrderedMap|iterable ...$others): self
276276
{
@@ -292,9 +292,9 @@ public function offsetExists(mixed $offset): bool
292292
/**
293293
* @param string $offset
294294
*
295-
* @return Item|InnerList<int, Item>
295+
* @return Value|InnerList<int, Value>
296296
*/
297-
public function offsetGet(mixed $offset): InnerList|Item
297+
public function offsetGet(mixed $offset): InnerList|Value
298298
{
299299
return $this->get($offset);
300300
}
@@ -308,7 +308,7 @@ public function offsetUnset(mixed $offset): void
308308
}
309309

310310
/**
311-
* @param InnerList<int, Item>|Item|DataType $value
311+
* @param InnerList<int, Value>|Value|DataType $value
312312
*/
313313
public function offsetSet(mixed $offset, mixed $value): void
314314
{

src/InnerList.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
use function count;
1515

1616
/**
17-
* @implements MemberList<int, Item>
17+
* @implements MemberList<int, Value>
1818
* @phpstan-import-type DataType from Item
1919
*/
2020
final class InnerList implements MemberList, ParameterAccess
2121
{
22-
/** @var list<Item> */
22+
/** @var list<Value> */
2323
private array $members;
2424

2525
/**
26-
* @param iterable<Item|DataType> $members
26+
* @param iterable<Value|DataType> $members
2727
*/
2828
private function __construct(private readonly Parameters $parameters, iterable $members)
2929
{
@@ -33,14 +33,14 @@ private function __construct(private readonly Parameters $parameters, iterable $
3333
/**
3434
* Returns a new instance.
3535
*/
36-
public static function from(Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
36+
public static function from(Value|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members): self
3737
{
3838
return new self(Parameters::create(), $members);
3939
}
4040

4141
/**
42-
* @param iterable<Item|DataType> $members
43-
* @param iterable<string, Item|DataType> $parameters
42+
* @param iterable<Value|DataType> $members
43+
* @param iterable<string, Value|DataType> $parameters
4444
*/
4545
public static function fromList(iterable $members = [], iterable $parameters = []): self
4646
{
@@ -57,12 +57,12 @@ public function parameters(): Parameters
5757
return clone $this->parameters;
5858
}
5959

60-
public function prependParameter(string $key, Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
60+
public function prependParameter(string $key, Value|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
6161
{
6262
return $this->withParameters($this->parameters()->prepend($key, $member));
6363
}
6464

65-
public function appendParameter(string $key, Item|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
65+
public function appendParameter(string $key, Value|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): static
6666
{
6767
return $this->withParameters($this->parameters()->append($key, $member));
6868
}
@@ -88,7 +88,7 @@ public function withParameters(Parameters $parameters): static
8888

8989
public function toHttpValue(): string
9090
{
91-
return '('.implode(' ', array_map(fn (Item $value): string => $value->toHttpValue(), $this->members)).')'.$this->parameters->toHttpValue();
91+
return '('.implode(' ', array_map(fn (Value $value): string => $value->toHttpValue(), $this->members)).')'.$this->parameters->toHttpValue();
9292
}
9393

9494
public function count(): int
@@ -107,7 +107,7 @@ public function hasMembers(): bool
107107
}
108108

109109
/**
110-
* @return Iterator<array-key, Item>
110+
* @return Iterator<array-key, Value>
111111
*/
112112
public function getIterator(): Iterator
113113
{
@@ -130,7 +130,7 @@ private function filterIndex(int $index): int|null
130130
};
131131
}
132132

133-
public function get(string|int $offset): Item
133+
public function get(string|int $offset): Value
134134
{
135135
if (!is_int($offset)) {
136136
throw InvalidOffset::dueToIndexNotFound($offset);
@@ -164,11 +164,11 @@ public function push(StructuredField|Token|ByteSequence|DateTimeInterface|String
164164
return $this;
165165
}
166166

167-
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Item
167+
private static function filterMember(StructuredField|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool $member): Value
168168
{
169169
return match (true) {
170-
$member instanceof Item => $member,
171-
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Item::class.'" instance; received a "'.$member::class.'" instead.'),
170+
$member instanceof Value => $member,
171+
$member instanceof StructuredField => throw new InvalidArgument('Expecting a "'.Value::class.'" instance; received a "'.$member::class.'" instead.'),
172172
default => Item::from($member),
173173
};
174174
}
@@ -237,7 +237,7 @@ public function offsetExists($offset): bool
237237
/**
238238
* @param int $offset
239239
*/
240-
public function offsetGet($offset): Item
240+
public function offsetGet($offset): Value
241241
{
242242
return $this->get($offset);
243243
}
@@ -251,7 +251,7 @@ public function offsetUnset($offset): void
251251
}
252252

253253
/**
254-
* @param Item|DataType $value the member to add
254+
* @param Value|DataType $value the member to add
255255
*
256256
* @see ::push
257257
* @see ::replace

0 commit comments

Comments
 (0)