Skip to content

Commit c5fa9dd

Browse files
committed
Add support for sanitizing any object
1 parent ef0a241 commit c5fa9dd

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
88

99
- `Item::fromPair` named constructor to create a new instance from a pair expressed as an array list with two values.
1010
- `Parameters::sanitize` ensure the container always contains only Bare Items.
11-
- `InnerList::sanitize` ensure the list is without gaps.
12-
- `OrderedLost::sanitize` ensure the list is without gaps.
11+
- `InnerList::sanitize` ensure the list is without gaps and calls `Parameters::sanitize`.
12+
- `OrderedList::sanitize` ensure the list is without gaps and calls `Parameters::sanitize`.
13+
- `Dictionnary::sanitize` ensure the list is without gaps and calls `Parameters::sanitize`.
14+
- `Item::sanitize` calls `Parameters::sanitize`.
1315
- `autoload.php` script to allow non composer application to load the package
1416
- `OrderedList` and `InnerList` now implements the PHP `ArrayAccess` interface.
1517

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ For each of these top-level types, the package provides a dedicated value object
8282
representation of the field and to serialize the value object back to the textual representation.
8383

8484
- Parsing is done via a common named constructor `fromHttpValue` which expects the Header or Trailer string value.
85-
- Serializing is done via a common `toHttpValue` public method. The method returns the normalized string representation suited for HTTP textual representation.
85+
- Serializing is done via a common `toHttpValue` public method. The method returns the **normalized string** representation suited for HTTP textual representation.
8686

8787
Building Structured Fields
8888
------------

src/Dictionary.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ public function clear(): self
258258
return $this;
259259
}
260260

261+
/**
262+
* Ensure the container contains valid members.
263+
*/
264+
public function sanitize(): self
265+
{
266+
foreach ($this->members as $member) {
267+
$member->sanitize();
268+
}
269+
270+
return $this;
271+
}
272+
261273
/**
262274
* Adds a member at the end of the instance and deletes any previous reference to the key if present.
263275
*

src/InnerList.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ public function remove(int ...$indexes): self
198198
*/
199199
public function sanitize(): self
200200
{
201+
$this->parameters->sanitize();
202+
203+
foreach ($this->members as $member) {
204+
$member->parameters->sanitize();
205+
}
206+
201207
if (!array_is_list($this->members)) {
202208
$this->members = array_values($this->members);
203209
}

src/Item.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,11 @@ public function isByteSequence(): bool
315315
{
316316
return $this->value instanceof ByteSequence;
317317
}
318+
319+
public function sanitize(): self
320+
{
321+
$this->parameters->sanitize();
322+
323+
return $this;
324+
}
318325
}

src/OrderedList.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ public function remove(int ...$indexes): self
200200
return $this;
201201
}
202202

203-
204203
/**
205204
* Ensure the container always contains list.
206205
*
@@ -209,6 +208,10 @@ public function remove(int ...$indexes): self
209208
*/
210209
public function sanitize(): self
211210
{
211+
foreach ($this->members as $member) {
212+
$member->parameters->sanitize();
213+
}
214+
212215
if (!array_is_list($this->members)) {
213216
$this->members = array_values($this->members);
214217
}

0 commit comments

Comments
 (0)