tipo: modificación del cpanel y gitignore de la carpeta vendor
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url;
|
||||
|
||||
use Spatie\Url\Contracts\Validator;
|
||||
|
||||
abstract class BaseValidator implements Validator
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url\Contracts;
|
||||
|
||||
interface Validator
|
||||
{
|
||||
public function validate(): void;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class InvalidArgument extends InvalidArgumentException
|
||||
{
|
||||
public static function invalidScheme(string $scheme, array $allowedSchemes): static
|
||||
{
|
||||
$schemes = implode(', ', array_map(fn ($scheme) => "`{$scheme}`", $allowedSchemes));
|
||||
|
||||
return new static("The scheme `{$scheme}` isn't valid. It should be either {$schemes}.");
|
||||
}
|
||||
|
||||
public static function invalidUrl(string $url): static
|
||||
{
|
||||
return new static("The string `{$url}` is no valid url.");
|
||||
}
|
||||
|
||||
public static function segmentZeroDoesNotExist(): static
|
||||
{
|
||||
return new static("Segment 0 doesn't exist. Segments can be retrieved by using 1-based index or a negative index.");
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url;
|
||||
|
||||
class QueryParameterBag implements \Stringable
|
||||
{
|
||||
public function __construct(
|
||||
protected array $parameters = [],
|
||||
) {
|
||||
//
|
||||
}
|
||||
|
||||
public static function fromString(string $query = ''): static
|
||||
{
|
||||
if ($query === '') {
|
||||
return new static();
|
||||
}
|
||||
|
||||
$parameters = [];
|
||||
parse_str($query, $parameters);
|
||||
$parameters = array_map(fn ($param) => $param !== '' ? $param : null, $parameters);
|
||||
|
||||
return new static($parameters);
|
||||
}
|
||||
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
if ($this->has($key)) {
|
||||
return $this->parameters[$key];
|
||||
}
|
||||
|
||||
return is_callable($default) ? $default() : $default;
|
||||
}
|
||||
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->parameters);
|
||||
}
|
||||
|
||||
public function set(string $key, string|array $value): self
|
||||
{
|
||||
$this->parameters[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unset(string $key): self
|
||||
{
|
||||
unset($this->parameters[$key]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unsetAll(): self
|
||||
{
|
||||
$this->parameters = [];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return http_build_query($this->parameters, '', '&', PHP_QUERY_RFC3986);
|
||||
}
|
||||
}
|
||||
Vendored
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url;
|
||||
|
||||
use Spatie\Macroable\Macroable;
|
||||
use Stringable;
|
||||
|
||||
class Scheme implements Stringable
|
||||
{
|
||||
use Macroable;
|
||||
|
||||
protected string $scheme;
|
||||
|
||||
protected SchemeValidator $validator;
|
||||
|
||||
public function __construct(
|
||||
string $scheme = '',
|
||||
array|null $allowedSchemes = null,
|
||||
) {
|
||||
$this->validator = new SchemeValidator($allowedSchemes);
|
||||
|
||||
$this->setScheme($scheme);
|
||||
}
|
||||
|
||||
protected function validate(string $scheme): void
|
||||
{
|
||||
$this->validator->setScheme($scheme);
|
||||
|
||||
$this->validator->validate();
|
||||
}
|
||||
|
||||
public function getScheme(): string
|
||||
{
|
||||
return $this->scheme;
|
||||
}
|
||||
|
||||
public function setScheme(string $scheme): void
|
||||
{
|
||||
$sanitizedScheme = $this->validator::sanitizeScheme($scheme);
|
||||
|
||||
$this->validate($sanitizedScheme);
|
||||
|
||||
$this->scheme = $sanitizedScheme;
|
||||
}
|
||||
|
||||
public function getAllowedSchemes(): array
|
||||
{
|
||||
return $this->validator->getAllowedSchemes();
|
||||
}
|
||||
|
||||
public function setAllowedSchemes(array $allowedSchemes): void
|
||||
{
|
||||
$this->validator->setAllowedSchemes($allowedSchemes);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->getScheme();
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->validator = clone $this->validator;
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url;
|
||||
|
||||
use Spatie\Url\Exceptions\InvalidArgument;
|
||||
|
||||
class SchemeValidator extends BaseValidator
|
||||
{
|
||||
public const VALID_SCHEMES = ['http', 'https', 'mailto', 'tel'];
|
||||
|
||||
private string|null $scheme;
|
||||
|
||||
public function __construct(
|
||||
private array|null $allowedSchemes = null
|
||||
) {
|
||||
$this->scheme = null;
|
||||
$this->allowedSchemes = $allowedSchemes ?? self::VALID_SCHEMES;
|
||||
}
|
||||
|
||||
public function validate(): void
|
||||
{
|
||||
// '' aka "no scheme" must always be valid
|
||||
$alwaysAllowedSchemes = [''];
|
||||
|
||||
if (! in_array($this->scheme, [...$this->allowedSchemes, ...$alwaysAllowedSchemes])) {
|
||||
throw InvalidArgument::invalidScheme($this->scheme, $this->allowedSchemes);
|
||||
}
|
||||
}
|
||||
|
||||
public static function sanitizeScheme(string $scheme): string
|
||||
{
|
||||
// TODO: regex to allow correct format according to https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
|
||||
return strtolower($scheme);
|
||||
}
|
||||
|
||||
public function getScheme(): string|null
|
||||
{
|
||||
return $this->scheme;
|
||||
}
|
||||
|
||||
public function setScheme(string $scheme): void
|
||||
{
|
||||
$this->scheme = $scheme;
|
||||
}
|
||||
|
||||
public function getAllowedSchemes(): array|null
|
||||
{
|
||||
return $this->allowedSchemes;
|
||||
}
|
||||
|
||||
public function setAllowedSchemes(array $allowedSchemes): void
|
||||
{
|
||||
$this->allowedSchemes = array_map(
|
||||
fn ($scheme) => static::sanitizeScheme($scheme),
|
||||
$allowedSchemes
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+373
@@ -0,0 +1,373 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Url;
|
||||
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Spatie\Macroable\Macroable;
|
||||
use Spatie\Url\Exceptions\InvalidArgument;
|
||||
use Stringable;
|
||||
|
||||
class Url implements UriInterface, Stringable
|
||||
{
|
||||
use Macroable;
|
||||
|
||||
protected Scheme $scheme;
|
||||
|
||||
protected string $host = '';
|
||||
|
||||
protected ?int $port = null;
|
||||
|
||||
protected string $user = '';
|
||||
|
||||
protected ?string $password = null;
|
||||
|
||||
protected string $path = '';
|
||||
|
||||
protected QueryParameterBag $query;
|
||||
|
||||
protected string $fragment = '';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->scheme = new Scheme();
|
||||
$this->query = new QueryParameterBag();
|
||||
}
|
||||
|
||||
public static function create(): static
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
public static function fromString(string $url, array|null $allowedSchemes = null): static
|
||||
{
|
||||
$toUrl = new static();
|
||||
|
||||
if($allowedSchemes !== null) {
|
||||
$toUrl = $toUrl->withAllowedSchemes($allowedSchemes);
|
||||
}
|
||||
|
||||
return static::make($url, $toUrl);
|
||||
}
|
||||
|
||||
protected static function make(string $fromUrl, self $toUrl): static
|
||||
{
|
||||
if (! $parts = parse_url($fromUrl)) {
|
||||
throw InvalidArgument::invalidUrl($fromUrl);
|
||||
}
|
||||
|
||||
$toUrl->scheme->setScheme(isset($parts['scheme']) ? $parts['scheme'] : '');
|
||||
$toUrl->host = $parts['host'] ?? '';
|
||||
$toUrl->port = $parts['port'] ?? null;
|
||||
$toUrl->user = $parts['user'] ?? '';
|
||||
$toUrl->password = $parts['pass'] ?? null;
|
||||
$toUrl->path = $parts['path'] ?? '/';
|
||||
$toUrl->query = QueryParameterBag::fromString($parts['query'] ?? '');
|
||||
$toUrl->fragment = $parts['fragment'] ?? '';
|
||||
|
||||
return $toUrl;
|
||||
}
|
||||
|
||||
public function getScheme(): string
|
||||
{
|
||||
return $this->scheme;
|
||||
}
|
||||
|
||||
public function getAuthority(): string
|
||||
{
|
||||
$authority = $this->host;
|
||||
|
||||
if ($this->getUserInfo()) {
|
||||
$authority = $this->getUserInfo().'@'.$authority;
|
||||
}
|
||||
|
||||
if ($this->port !== null) {
|
||||
$authority .= ':'.$this->port;
|
||||
}
|
||||
|
||||
return $authority;
|
||||
}
|
||||
|
||||
public function getUserInfo(): string
|
||||
{
|
||||
$userInfo = $this->user;
|
||||
|
||||
if ($this->password !== null) {
|
||||
$userInfo .= ':'.$this->password;
|
||||
}
|
||||
|
||||
return $userInfo;
|
||||
}
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
public function getPort(): ?int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
public function getPath(): string
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function getBasename(): string
|
||||
{
|
||||
return $this->getSegment(-1);
|
||||
}
|
||||
|
||||
public function getDirname(): string
|
||||
{
|
||||
$segments = $this->getSegments();
|
||||
|
||||
array_pop($segments);
|
||||
|
||||
return '/'.implode('/', $segments);
|
||||
}
|
||||
|
||||
public function getQuery(): string
|
||||
{
|
||||
return (string) $this->query;
|
||||
}
|
||||
|
||||
public function getQueryParameter(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return $this->query->get($key, $default);
|
||||
}
|
||||
|
||||
public function hasQueryParameter(string $key): bool
|
||||
{
|
||||
return $this->query->has($key);
|
||||
}
|
||||
|
||||
public function getAllQueryParameters(): array
|
||||
{
|
||||
return $this->query->all();
|
||||
}
|
||||
|
||||
public function withQueryParameter(string $key, string $value): static
|
||||
{
|
||||
$url = clone $this;
|
||||
$url->query->unset($key);
|
||||
|
||||
$url->query->set($key, $value);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withQueryParameters(array $parameters): static
|
||||
{
|
||||
$parameters = array_merge($this->getAllQueryParameters(), $parameters);
|
||||
$url = clone $this;
|
||||
$url->query = new QueryParameterBag($parameters);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withoutQueryParameter(string $key): static
|
||||
{
|
||||
$url = clone $this;
|
||||
$url->query->unset($key);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withoutQueryParameters(): static
|
||||
{
|
||||
$url = clone $this;
|
||||
$url->query->unsetAll();
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function getFragment(): string
|
||||
{
|
||||
return $this->fragment;
|
||||
}
|
||||
|
||||
public function getSegments(): array
|
||||
{
|
||||
return explode('/', trim($this->path, '/'));
|
||||
}
|
||||
|
||||
public function getSegment(int $index, mixed $default = null): mixed
|
||||
{
|
||||
$segments = $this->getSegments();
|
||||
|
||||
if ($index === 0) {
|
||||
throw InvalidArgument::segmentZeroDoesNotExist();
|
||||
}
|
||||
|
||||
if ($index < 0) {
|
||||
$segments = array_reverse($segments);
|
||||
$index = abs($index);
|
||||
}
|
||||
|
||||
return $segments[$index - 1] ?? $default;
|
||||
}
|
||||
|
||||
public function getFirstSegment(): mixed
|
||||
{
|
||||
$segments = $this->getSegments();
|
||||
|
||||
return $segments[0] ?? null;
|
||||
}
|
||||
|
||||
public function getLastSegment(): mixed
|
||||
{
|
||||
$segments = $this->getSegments();
|
||||
|
||||
return end($segments) ?? null;
|
||||
}
|
||||
|
||||
public function withScheme($scheme): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->scheme->setScheme($scheme);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withAllowedSchemes(array $schemes): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->scheme->setAllowedSchemes($schemes);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withUserInfo($user, $password = null): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->user = $user;
|
||||
$url->password = $password;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withHost($host): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->host = $host;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withPort($port): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->port = $port;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withPath($path): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
if (! str_starts_with($path, '/')) {
|
||||
$path = '/'.$path;
|
||||
}
|
||||
|
||||
$url->path = $path;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withDirname(string $dirname): static
|
||||
{
|
||||
$dirname = trim($dirname, '/');
|
||||
|
||||
if (! $this->getBasename()) {
|
||||
return $this->withPath($dirname);
|
||||
}
|
||||
|
||||
return $this->withPath($dirname.'/'.$this->getBasename());
|
||||
}
|
||||
|
||||
public function withBasename(string $basename): static
|
||||
{
|
||||
$basename = trim($basename, '/');
|
||||
|
||||
if ($this->getDirname() === '/') {
|
||||
return $this->withPath('/'.$basename);
|
||||
}
|
||||
|
||||
return $this->withPath($this->getDirname().'/'.$basename);
|
||||
}
|
||||
|
||||
public function withQuery($query): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->query = QueryParameterBag::fromString($query);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function withFragment($fragment): static
|
||||
{
|
||||
$url = clone $this;
|
||||
|
||||
$url->fragment = $fragment;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function matches(self $url): bool
|
||||
{
|
||||
return (string) $this === (string) $url;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$url = '';
|
||||
|
||||
if ($this->getScheme() !== '' && ! in_array($this->getScheme(), ['mailto', 'tel'], true)) {
|
||||
$url .= $this->getScheme().'://';
|
||||
}
|
||||
|
||||
if (in_array($this->getScheme(), ['mailto', 'tel'], true) && $this->getPath() !== '') {
|
||||
$url .= $this->getScheme().':';
|
||||
}
|
||||
|
||||
if ($this->getScheme() === '' && $this->getAuthority() !== '') {
|
||||
$url .= '//';
|
||||
}
|
||||
|
||||
if ($this->getAuthority() !== '') {
|
||||
$url .= $this->getAuthority();
|
||||
}
|
||||
|
||||
if ($this->getPath() !== '/') {
|
||||
$path = in_array($this->getScheme(), ['mailto', 'tel'], true)
|
||||
? ltrim($this->getPath(), '/')
|
||||
: $this->getPath();
|
||||
|
||||
$url .= $path;
|
||||
}
|
||||
|
||||
if ($this->getQuery() !== '') {
|
||||
$url .= '?'.$this->getQuery();
|
||||
}
|
||||
|
||||
if ($this->getFragment() !== '') {
|
||||
$url .= '#'.$this->getFragment();
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->query = clone $this->query;
|
||||
$this->scheme = clone $this->scheme;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user