Skip to content

Commit 3eb3ec7

Browse files
committed
Adding new methods to OrderedMap
1 parent ca7f9b0 commit 3eb3ec7

File tree

8 files changed

+44
-6
lines changed

8 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1010
- `OrederedMap::unshift`
1111
- `OrederedMap::insert`
1212
- `OrederedMap::replace`
13+
- `OrederedMap::removeByIndices`
14+
- `OrederedMap::removeByKeys`
1315
- `ParameterAccess::pushParanmeters`
1416
- `ParameterAccess::unshiftParamaters`
1517
- `ParameterAccess::insertParamaters`

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ $map->unshift(array ...$pairs): static;
448448
$map->push(array ...$pairs): static;
449449
$map->insert(int $key, array ...$pairs): static;
450450
$map->replace(int $key, array $pair): static;
451+
$map->removeByKeys(string ...$keys): static;
452+
$map->removeByIndices(int ...$indices): static;
451453
```
452454

453455
We can rewrite the previous example
@@ -477,8 +479,8 @@ echo $value; //b=?0, a=(bar "42" 42 42.0), c=@1671800423
477479

478480
**⚠️WARNING: on duplicate `keys` pair values are merged as per RFC logic.**
479481

480-
The `remove` always accepted string or integer as input. Since version `1.1` it will also remove
481-
the corresponding pair if its index is given to the method.
482+
The `remove` always accepted string or integer as input. Since version `1.1` the method is fixed to
483+
remove the corresponding pair if its index is given to the method.
482484

483485
```diff
484486
<?php
@@ -490,6 +492,16 @@ $field = Dictionary::fromHttpValue('b=?0, a=(bar "42" 42 42.0), c=@1671800423');
490492
+ echo $field->remove('b', 2)->toHttpValue(); // returns a=(bar "42" 42 42.0)
491493
```
492494

495+
If a stricter approach is needed, use the following new methods `removeByIndices` and/or `removeByKeys`:
496+
497+
```php
498+
use Bakame\Http\StructuredFields\Parameters;
499+
500+
$field = Parameters::fromHttpValue(';expire=@1681504328;path="/";max-age=2500;secure;httponly=?0;samesite=lax');
501+
echo $field->removeByIndices(4, 2, 0)->toHttpValue(); // returns ;path="/";secure;samesite=lax
502+
echo $field->removeByKeys('expire', 'httponly', 'max-age')->toHttpValue(); // returns ;path="/";secure;samesite=lax
503+
```
504+
493505
#### Automatic conversion
494506

495507
For all containers, to ease instantiation the following automatic conversion are applied on

src/Dictionary.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ public function remove(string|int ...$keys): static
290290
};
291291
}
292292

293+
public function removeByIndices(int ...$indices): static
294+
{
295+
return $this->remove(...$indices);
296+
}
297+
298+
public function removeByKeys(string ...$keys): static
299+
{
300+
return $this->remove(...$keys);
301+
}
302+
293303
/**
294304
* @param SfMember|SfMemberInput $member
295305
*/

src/DictionaryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ public function it_can_delete_a_member_via_remove_method(): void
256256

257257
self::assertFalse($newInstance->remove('foo')->hasMembers());
258258
self::assertSame($newInstance, $newInstance->remove('baz', 'bar', 'yolo-not-there', 325));
259+
self::assertSame($newInstance, $newInstance->removeByKeys('baz', 'bar', 'yolo-not-there'));
260+
self::assertSame($newInstance, $newInstance->removeByIndices(325));
259261

260262
$instanceWithoutMember = Dictionary::new();
261263
self::assertSame($instanceWithoutMember->remove(), $instanceWithoutMember);

src/InnerList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ public function withoutParameter(string ...$keys): static
386386

387387
public function withoutParameterByKeys(string ...$keys): static
388388
{
389-
return $this->withParameters($this->parameters()->remove(...$keys));
389+
return $this->withParameters($this->parameters()->removeByKeys(...$keys));
390390
}
391391

392392
public function withoutParameterByIndices(int ...$indices): static
393393
{
394-
return $this->withParameters($this->parameters()->remove(...$indices));
394+
return $this->withParameters($this->parameters()->removeByIndices(...$indices));
395395
}
396396

397397
public function withoutAnyParameter(): static

src/Item.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ public function withoutParameter(string ...$keys): static
339339

340340
public function withoutParameterByKeys(string ...$keys): static
341341
{
342-
return $this->withParameters($this->parameters()->remove(...$keys));
342+
return $this->withParameters($this->parameters()->removeByKeys(...$keys));
343343
}
344344

345345
public function withoutParameterByIndices(int ...$indices): static
346346
{
347-
return $this->withParameters($this->parameters()->remove(...$indices));
347+
return $this->withParameters($this->parameters()->removeByIndices(...$indices));
348348
}
349349

350350
public function withoutAnyParameter(): static

src/MemberOrderedMap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* @method static unshift(array ...$pairs) Inserts pair at the start of the member list
1616
* @method static insert(int $index, array ...$pairs) Inserts pairs at the index
1717
* @method static replace(int $index, array $pair) Replaces the pair at the given index
18+
* @method static removeByIndices(int ... $indices) Remove members by index
19+
* @method static removeByKeys(string ...$keys) Remove members by keys
1820
*/
1921
interface MemberOrderedMap extends MemberContainer
2022
{

src/Parameters.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ public function remove(string|int ...$keys): static
272272
};
273273
}
274274

275+
public function removeByIndices(int ...$indices): static
276+
{
277+
return $this->remove(...$indices);
278+
}
279+
280+
public function removeByKeys(string ...$keys): static
281+
{
282+
return $this->remove(...$keys);
283+
}
284+
275285
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
276286
{
277287
$members = $this->members;

0 commit comments

Comments
 (0)