Skip to content

Commit d00a622

Browse files
committed
Adding missing Modifier methods
1 parent b9b8238 commit d00a622

File tree

6 files changed

+168
-45
lines changed

6 files changed

+168
-45
lines changed

components/CHANGELOG.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ All Notable changes to `League\Uri\Components` will be documented in this file
99
- `Domain::first` returns the first domain label
1010
- `Domain::last` returns the last domain label
1111
- `Domain::contains` tells wether the label is present in the domain
12-
- `Domain::indexOf` returns the index of the first occurrence of the label or null
13-
- `Domain::lastIndexOf` returns the index of the last occurrence of the label or null
12+
- `Domain::indexOf` returns the index of the first occurrence of the label
13+
- `Domain::lastIndexOf` returns the index of the last occurrence of the label
1414
- `Domain::isEmpty` tells whether the domain is empty or not (contains at least on label)
1515
- `HierarchicalPath::first` returns the first segment
1616
- `HierarchicalPath::last` returns the last segment
1717
- `HierarchicalPath::contains` tells whether the segment is present in the path
18-
- `HierarchicalPath::indexOf` returns the index of the first occurrence of the segment or null
19-
- `HierarchicalPath::lastIndexOf` returns the index of the last occurrence of the segment or null
18+
- `HierarchicalPath::indexOf` returns the index of the first occurrence of the segment
19+
- `HierarchicalPath::lastIndexOf` returns the index of the last occurrence of the segment
2020
- `HierarchicalPath::isEmpty` tells whether the path is empty or not (contains at least on segment)
2121
- `Query::first` returns the first value for a given query key pair
2222
- `Query::last` returns the last value for a given query key pair
@@ -31,17 +31,22 @@ All Notable changes to `League\Uri\Components` will be documented in this file
3131
- `Modifier::redactPathSegmentsByOffset` redacts specific path segments by offset if present
3232
- `Modifier::redactQueryPairs` redacts specific query pair if present
3333
- `Modifier::prependQuery` prepend a query string to the URI query component
34+
- `Modifier::prependPath` replace `Modifier::prependSegment`
35+
- `Modifier::appendPath` replace `Modifier::appendSegment`
36+
- `Modifier::prependSegments`
37+
- `Modifier::appendSegments`
3438

3539
### Fixed
3640

3741
- Fix deprecation message for `Modifier::uri()` method [166](https://github.com/thephpleague/uri-src/pull/166) by [meyerbaptiste](https://github.com/meyerbaptiste)
38-
- Fix missing SensitiveParameter usage on `Modifier::withUserInfo` method
42+
- Fix missing `SensitiveParameter` usage on `Modifier::withUserInfo` method
3943
- Fix Host resolution using the new `HostRecord` class
40-
- Fix `Modifier::withPath` improve handling of path leading slash presence
44+
- Fix `Modifier::withPath` improve handling of path leading slash presence for PSR-7 implementing classes
4145

4246
### Deprecated
4347

44-
- None
48+
- `Modifier::prependSegment` use `Modifier::prependPath` instead
49+
- `Modifier::appendSegment` use `Modifier::appendPath` instead
4550

4651
### Removed
4752

components/Components/HierarchicalPath.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
use Uri\Rfc3986\Uri as Rfc3986Uri;
2929
use Uri\WhatWg\Url as WhatWgUrl;
3030

31+
use ValueError;
3132
use function array_count_values;
3233
use function array_filter;
3334
use function array_keys;
35+
use function array_map;
3436
use function array_pop;
3537
use function array_unshift;
3638
use function count;
@@ -307,30 +309,60 @@ public function withTrailingSlash(): PathInterface
307309
};
308310
}
309311

310-
public function append(Stringable|string $segment): SegmentedPathInterface
312+
public function append(Stringable|string $path): SegmentedPathInterface
311313
{
312-
/** @var string $segment */
313-
$segment = self::filterComponent($segment);
314+
/** @var string $path */
315+
$path = self::filterComponent($path);
314316

315317
return new self(
316318
rtrim($this->path->toString(), self::SEPARATOR)
317319
.self::SEPARATOR
318-
.ltrim($segment, self::SEPARATOR)
320+
.ltrim($path, self::SEPARATOR)
319321
);
320322
}
321323

322-
public function prepend(Stringable|string $segment): SegmentedPathInterface
324+
/**
325+
* @param iterable<Stringable|string> $segments
326+
*
327+
* @return SegmentedPathInterface
328+
*/
329+
public function appendSegments(iterable $segments): SegmentedPathInterface
323330
{
324-
/** @var string $segment */
325-
$segment = self::filterComponent($segment);
331+
$newSegments = [];
332+
foreach ($segments as $segment) {
333+
$newSegments[] = str_replace('/', '%2F', self::filterComponent($segment) ?? throw new ValueError('The segment can not be null.'));
334+
}
335+
336+
return $this->append(implode('/', $newSegments));
337+
}
338+
339+
public function prepend(Stringable|string $path): SegmentedPathInterface
340+
{
341+
/** @var string $path */
342+
$path = self::filterComponent($path);
326343

327344
return new self(
328-
rtrim($segment, self::SEPARATOR)
345+
rtrim($path, self::SEPARATOR)
329346
.self::SEPARATOR
330347
.ltrim($this->path->toString(), self::SEPARATOR)
331348
);
332349
}
333350

351+
/**
352+
* @param iterable<Stringable|string> $segments
353+
*
354+
* @return SegmentedPathInterface
355+
*/
356+
public function prependSegments(iterable $segments): SegmentedPathInterface
357+
{
358+
$newSegments = [];
359+
foreach ($segments as $segment) {
360+
$newSegments[] = str_replace('/', '%2F', self::filterComponent($segment) ?? throw new ValueError('The segment can not be null.'));
361+
}
362+
363+
return $this->prepend(implode('/', $newSegments));
364+
}
365+
334366
public function withSegment(int $key, Stringable|string $segment): SegmentedPathInterface
335367
{
336368
$nbSegments = count($this->segments);

components/Modifier.php

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use League\Uri\Contracts\FragmentDirective;
3232
use League\Uri\Contracts\FragmentInterface;
3333
use League\Uri\Contracts\PathInterface;
34+
use League\Uri\Contracts\SegmentedPathInterface;
3435
use League\Uri\Contracts\UriAccess;
3536
use League\Uri\Contracts\UriInterface;
3637
use League\Uri\Exceptions\MissingFeature;
@@ -968,11 +969,39 @@ public function addTrailingSlash(): static
968969
}
969970

970971
/**
971-
* Append a new segment or a new path to the URI path.
972+
* Append a new path or add a path to the URI path.
972973
*/
973-
public function appendSegment(Stringable|string $segment): static
974+
public function appendPath(Stringable|string $path): static
975+
{
976+
return $this->withPath(HierarchicalPath::fromUri($this->uri)->append($path));
977+
}
978+
979+
/**
980+
* Prepend a path or add a new path to the URI path.
981+
*/
982+
public function prependPath(Stringable|string $path): static
974983
{
975-
return $this->withPath(HierarchicalPath::fromUri($this->uri)->append($segment));
984+
return $this->withPath(HierarchicalPath::fromUri($this->uri)->prepend($path));
985+
}
986+
987+
/**
988+
* Append a list of segments or a new path to the URI path.
989+
*
990+
* @param iterable<Stringable|string> $segments
991+
*/
992+
public function appendSegments(iterable $segments): static
993+
{
994+
return $this->withPath(HierarchicalPath::fromUri($this->uri)->appendSegments($segments));
995+
}
996+
997+
/**
998+
* Prepend a list of segments or a new path to the URI path.
999+
*
1000+
* @param iterable<Stringable|string> $segments
1001+
*/
1002+
public function prependSegments(iterable $segments): static
1003+
{
1004+
return $this->withPath(HierarchicalPath::fromUri($this->uri)->prependSegments($segments));
9761005
}
9771006

9781007
/**
@@ -991,14 +1020,6 @@ public function dataPathToBinary(): static
9911020
return $this->withPath(DataPath::fromUri($this->uri)->toBinary()->toString());
9921021
}
9931022

994-
/**
995-
* Prepend a new segment or a new path to the URI path.
996-
*/
997-
public function prependSegment(Stringable|string $segment): static
998-
{
999-
return$this->withPath(HierarchicalPath::fromUri($this->uri)->prepend($segment));
1000-
}
1001-
10021023
/**
10031024
* Remove a base path from the URI path.
10041025
*/
@@ -1372,6 +1393,32 @@ public function retainFragmentDirectives(): static
13721393
return $this->withFragment(FragmentDirectives::fromUri($this->unwrap()));
13731394
}
13741395

1396+
/**
1397+
* DEPRECATION WARNING! This method will be removed in the next major point release.
1398+
*
1399+
* @deprecated Since version 7.7.0
1400+
* @codeCoverageIgnore
1401+
* @see Modifier::appendPath
1402+
*/
1403+
#[Deprecated(message:'use League\Uri\Modifier::appendPath() instead', since:'league/uri-components:7.7.0')]
1404+
public function appendSegment(Stringable|string $segment): static
1405+
{
1406+
return $this->appendPath($segment);
1407+
}
1408+
1409+
/**
1410+
* DEPRECATION WARNING! This method will be removed in the next major point release.
1411+
*
1412+
* @deprecated Since version 7.7.0
1413+
* @codeCoverageIgnore
1414+
* @see Modifier::prependPath
1415+
*/
1416+
#[Deprecated(message:'use League\Uri\Modifier::prependPath() instead', since:'league/uri-components:7.7.0')]
1417+
public function prependSegment(Stringable|string $segment): static
1418+
{
1419+
return $this->prependPath($segment);
1420+
}
1421+
13751422
/**
13761423
* DEPRECATION WARNING! This method will be removed in the next major point release.
13771424
*

components/ModifierTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -571,27 +571,27 @@ public function testDataUriWithParameters(): void
571571
);
572572
}
573573

574-
#[DataProvider('appendSegmentProvider')]
574+
#[DataProvider('appendPathProvider')]
575575
public function testAppendProcess(string $segment, string $append): void
576576
{
577-
self::assertSame($append, $this->modifier->appendSegment($segment)->unwrap()->getPath());
577+
self::assertSame($append, $this->modifier->appendPath($segment)->unwrap()->getPath());
578578
}
579579

580-
public static function appendSegmentProvider(): array
580+
public static function appendPathProvider(): array
581581
{
582582
return [
583583
['toto', '/path/to/the/sky.php/toto'],
584584
['le blanc', '/path/to/the/sky.php/le%20blanc'],
585585
];
586586
}
587587

588-
#[DataProvider('validAppendSegmentProvider')]
588+
#[DataProvider('validAppendPathProvider')]
589589
public function testAppendProcessWithRelativePath(string $uri, string $segment, string $expected): void
590590
{
591-
self::assertSame($expected, Modifier::wrap($uri)->appendSegment($segment)->toString());
591+
self::assertSame($expected, Modifier::wrap($uri)->appendPath($segment)->toString());
592592
}
593593

594-
public static function validAppendSegmentProvider(): array
594+
public static function validAppendPathProvider(): array
595595
{
596596
return [
597597
'uri with trailing slash' => [
@@ -656,14 +656,14 @@ public static function validDirnameProvider(): array
656656
];
657657
}
658658

659-
#[DataProvider('prependSegmentProvider')]
660-
public function testPrependProcess(string $uri, string $segment, string $expectedPath): void
659+
#[DataProvider('prependPathProvider')]
660+
public function testPrependPathProcess(string $uri, string $segment, string $expectedPath): void
661661
{
662662
$uri = Uri::new($uri);
663-
self::assertSame($expectedPath, Modifier::wrap($uri)->prependSegment($segment)->unwrap()->getPath());
663+
self::assertSame($expectedPath, Modifier::wrap($uri)->prependPath($segment)->unwrap()->getPath());
664664
}
665665

666-
public static function prependSegmentProvider(): array
666+
public static function prependPathProvider(): array
667667
{
668668
return [
669669
[

docs/components/7.0/modifiers.md

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ use League\Uri\Modifier;
130130
use Uri\WhatWg\Url;
131131

132132
$uri = Modifier::wrap(new Url('https://www.mypoems.net/the-book-of-mwana-kupona/'))
133-
->prependSegment('epic')
133+
->prependPath('epic')
134134
->removeLabels(-1)
135135
->replaceLabel(1, "africanpoems")
136136
->appendFragmentDirectives(new TextDirective(start: "Negema wangu binti", end: "neno lema kukwambia."))
@@ -313,8 +313,10 @@ to apply the following changes to the submitted URI.
313313
<li><a href="#modifierreplaceextension">replaceExtension</a></li>
314314
<li><a href="#modifieraddbasepath">addBasePath</a></li>
315315
<li><a href="#modifierremovebasepath">removeBasePath</a></li>
316-
<li><a href="#modifierappendsegment">appendSegment</a></li>
317-
<li><a href="#modifierprependsegment">prependSegment</a></li>
316+
<li><a href="#modifierappendpath">appendPath</a></li>
317+
<li><a href="#modifierprependpath">prependPath</a></li>
318+
<li><a href="#modifierappendsegments">appendSegments</a></li>
319+
<li><a href="#modifierprependsegments">prependSegments</a></li>
318320
<li><a href="#modifierreplacesegment">replaceSegment</a></li>
319321
<li><a href="#modifierremovesegments">removeSegments</a></li>
320322
<li><a href="#modifierslicesegments">sliceSegments</a></li>
@@ -1047,27 +1049,64 @@ echo Modifier::wrap($uri)
10471049
//display "/sky"
10481050
~~~
10491051

1050-
### Modifier::appendSegment
1052+
### Modifier::appendPath
1053+
1054+
<p class="message-notice">since version <code>7.7.0</code></p>
1055+
<p class="message-info">replaces and deprecates <code>Modifier::appendSegment</code> since <code>7.7.0</code></p>
1056+
10511057

10521058
Appends a path to the current URI path.
10531059

10541060
~~~php
10551061
$uri = Http::new("http://www.example.com/path/to/the/sky/");
10561062
echo Modifier::wrap($uri)
1057-
->appendSegment("and/above")
1063+
->appendPath("and/above")
1064+
->unwrap()
1065+
->getPath();
1066+
//display "/path/to/the/sky/and/above"
1067+
~~~
1068+
1069+
### Modifier::prependPath
1070+
1071+
<p class="message-notice">since version <code>7.7.0</code></p>
1072+
<p class="message-info">replaces and deprecates <code>Modifier::prependSegment</code> since <code>7.7.0</code></p>
1073+
1074+
Prepends a path to the current URI path.
1075+
1076+
~~~php
1077+
$uri = Http::new("http://www.example.com/path/to/the/sky/");
1078+
echo Modifier::wrap($uri)
1079+
->prependPath("and/above")
1080+
->unwrap()
1081+
->getPath();
1082+
//display "/and/above/path/to/the/sky/"
1083+
~~~
1084+
1085+
### Modifier::appendSegments
1086+
1087+
<p class="message-notice">since version <code>7.7.0</code></p>
1088+
1089+
Appends a list of path segments to the current URI path.
1090+
1091+
~~~php
1092+
$uri = Http::new("http://www.example.com/path/to/the/sky/");
1093+
echo Modifier::wrap($uri)
1094+
->appendSegments(["and","above"])
10581095
->unwrap()
10591096
->getPath();
10601097
//display "/path/to/the/sky/and/above"
10611098
~~~
10621099

1063-
### Modifier::prependSegment
1100+
### Modifier::prependSegments
1101+
1102+
<p class="message-notice">since version <code>7.7.0</code></p>
10641103

10651104
Prepends a path to the current URI path.
10661105

10671106
~~~php
10681107
$uri = Http::new("http://www.example.com/path/to/the/sky/");
10691108
echo Modifier::wrap($uri)
1070-
->prependSegment("and/above")
1109+
->prependSegments(["and","above"])
10711110
->unwrap()
10721111
->getPath();
10731112
//display "/and/above/path/to/the/sky/"

interfaces/Contracts/SegmentedPathInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function keys(Stringable|string|null $segment = null): array;
7171
/**
7272
* Appends a segment to the path.
7373
*/
74-
public function append(Stringable|string $segment): self;
74+
public function append(Stringable|string $path): self;
7575

7676
/**
7777
* Extracts a slice of $length elements starting at position $offset from the host.
@@ -86,7 +86,7 @@ public function slice(int $offset, ?int $length = null): self;
8686
/**
8787
* Prepends a segment to the path.
8888
*/
89-
public function prepend(Stringable|string $segment): self;
89+
public function prepend(Stringable|string $path): self;
9090

9191
/**
9292
* Returns an instance with the modified segment.

0 commit comments

Comments
 (0)