1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri\Components;
use Deprecated;
use League\Uri\Contracts\PathInterface;
use League\Uri\Contracts\UriInterface;
use League\Uri\Encoder;
use League\Uri\Uri;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
use function array_pop;
use function array_reduce;
use function end;
use function explode;
use function implode;
use function substr;
final class Path extends Component implements PathInterface
{
private const DOT_SEGMENTS = ['.' => 1, '..' => 1];
private const SEPARATOR = '/';
private readonly string $path;
/**
* New instance.
*/
private function __construct(Stringable|string $path)
{
$this->path = $this->validate($path);
}
/**
* Validate the component content.
*/
private function validate(Stringable|string $path): string
{
return (string) $this->validateComponent($path);
}
/**
* Returns a new instance from a string or a stringable object.
*/
public static function new(Stringable|string $value = ''): self
{
return new self($value);
}
/**
* Create a new instance from a URI object.
*/
public static function fromUri(Stringable|string $uri): self
{
if (!$uri instanceof UriInterface) {
$uri = Uri::new($uri);
}
$path = $uri->getPath();
$authority = $uri->getAuthority();
return match (true) {
null === $authority, '' === $authority, '' === $path, '/' === $path[0] => new self($path),
default => new self('/'.$path),
};
}
public function value(): ?string
{
return Encoder::encodePath($this->path);
}
public function decoded(): string
{
return $this->path;
}
public function isAbsolute(): bool
{
return self::SEPARATOR === ($this->path[0] ?? '');
}
public function hasTrailingSlash(): bool
{
return '' !== $this->path && self::SEPARATOR === substr($this->path, -1);
}
public function withoutDotSegments(): PathInterface
{
$current = $this->toString();
if (!str_contains($current, '.')) {
return $this;
}
$input = explode(self::SEPARATOR, $current);
$new = implode(self::SEPARATOR, array_reduce($input, $this->filterDotSegments(...), []));
if (isset(self::DOT_SEGMENTS[end($input)])) {
$new .= self::SEPARATOR ;
}
return new self($new);
}
/**
* Filter Dot segment according to RFC3986.
*
* @see http://tools.ietf.org/html/rfc3986#section-5.2.4
*
* @return string[]
*/
private function filterDotSegments(array $carry, string $segment): array
{
if ('..' === $segment) {
array_pop($carry);
return $carry;
}
if (!isset(self::DOT_SEGMENTS[$segment])) {
$carry[] = $segment;
}
return $carry;
}
/**
* Returns an instance with a trailing slash.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the path component with a trailing slash
*/
public function withTrailingSlash(): PathInterface
{
return $this->hasTrailingSlash() ? $this : new self($this->toString().self::SEPARATOR);
}
public function withoutTrailingSlash(): PathInterface
{
return !$this->hasTrailingSlash() ? $this : new self(substr($this->toString(), 0, -1));
}
public function withLeadingSlash(): PathInterface
{
return $this->isAbsolute() ? $this : new self(self::SEPARATOR.$this->toString());
}
public function withoutLeadingSlash(): PathInterface
{
return !$this->isAbsolute() ? $this : new self(substr($this->toString(), 1));
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see HierarchicalPath::new()
*
* @codeCoverageIgnore
*
* Returns a new instance from a string or a stringable object.
*/
#[Deprecated(message:'use League\Uri\Components\HierarchicalPath::new() instead', since:'league/uri-components:7.0.0')]
public static function createFromString(Stringable|string|int $path): self
{
return self::new((string) $path);
}
/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.0.0
* @see HierarchicalPath::fromUri()
*
* @codeCoverageIgnore
*
* Create a new instance from a URI object.
*/
#[Deprecated(message:'use League\Uri\Components\HierarchicalPath::fromUri() instead', since:'league/uri-components:7.0.0')]
public static function createFromUri(Psr7UriInterface|UriInterface $uri): self
{
return self::fromUri($uri);
}
}
|