Skip to content

Commit ea97030

Browse files
committed
Improve Public API
1 parent 88fb076 commit ea97030

22 files changed

+323
-224
lines changed

CHANGELOG.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
66

77
### Added
88

9-
- None
9+
- `ParameterAccess` interface updated with 3 new methods to ease parameter members modification.
10+
- Support for `Stringable` instance to `Item::from`, the instances will be converted to the string data type.
11+
- `Parameter::new` named constructor to create a new instance without any parameter.
12+
- `Dictionnary::new` named constructor to create a new instance without any parameter.
13+
- `InnerList::new` named constructor to create a new instance with members as variadic arguments.
14+
- `OrderedList::new` named constructor to create a new instance with members as variadic arguments.
1015

1116
### Fixed
1217

18+
- **[BC Break]** `::fromAssociative`, `::fromList`, `::fromPairs` methods require iterable arguments without default value.
1319
- **[BC Break]** `Item::value` method returns the Item (returns value can be `float|int|string|bool|ByteSequence|Token`).
1420
- `InnerList::parameters` is no longer accessible as a public readonly property.
1521

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

2026
### Removed
2127

22-
- None
28+
- **[BC Break]** `InnerList::from` named constructor use `InnerList::new` method instead.
29+
- **[BC Break]** `OrderedList::from` named constructor use `OrderedList::new` method instead.
2330

2431
## [0.6.0] - 2022-11-12
2532

@@ -176,7 +183,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
176183

177184
**Initial release!**
178185

179-
[Next]: https://github.com/bakame-php/http-structured-fields/compare/0.5.0...master
186+
[Next]: https://github.com/bakame-php/http-structured-fields/compare/0.6.0...master
187+
[0.6.0]: https://github.com/bakame-php/http-structured-fields/compare/0.5.0...0.6.0
180188
[0.5.0]: https://github.com/bakame-php/http-structured-fields/compare/0.4.0...0.5.0
181189
[0.4.0]: https://github.com/bakame-php/http-structured-fields/compare/0.3.0...0.4.0
182190
[0.3.0]: https://github.com/bakame-php/http-structured-fields/compare/0.2.0...0.3.0

Makefile

Lines changed: 0 additions & 33 deletions
This file was deleted.

README.md

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Structured Field Values for PHP
2-
=======================================
1+
# HTTP Structured Fields for PHP
32

43
[![Author](http://img.shields.io/badge/author-@nyamsprod-blue.svg?style=flat-square)](https://twitter.com/nyamsprod)
54
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
@@ -20,21 +19,19 @@ The package can be used to:
2019
- build or update HTTP Structured Fields in a predicable way;
2120

2221
```php
23-
use Bakame\Http\StructuredFields;
22+
use Bakame\Http\StructuredFields\Item;
2423

25-
$field = StructuredFields\Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
24+
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
2625
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
2726
echo $field->value(); // display "/terms"
2827
echo $field->parameters()['rel']->value(); // display "copyright"
2928
```
3029

31-
System Requirements
32-
-------
30+
## System Requirements
3331

3432
**PHP >= 8.1** is required but the latest stable version of PHP is recommended.
3533

36-
Installation
37-
------------
34+
## Installation
3835

3936
Use composer:
4037

@@ -50,24 +47,21 @@ or download the library and:
5047
~~~php
5148
require 'path/to/http-structured-fields/repo/autoload.php';
5249

53-
use Bakame\Http\StructuredFields;
50+
use Bakame\Http\StructuredFields\OrderedList;
5451

55-
$list = StructuredFields\OrderedList::fromHttpValue('"/member/*/author", "/member/*/comments"');
52+
$list = OrderedList::fromHttpValue('"/member/*/author", "/member/*/comments"');
5653
echo $list[-1]->value(); // returns '/member/*/comments'
5754
~~~
5855

59-
Documentation
60-
------------
56+
## Documentation
6157

6258
Full documentation can be found in the [docs](/docs).
6359

64-
Contributing
65-
-------
60+
## Contributing
6661

6762
Contributions are welcome and will be fully credited. Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE OF CONDUCT](.github/CODE_OF_CONDUCT.md) for details.
6863

69-
Testing
70-
-------
64+
## Testing
7165

7266
The library:
7367

@@ -79,27 +73,23 @@ The library:
7973
To run the tests, run the following command from the project folder.
8074

8175
``` bash
82-
$ composer test
76+
composer test
8377
```
8478

85-
Security
86-
-------
79+
## Security
8780

8881
If you discover any security related issues, please email nyamsprod@gmail.com instead of using the issue tracker.
8982

90-
Credits
91-
-------
83+
## Credits
9284

9385
- [ignace nyamagana butera](https://github.com/nyamsprod)
9486
- [All Contributors](https://github.com/bakame-php/http-structured-fields/contributors)
9587

96-
Attribution
97-
-------
88+
## Attribution
9889

9990
The package internal parser is heavily inspired by previous work done by [Gapple](https://twitter.com/gappleca) on [Structured Field Values for PHP](https://github.com/gapple/structured-fields/).
10091

101-
License
102-
-------
92+
## License
10393

10494
The MIT License (MIT). Please see [License File](LICENSE) for more information.
10595

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"require-dev": {
3131
"friendsofphp/php-cs-fixer": "^v3.13.2",
3232
"httpwg/structured-field-tests": "*@dev",
33-
"phpstan/phpstan": "^1.9.8",
34-
"phpstan/phpstan-strict-rules": "^1.4.4",
33+
"phpstan/phpstan": "^1.9.9",
34+
"phpstan/phpstan-strict-rules": "^1.4.5",
3535
"phpstan/phpstan-phpunit": "^1.3.3",
3636
"phpstan/phpstan-deprecation-rules": "^1.1.1",
3737
"phpunit/phpunit": "^9.5.27"

docs/basic-usage.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ representation of the field and to serialize the value object back to the textua
1414
- Serializing is done via a common `toHttpValue` public method. The method returns the **normalized string** representation suited for HTTP textual representation.
1515

1616
```php
17-
use Bakame\Http\StructuredFields;
17+
use Bakame\Http\StructuredFields\Dictionary;
18+
use Bakame\Http\StructuredFields\Item;
19+
use Bakame\Http\StructuredFields\OrderedList;
1820

19-
$dictionary = StructuredFields\Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
21+
$dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
2022
echo $dictionary->toHttpValue(); // 'a=?0, b, c;foo=bar'
2123

22-
$list = StructuredFields\OrderedList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
24+
$list = OrderedList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
2325
echo $list->toHttpValue(); // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
2426

25-
$item = StructuredFields\Item::fromHttpValue('"foo";a=1;b=2');
27+
$item = Item::fromHttpValue('"foo";a=1;b=2');
2628
echo $item->toHttpValue(); // "foo";a=1;b=2
2729
```
2830

docs/containers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Containers
22

33
```php
4-
use Bakame\Http\StructuredFields;
4+
use Bakame\Http\StructuredFields\Parameters;
55

6-
$parameters = StructuredFields\Parameters::fromAssociative(['a' => 1, 'b' => 2, 'c' => "hello world"]);
6+
$parameters = Parameters::fromAssociative(['a' => 1, 'b' => 2, 'c' => "hello world"]);
77
count($parameters); // returns 3
88
$parameters->hasMembers(); // returns true
99
$parameters->toHttpValue(); // returns ';a=1;b=2;c="hello world"'

docs/index.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Structured Field Values for PHP
2-
=======================================
1+
HTTP Structured Fields For PHP
2+
=====================
33

44
[![Author](http://img.shields.io/badge/author-@nyamsprod-blue.svg?style=flat-square)](https://twitter.com/nyamsprod)
55
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
@@ -20,9 +20,9 @@ The package can be used to:
2020
- build or update HTTP Structured Fields in a predicable way;
2121

2222
```php
23-
use Bakame\Http\StructuredFields;
23+
use Bakame\Http\StructuredFields\Item;
2424

25-
$field = StructuredFields\Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
25+
$field = Item::from("/terms", ['rel' => 'copyright', 'anchor' => '#foo']);
2626
echo $field->toHttpValue(); // display "/terms";rel="copyright";anchor="#foo"
2727
echo $field->value(); // display "/terms"
2828
echo $field->parameters['rel']->value(); // display "copyright"
@@ -50,9 +50,9 @@ or download the library and:
5050
~~~php
5151
require 'path/to/http-structured-fields/repo/autoload.php';
5252

53-
use Bakame\Http\StructuredFields;
53+
use Bakame\Http\StructuredFields\OrderedList;
5454

55-
$list = StructuredFields\OrderedList::fromHttpValue('"/member/*/author", "/member/*/comments"');
55+
$list = OrderedList::fromHttpValue('"/member/*/author", "/member/*/comments"');
5656
echo $list[-1]->value(); // returns '/member/*/comments'
5757
~~~
5858

docs/item.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ The table below summarizes the item value type.
2222
### Token
2323

2424
```php
25-
use Bakame\Http\StructuredFields;
25+
use Bakame\Http\StructuredFields\Token;
2626

27-
$token = StructuredFields\Token::fromString('bar');
27+
$token = Token::fromString('bar');
2828

2929
echo $token->value; // displays 'bar'
3030
```
@@ -38,10 +38,10 @@ The class also exposes its value via the public readonly property `value` to ena
3838
### Byte Sequence
3939

4040
```php
41-
use Bakame\Http\StructuredFields;
41+
use Bakame\Http\StructuredFields\ByteSequence;
4242

43-
$sequenceFromDecoded = StructuredFields\ByteSequence::fromDecoded("Hello World");
44-
$sequenceFromEncoded = StructuredFields\ByteSequence::fromEncoded("SGVsbG8gV29ybGQ=");
43+
$sequenceFromDecoded = ByteSequence::fromDecoded("Hello World");
44+
$sequenceFromEncoded = ByteSequence::fromEncoded("SGVsbG8gV29ybGQ=");
4545

4646
echo $sequenceFromEncoded->decoded(); // displays 'Hello World'
4747
echo $sequenceFromDecoded->encoded(); // displays 'SGVsbG8gV29ybGQ='
@@ -63,9 +63,9 @@ keys are strings and the value are bare items. Their public API is covered in th
6363
**Exception will be thrown when trying to access or serialize a parameter object containing non-bare items.**
6464

6565
```php
66-
use Bakame\Http\StructuredFields;
66+
use Bakame\Http\StructuredFields\Item;
6767

68-
$item = StructuredFields\Item::from("hello world", ["a" => true]);
68+
$item = Item::from("hello world", ["a" => true]);
6969
$item->value(); // returns "hello world"
7070
$item->isString(); // returns true
7171
$item->isToken(); // returns false
@@ -88,11 +88,12 @@ Those listed named constructors expect a string or a stringable object as its fi
8888
same argument definition as in `Item::from` for parameters argument.
8989

9090
```php
91-
use Bakame\Http\StructuredFields;
91+
use Bakame\Http\StructuredFields\ByteSequence;
92+
use Bakame\Http\StructuredFields\Item;
9293

93-
$item = StructuredFields\Item::fromPair([
94+
$item = Item::fromPair([
9495
"hello world", [
95-
["a", StructuredFields\ByteSequence::fromDecoded("Hello World")],
96+
["a", ByteSequence::fromDecoded("Hello World")],
9697
]
9798
]);
9899
$item->value(); // returns "hello world"
@@ -115,23 +116,24 @@ Once instantiated, accessing `Item` properties is done via:
115116
- the method `Item::parameters` which returns the parameters associated to the `Item` as a distinct `Parameters` object
116117

117118
```php
118-
use Bakame\Http\StructuredFields;
119+
use Bakame\Http\StructuredFields\ByteSequence;
120+
use Bakame\Http\StructuredFields\Item;
119121

120-
$item = StructuredFields\Item::from(StructuredFields\ByteSequence::fromEncoded("SGVsbG8gV29ybGQ="));
122+
$item = Item::from(ByteSequence::fromEncoded("SGVsbG8gV29ybGQ="));
121123
$item->isByteSequence(); // returns true
122124
echo $item->value(); // returns StructuredFields\ByteSequence::fromEncoded("SGVsbG8gV29ybGQ=");
123125
```
124126

125127
**Of note: to instantiate a decimal number type a float MUST be used as the first argument of `Item::from`.**
126128

127129
```php
128-
use Bakame\Http\StructuredFields;
130+
use Bakame\Http\StructuredFields\Item;
129131

130-
$decimal = StructuredFields\Item::from(42.0);
132+
$decimal = Item::from(42.0);
131133
$decimal->isDecimal(); //return true
132134
$decimal->isInteger(); //return false
133135

134-
$item = StructuredFields\Item::fromPair([42]);
136+
$item = Item::fromPair([42]);
135137
$item->isDecimal(); //return false
136138
$item->isInteger(); //return true
137139
```

docs/lists.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Both classes expose the following:
1717
named constructors:
1818

1919
- `fromList` instantiates the container with a list of members in an iterable construct;
20-
- `from` instantiates the container with a list of members as variadic;
20+
- `new` instantiates the container with a list of members as variadic;
2121

2222
setter methods
2323

@@ -34,17 +34,20 @@ In addition to `StructuredField` specific interfaces, both classes implements:
3434
- PHP `ArrayAccess` interface.
3535

3636
```php
37-
use Bakame\Http\StructuredFields;
37+
use Bakame\Http\StructuredFields\InnerList;
38+
use Bakame\Http\StructuredFields\Item;
39+
use Bakame\Http\StructuredFields\OrderedList;
40+
use Bakame\Http\StructuredFields\Token;
3841

39-
$innerList = StructuredFields\InnerList::fromList([42, 42.0, "42"], ["a" => true]);
42+
$innerList = InnerList::fromList([42, 42.0, "42"], ["a" => true]);
4043
$innerList->has(2); //return true
4144
$innerList->has(42); //return false
42-
$innerList->push(StructuredFields\Token::fromString('forty-two'));
45+
$innerList->push(Token::fromString('forty-two'));
4346
$innerList->remove(0, 2);
4447
echo $innerList->toHttpValue(); //returns '(42.0 forty-two);a'
4548

46-
$orderedList = StructuredFields\OrderedList::from(
47-
StructuredFields\Item::from("42", ["foo" => "bar"]),
49+
$orderedList = OrderedList::new(
50+
Item::from("42", ["foo" => "bar"]),
4851
$innerList
4952
);
5053
echo $orderedList->toHttpValue(); //returns '"42";foo="bar", (42.0 forty-two);a'
@@ -53,12 +56,13 @@ echo $orderedList->toHttpValue(); //returns '"42";foo="bar", (42.0 forty-two);a'
5356
Same example using the `ArrayAccess` interface.
5457

5558
```php
56-
use Bakame\Http\StructuredFields;
59+
use Bakame\Http\StructuredFields\InnerList;
60+
use Bakame\Http\StructuredFields\Token;
5761

58-
$innerList = StructuredFields\InnerList::fromList([42, 42.0, "42"], ["a" => true]);
62+
$innerList = InnerList::fromList([42, 42.0, "42"], ["a" => true]);
5963
isset($innerList[2]); //return true
6064
isset($innerList[42]); //return false
61-
$innerList[] = StructuredFields\Token::fromString('forty-two');
65+
$innerList[] = Token::fromString('forty-two');
6266
unset($innerList[0], $innerList[2]);
6367
echo $innerList->toHttpValue(); //returns '(42.0 forty-two);a'
6468
```
@@ -69,9 +73,10 @@ keys. Be aware that re-indexation behaviour may affect
6973
your logic**
7074

7175
```php
72-
use Bakame\Http\StructuredFields;
76+
use Bakame\Http\StructuredFields\OrderedList;
77+
use Bakame\Http\StructuredFields\Token;
7378

74-
$innerList = StructuredFields\OrderedList::fromList([42, 42.0]);
75-
$innerList[2] = StructuredFields\Token::fromString('forty-two'); // will throw
79+
$innerList = OrderedList::fromList([42, 42.0]);
80+
$innerList[2] = Token::fromString('forty-two'); // will throw
7681
echo $innerList->toHttpValue(), PHP_EOL;
7782
```

0 commit comments

Comments
 (0)