Skip to content

Commit 90f7faa

Browse files
committed
Update documentation
1 parent 5f433f0 commit 90f7faa

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ echo OuterList::new(
3434
'max-age' => 2500,
3535
'secure' => true,
3636
'httponly' => true,
37-
'samesite' => BakameToken::fromString('lax'),
37+
'samesite' => Token::fromString('lax'),
3838
])
3939
)
4040
->toHttpValue();
@@ -134,7 +134,34 @@ The table below summarizes the item value type.
134134
| Byte Sequence | class `ByteSequence` | `Type::ByteSequence` |
135135
| Date | class `DateTimeImmutable` | `Type::Date` |
136136

137-
The RFC define two (2) specific data types that can not be represented by
137+
The Enum `Type` which list all available types can be use to determine the RFC type
138+
corresponding to a PHP structure using the `Type::fromValue` static method.
139+
The method will throw if the structure is not recognized Alternatively it is possible
140+
to use the `Type::tryFromValue` which will instead return `null` on unindentified type.
141+
On success both methods returns the corresponding enum `Type`.
142+
143+
```php
144+
use Bakame\Http\StructuredFields\Type;
145+
146+
echo Type::fromValue(42); // returns Type::Integer
147+
echo Type::fromValue(42.0)->name; // returns 'Decimal'
148+
echo Type::fromValue(new SplTempFileObject()); // throws TypeError
149+
echo Type::tryFromValue(new SplTempFileObject()); // returns null
150+
```
151+
152+
To ease validation a `Type::equals` method is exposed to check if the `Item` has
153+
the expected type. It can also be used to compare types.
154+
155+
```php
156+
use Bakame\Http\StructuredFields\Type;
157+
158+
$field = Item::fromHttpValue('"foo"');
159+
Type::Date->equals($field); // returns false
160+
Type::String->equals($field); // returns true;
161+
Type::Boolean->equals(Type::String); // returns false
162+
```
163+
164+
The RFC defines two (2) specific data types that can not be represented by
138165
PHP default type system, for them, we have defined two classes `Token`
139166
and `ByteSequence` to help with their representation.
140167

@@ -183,8 +210,6 @@ use Bakame\Http\StructuredFields\Type;
183210
$item = Item::fromHttpValue('@1234567890');
184211
$item->type(); // return Type::Date;
185212
$item->value() // return the equivalent to DateTimeImmutable('@1234567890');
186-
// you can also do
187-
Type::Date->equals($item); // returns true
188213
```
189214

190215
#### Containers

0 commit comments

Comments
 (0)