|
1 | 1 | ---
|
2 |
| -title: HTTP Fields value types |
| 2 | +title: Accessing the HTTP field values |
3 | 3 | order: 4
|
4 | 4 | ---
|
5 | 5 |
|
6 |
| -# Structured Fields Values |
| 6 | +# HTTP Fields Values |
7 | 7 |
|
8 | 8 | ## Value type conversion to PHP
|
9 | 9 |
|
@@ -121,4 +121,79 @@ $displayString->type(); // returns Type::DisplayString
|
121 | 121 | > Values are not directly accessible. They can only be retrieved from an Item
|
122 | 122 | > Data type.
|
123 | 123 |
|
124 |
| -← [Parsing and Serializing](02-parsing-serializing.md) | [Item](04-item.md) → |
| 124 | +## The Item Data Type |
| 125 | + |
| 126 | +This is the structure from which you will be able to access the actual field content. |
| 127 | + |
| 128 | +### Item value |
| 129 | + |
| 130 | +The eight (8) defined value types are all attached to an `Item` object where their value and |
| 131 | +type are accessible using the following methods: |
| 132 | + |
| 133 | +```php |
| 134 | +use Bakame\Http\StructuredFields\Item; |
| 135 | + |
| 136 | +$item = Item::fromHttpValue('@1234567890'); |
| 137 | +$item->type(); // return Type::Date; |
| 138 | +$item->value() // return the equivalent to DateTimeImmutable('@1234567890'); |
| 139 | +``` |
| 140 | + |
| 141 | +The `Item` value object exposes the following named constructors to instantiate |
| 142 | +bare items (ie: item without parameters attached to them). |
| 143 | + |
| 144 | +```php |
| 145 | +use Bakame\Http\StructuredFields\Byte; |
| 146 | +use Bakame\Http\StructuredFields\Item; |
| 147 | +use Bakame\Http\StructuredFields\Token; |
| 148 | + |
| 149 | +Item:new(DateTimeInterface|Byte|Token|DisplayString|string|int|array|float|bool $value): self |
| 150 | +Item:tryNew(mixed $value): ?self |
| 151 | +Item::fromDecodedByteSequence(Stringable|string $value): self; |
| 152 | +Item::fromEncodedDisplayString(Stringable|string $value): self; |
| 153 | +Item::fromDecodedDisplayString(Stringable|string $value): self; |
| 154 | +Item::fromEncodedByteSequence(Stringable|string $value): self; |
| 155 | +Item::fromToken(Stringable|string $value): self; |
| 156 | +Item::fromString(Stringable|string $value): self; |
| 157 | +Item::fromDate(DateTimeInterface $datetime): self; |
| 158 | +Item::fromDateFormat(string $format, string $datetime): self; |
| 159 | +Item::fromDateString(string $datetime, DateTimeZone|string|null $timezone = null): self; |
| 160 | +Item::fromTimestamp(int $value): self; |
| 161 | +Item::fromDecimal(int|float $value): self; |
| 162 | +Item::fromInteger(int|float $value): self; |
| 163 | +Item::true(): self; |
| 164 | +Item::false(): self; |
| 165 | +``` |
| 166 | + |
| 167 | +To update the `Item` instance value, use the `withValue` method: |
| 168 | + |
| 169 | +```php |
| 170 | +use Bakame\Http\StructuredFields\Item; |
| 171 | + |
| 172 | +Item::withValue(DateTimeInterface|Byte|Token|DisplayString|string|int|float|bool $value): static |
| 173 | +``` |
| 174 | + |
| 175 | +### Item Parameters |
| 176 | + |
| 177 | +Items can have parameters attached to them. A parameter is a bere item, an item which can not have parameters |
| 178 | +attach to it, to avoid recursive behaviour. Parameters are grouped in an ordered map container called `Parameters`. |
| 179 | +They can be accessed by their indices **but also** by their required key attached to them. |
| 180 | + |
| 181 | +```php |
| 182 | + |
| 183 | +$item = Item::fromHttpValue('application/xml;q=0.9;foobar'); |
| 184 | +$item->value()->toString(); // returns 'application/xhtml+xml' |
| 185 | +$item->parameterByName(name: 'q', default: 1.0); // returns 1.0 if the parameter is not defined |
| 186 | +$item->parameterByIndex(index: 1, default: ['toto', false]); // returns ['foobar', true] because there's a parameter at index 1 |
| 187 | +$item->parameters(); // returns a Parameters instance. |
| 188 | +``` |
| 189 | + |
| 190 | +By default, you can access the member `Item` of a parameters using the following methods: |
| 191 | + |
| 192 | +- `Item::parameters` returns a `Parameters` instance; |
| 193 | +- `Item::parameterByName` returns the value of the bare item instance attached to the supplied `name`; |
| 194 | +- `Item::parameterByIndex` returns the value of the bare item instance attached to the supplied `index`; |
| 195 | + |
| 196 | +It is possible to alter and modify the `Parameters` attached to an `Item` but this section |
| 197 | +will be explored in the next section about the containers. |
| 198 | + |
| 199 | +← [Parsing and Serializing](02-parsing-serializing.md) | [Containers](04-containers.md) → |
0 commit comments