@@ -6,10 +6,11 @@ Structured Field Values for PHP
6
6
[ ![ Build] ( https://github.com/bakame-php/http-structured-fields/workflows/build/badge.svg )] ( https://github.com/bakame-php/http-structured-fields/actions?query=workflow%3A%22build%22 )
7
7
8
8
The package uses pragmatic value objects to parse and serialize [ HTTP Structured Fields] [ 1 ] in PHP.
9
- Structured fields are intended for use by specifications of new HTTP fields that wish to use a
10
- common syntax that is more restrictive than traditional HTTP field values.
9
+ HTTP Structured fields are intended for use by specifications of new HTTP fields that wish to
10
+ use a common syntax that is more restrictive than traditional HTTP field values or could be
11
+ used to [ retrofit current headers] ( https://www.ietf.org/id/draft-ietf-httpbis-retrofit-00.html ) to have them compliant with the new syntax.
11
12
12
- You will be able to:
13
+ The package can be used to:
13
14
14
15
- parse and serialize HTTP Structured Fields
15
16
- create and update HTTP Structured Fields in a predicable way;
@@ -25,7 +26,7 @@ echo $fields->toHttpValue(); //display "/terms";rel="copyright";anchor="#foo"
25
26
System Requirements
26
27
-------
27
28
28
- - You require ** PHP >= 8.1** but the latest stable version of PHP is recommended
29
+ ** PHP >= 8.1** is required but the latest stable version of PHP is recommended.
29
30
30
31
Installation
31
32
------------
@@ -51,7 +52,7 @@ For each of those top-level types, the package provide a dedicated value object
51
52
and to serialize the value object back to the textual representation.
52
53
53
54
- Parsing is done via a common named constructor ` fromHttpValue ` which expects the Header or Trailer string value.
54
- - Serializing is done via a common ` toHttpValue ` public method. The method returns the normalized string representation suited for HTTP textual representation
55
+ - Serializing is done via a common ` toHttpValue ` public method. The method returns the normalized string representation suited for HTTP textual representation.
55
56
56
57
``` php
57
58
use Bakame\Http\StructuredFields\Dictionary;
@@ -68,14 +69,14 @@ $item = Item::fromHttpValue('"foo";a=1;b=2"');
68
69
echo $item->toHttpValue(); // "foo";a=1;b=2
69
70
```
70
71
71
- ## Structured Data Types
72
+ ## Manipulating Structured Fields Value Objects
72
73
73
74
### Items
74
75
75
76
#### Types
76
77
77
- Bare types defined in the RFC are translated to PHP
78
- native type when possible. Two additional classes:
78
+ Item types [ defined in the RFC] ( https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3 ) are translated to PHP native type when possible.
79
+ Two additional classes
79
80
80
81
- ` Bakame\Http\StructuredFields\Token ` and
81
82
- ` Bakame\Http\StructuredFields\ByteSequence `
@@ -93,10 +94,10 @@ are used to represent non-native types as shown in the table below:
93
94
94
95
#### Parameters
95
96
96
- As explain in the RFC, ` Parameters ` are containers of ` Item ` instances. They can be associated
97
- to an ` Item ` instance or other container types ** BUT** the items it contains can not
98
- themselves contain ` Parameters ` instance. More on parameters public API
99
- will be cover in subsequent paragraphs.
97
+ As explain in the RFC, ` Parameters ` are an ordered map of key-value pairs that can be
98
+ associated with an ` Item ` . They can be associated ** BUT** the items they contain
99
+ can not themselves contain ` Parameters ` instance. More on parameters
100
+ public API will be cover in subsequent paragraphs.
100
101
101
102
#### Usage
102
103
@@ -109,13 +110,13 @@ $item = Item::from("hello world", ["a" => 1]);
109
110
$item->value(); //returns "hello world"
110
111
$item->isString(); //return true
111
112
$item->isToken(); //return false
112
- $item->parameters()->getByKey ("a")->value(); //returns 1
113
+ $item->parameters()->get ("a")->value(); //returns 1
113
114
```
114
115
115
116
Once instantiated, accessing ` Item ` properties is done via two methods:
116
117
117
118
- ` Item::value() ` which returns the instance underlying value
118
- - ` Item::parameters() ` which returns the item associated parameters as a distinct ` Parameters ` object
119
+ - ` Item::parameters() ` which returns the parameters associated to the ` Item ` as a distinct ` Parameters ` object
119
120
120
121
** To instantiate a decimal number a float MUST be used as the first argument input.**
121
122
@@ -134,19 +135,24 @@ $item->isInteger(); //return true
134
135
### Containers
135
136
136
137
Apart from the ` Item ` , the RFC defines different containers with different requirements. The
137
- package exposes those containers via the following value objects ` Parameters ` , ` Dictionary ` ,
138
- ` InnerList ` and ` OrderedList ` with the same basic public API. At any given time it
139
- is possible to:
138
+ package exposes those containers via the following value objects:
139
+
140
+ - ` Parameters ` ,
141
+ - ` Dictionary ` ,
142
+ - ` InnerList ` ,
143
+ - and ` OrderedList ` to represent a generic list,
144
+
145
+ At any given time it is possible with each of these objects to:
140
146
141
147
- iterate over each contained element and its optional associated key via the ` IteratorAggregate ` interface;
142
148
- tell whether the container is empty via an ` isEmpty ` method;
143
149
- know the number of elements contained in the container via the ` Countable ` interface;
144
- - merge multiple instance of the same container using the ` merge ` method;
150
+ - merge multiple instance of ** the same type ** using the ` merge ` method;
145
151
146
152
``` php
147
153
use Bakame\Http\StructuredFields\Parameters;
148
154
149
- $parameters = new Parameters(['a' => 1, 'b' => 2, 'c' => Item::from( "hello world") ]);
155
+ $parameters = Parameters::fromAssociative (['a' => 1, 'b' => 2, 'c' => "hello world"]);
150
156
count($parameters); // return 2
151
157
$parameters->isEmpty(); // returns false
152
158
$parameters->toHttpValue(); // return ";a=1;b=2"
@@ -157,34 +163,39 @@ $parameters->toHttpValue(); // return ";a=1;b=2"
157
163
The ` Parameters ` and the ` Dictionary ` classes allow associating a string
158
164
key to its members as such they expose the following methods:
159
165
166
+ - ` fromAssociative ` a named constructor to instantiate the container with an associative array;
167
+ - ` fromPairs ` a named constructor to instantiate the container with a list of key-value pairs;
168
+ - ` has ` tell whether a specific element is associated to a given ` key ` ;
169
+ - ` get ` returns the element associated to a specific ` key ` ;
170
+ - ` hasPair ` tell whether a ` key-value ` association exists at a given ` index ` (negative indexes are supported);
171
+ - ` pair ` returns the key-pair association present at a specific ` index ` (negative indexes are supported);
172
+ - ` pairs ` returns an iterator to iterate over the container pairs;
160
173
- ` set ` add an element at the end of the container if the key is new otherwise only the value is updated;
161
174
- ` append ` always add an element at the end of the container, if already present the previous value is removed;
162
175
- ` prepend ` always add an element at the beginning of the container, if already present the previous value is removed;
163
176
- ` delete ` to remove elements based on their associated keys;
164
- - tell whether an element is attached to the container using its ` index ` or ` key ` via ` hasIndex ` and ` hasKey ` methods;
165
- - get any element by its string ` key ` or by its integer ` index ` via ` getByKey ` and ` getByIndex ` methods when applicable;
166
177
167
178
``` php
168
179
use Bakame\Http\StructuredFields\Dictionary;
169
180
use Bakame\Http\StructuredFields\Item;
170
181
171
- $dictionary = new Dictionary();
172
- $dictionary->set('b', true);
182
+ $dictionary = Dictionary::fromPairs([['b', true]]);
173
183
$dictionary->append('c', Item::from(true, ['foo' => new Token('bar')]));
174
184
$dictionary->prepend('a', false);
175
185
$dictionary->toHttpValue(); //returns "a=?0, b, c;foo=bar"
176
- $dictionary->hasKey ('a'); //return true
177
- $dictionary->hasKey ('foo'); //return false
178
- $dictionary->getByIndex (1); //return Item::fromBoolean(true)
179
- $dictionary->hasIndex (-1); //return Item::fromBoolean( true)
186
+ $dictionary->has ('a'); //return true
187
+ $dictionary->has ('foo'); //return false
188
+ $dictionary->pair (1); //return ['b', Item::fromBoolean(true)]
189
+ $dictionary->hasPair (-1); //return true
180
190
$dictionary->append('z', 42.0);
181
191
$dictionary->delete('b', 'c');
182
192
echo $dictionary->toHttpValue(); //returns "a=?0, z=42.0"
183
193
```
184
194
185
195
** Item types are inferred using ` Item::from ` if a ` Item ` object is not submitted.**
186
196
187
- - ` getByIndex ` supports negative index
197
+ ** EVERY CHANGE IN THE ORDERED MAP WILL RE-INDEX THE PAIRS AS TO NOT EXPOSE MISSING INDEXES**
198
+
188
199
- ` Parameters ` can only contains ` Item ` instances
189
200
- ` Dictionary ` instance can contain ` Item ` and ` InnerList ` instances.
190
201
@@ -233,11 +244,12 @@ Contributions are welcome and will be fully credited. Please see [CONTRIBUTING](
233
244
Testing
234
245
-------
235
246
236
- The library has a :
247
+ The library :
237
248
238
- - a [ PHPUnit] ( https://phpunit.de ) test suite
239
- - a coding style compliance test suite using [ PHP CS Fixer] ( https://cs.sensiolabs.org/ ) .
240
- - a code analysis compliance test suite using [ PHPStan] ( https://github.com/phpstan/phpstan ) .
249
+ - has a [ PHPUnit] ( https://phpunit.de ) test suite
250
+ - has a coding style compliance test suite using [ PHP CS Fixer] ( https://cs.sensiolabs.org/ ) .
251
+ - has a code analysis compliance test suite using [ PHPStan] ( https://github.com/phpstan/phpstan ) .
252
+ - is compliant with [ the language agnostic HTTP Structured Fields Test suite] ( https://github.com/httpwg/structured-field-tests ) .
241
253
242
254
To run the tests, run the following command from the project folder.
243
255
0 commit comments