Skip to content

Commit 8f13d4f

Browse files
committed
Update http structured fields package
1 parent eb5bfa8 commit 8f13d4f

File tree

5 files changed

+63
-39
lines changed

5 files changed

+63
-39
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1111
- `Token::tryFromString`
1212
- `OuterList::fromPairs`
1313
- `DataType` is now part of the public API
14+
- `Type::fromVariable`
15+
- `Type::tryFromVariable`
16+
- the `Type` enum is now a baked string Enum.
1417

1518
### Fixed
1619

@@ -19,7 +22,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1922

2023
### Deprecated
2124

22-
- None
25+
- `Type::fromValue` use `Type::fromVariable` instead
26+
- `Type::tryFromValue` use `Type::tryFromVariable` instead
2327

2428
### Removed
2529

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use Bakame\Http\StructuredFields\DataType;
1717
use Bakame\Http\StructuredFields\Token;
1818

1919
//1 - parsing an Accept Header
20-
$headerValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
21-
$field = DataType::List->parse($headerValue);
20+
$fieldValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8';
21+
$field = DataType::List->parse($fieldValue);
2222
$field[2]->value()->toString(); // returns 'application/xml'
2323
$field[2]->parameter('q'); // returns (float) 0.9
2424
$field[0]->value()->toString(); // returns 'text/html'
@@ -69,11 +69,11 @@ header. Content validation is out of scope for this library.
6969
> [!NOTE]
7070
> New in version 1.2.0
7171
72-
The `DataType` enum serves as a factory class to quickly parse and build a structured field.
73-
The enum list all available data type according to the RFC. To parse a header you need to
74-
give the `parse` method a string or a stringable object. On success, it will return a
75-
`Bakame\Http\StruncturedFields\StruncturedField` implementing object otherwise an
76-
exception will be thrown.
72+
The `DataType` enum list all available data type according to the RFC. It is also a
73+
Factory to enable parsing and building such data types. To parse a header you need
74+
to give the `parse` method a string or a stringable object. On success, it will
75+
return a `Bakame\Http\StruncturedFields\StruncturedField` implementing object
76+
otherwise an exception will be thrown.
7777

7878
```php
7979
$headerLine = 'bar;baz=42'; //the raw header line is a structured field item
@@ -103,14 +103,12 @@ echo DataType::List->build([
103103
// display "dumela lefatshe";a=?0, ("a" "b" @1703319068);a
104104
```
105105

106-
The data type can be given as a string or using the `DataType` enum.
107-
108106
#### Using specific named constructor
109107

110-
The package provides specific classes for each data type. if you do not wish to
111-
use the `DataType` factoring, parsing the header value is done via the
112-
`fromHttpValue` named constructor. The method is attached to each library's
113-
structured fields representation as shown below:
108+
To complement the factory and to allow for more fine-grained manipulations, the package
109+
provides specific classes for each data type. if you do not wish to use the `DataType`
110+
factoring, parsing the header value is done via the `fromHttpValue` named constructor.
111+
The method is attached to each library's structured fields representation as shown below:
114112

115113
```php
116114
declare(strict_types=1);
@@ -230,18 +228,18 @@ The table below summarizes the item value type.
230228
| Date | class `DateTimeImmutable` | `Type::Date` |
231229
| DisplayString | class `DisplayString` | `Type::DisplayString` |
232230

233-
The Enum `Type` which list all available types can be use to determine the RFC type
234-
corresponding to a PHP structure using the `Type::fromValue` static method.
231+
The Enum `Type` which list all available types can be used to determine the RFC type
232+
corresponding to a PHP structure using the `Type::fromVariable` static method.
235233
The method will throw if the structure is not recognized Alternatively it is possible
236234
to use the `Type::tryFromValue` which will instead return `null` on unindentified type.
237235
On success both methods returns the corresponding enum `Type`.
238236

239237
```php
240238
use Bakame\Http\StructuredFields\Type;
241239

242-
echo Type::fromValue(42); // returns Type::Integer
243-
echo Type::fromValue(42.0)->name; // returns 'Decimal'
244-
echo Type::fromValue(new SplTempFileObject()); // throws InvalidArgument
240+
echo Type::fromVariable(42)->value; // returns 'integer'
241+
echo Type::fromVariable(42.0)->name; // returns 'Decimal'
242+
echo Type::fromVariable(new SplTempFileObject()); // throws InvalidArgument
245243
echo Type::tryFromValue(new SplTempFileObject()); // returns null
246244
```
247245

src/Type.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,51 @@ public function equals(mixed $other): bool
3131
/**
3232
* @throws InvalidArgument if the value can not be resolved into a supported HTTP structured field data type
3333
*/
34-
public static function fromValue(mixed $value): self
34+
public static function fromVariable(mixed $value): self
3535
{
36-
return self::tryFromValue($value) ?? throw new InvalidArgument((is_object($value) ? 'An instance of "'.$value::class.'"' : 'A value of type "'.gettype($value).'"').' can not be used as an HTTP structured field data type.');
36+
return self::tryFromVariable($value) ?? throw new InvalidArgument((is_object($value) ? 'An instance of "'.$value::class.'"' : 'A value of type "'.gettype($value).'"').' can not be used as an HTTP structured field data type.');
3737
}
3838

39-
public static function tryFromValue(mixed $value): self|null
39+
public static function tryFromVariable(mixed $variable): self|null
4040
{
4141
return match (true) {
42-
$value instanceof ValueAccess,
43-
$value instanceof Token,
44-
$value instanceof DisplayString,
45-
$value instanceof ByteSequence => $value->type(),
46-
$value instanceof DateTimeInterface => Type::Date,
47-
is_int($value) => Type::Integer,
48-
is_float($value) => Type::Decimal,
49-
is_bool($value) => Type::Boolean,
50-
is_string($value) => match (true) {
51-
null !== Token::tryFromString($value) => Type::Token,
52-
null !== ByteSequence::tryFromEncoded($value) => Type::ByteSequence,
53-
1 === preg_match('/[^\x20-\x7f]/', $value) => Type::DisplayString,
42+
$variable instanceof ValueAccess,
43+
$variable instanceof Token,
44+
$variable instanceof DisplayString,
45+
$variable instanceof ByteSequence => $variable->type(),
46+
$variable instanceof DateTimeInterface => Type::Date,
47+
is_int($variable) => Type::Integer,
48+
is_float($variable) => Type::Decimal,
49+
is_bool($variable) => Type::Boolean,
50+
is_string($variable) => match (true) {
51+
null !== Token::tryFromString($variable) => Type::Token,
52+
null !== ByteSequence::tryFromEncoded($variable) => Type::ByteSequence,
53+
1 === preg_match('/[^\x20-\x7f]/', $variable) => Type::DisplayString,
5454
default => Type::String,
5555
},
5656
default => null,
5757
};
5858
}
59+
60+
/**
61+
* @deprecated since version 1.2.0 will be removed in the next major release.
62+
* @see Type::fromVariable()
63+
* @codeCoverageIgnore
64+
*
65+
* @throws InvalidArgument if the value can not be resolved into a supported HTTP structured field data type
66+
*/
67+
public static function fromValue(mixed $value): self
68+
{
69+
return self::fromVariable($value);
70+
}
71+
72+
/**
73+
* @deprecated since version 1.2.0 will be removed in the next major release.
74+
* @see Type::tryFromVariable()
75+
* @codeCoverageIgnore
76+
*/
77+
public static function tryFromValue(mixed $value): self|null
78+
{
79+
return self::tryFromVariable($value);
80+
}
5981
}

src/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(ValueAccess|Token|ByteSequence|DisplayString|DateTim
4747
is_float($value) => self::filterDecimal($value),
4848
default => self::filterString($value),
4949
};
50-
$this->type = Type::fromValue($value);
50+
$this->type = Type::fromVariable($value);
5151
}
5252

5353
/**

tests/TypeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ final class TypeTest extends TestCase
1515
#[Test]
1616
public function it_will_return_null_if_the_type_is_no_supported(): void
1717
{
18-
self::assertNull(Type::tryFromValue([]));
18+
self::assertNull(Type::tryFromVariable([]));
1919
}
2020

2121
#[Test]
2222
public function it_will_return_true_if_the_type_is_valid_but_its_value_is_invalid(): void
2323
{
24-
self::assertTrue(Type::Integer->equals(Type::tryFromValue(1_000_000_000_000_000)));
24+
self::assertTrue(Type::Integer->equals(Type::tryFromVariable(1_000_000_000_000_000)));
2525
}
2626

2727
#[Test]
@@ -35,8 +35,8 @@ public function it_can_tell_the_item_type_from_a_value_instance(): void
3535
#[DataProvider('itemTypeProvider')]
3636
public function it_can_tell_the_item_type(mixed $value, Type $expectedType): void
3737
{
38-
self::assertTrue($expectedType->equals(Type::fromValue($value)));
39-
self::assertTrue($expectedType->equals(Type::tryFromValue($value)));
38+
self::assertTrue($expectedType->equals(Type::fromVariable($value)));
39+
self::assertTrue($expectedType->equals(Type::tryFromVariable($value)));
4040
}
4141

4242
/**

0 commit comments

Comments
 (0)