Skip to content

Commit

Permalink
Update CHANGELOG for next release
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jun 17, 2023
1 parent baa3246 commit db2bec0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 51 deletions.
23 changes: 20 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,37 @@ All Notable changes to `League\Uri` will be documented in this file

### Added

- `League\Uri\UriTemplate\Operator` to improve internal representation when using UriTemplate features.
- `League\Uri\UriTemplate\Template` is now part of the public API

### Fixed

- `UriResolver` and `UriInfo` uri input now supports `Stringable` and `string` type.
- `League\Uri\UriResolver` and `League\Uri\UriInfo` uri input now supports `Stringable` and `string` type.
- `League\Uri\UriTemplate`, `League\Uri\UriTemplate\Template`, `League\Uri\UriTemplate\VariableBag` are the only classes that are part of the public API. All the other classes are internal by default.
- `League\Uri\UriTemplate\VariableBag` implements the `IteratorAggregate` interface
- `League\Uri\UriTemplate\Operator` to improve internal representation when using UriTemplate features.

### Deprecated

- None
- `League\Uri\Uri::createFromString` use `League\Uri\Uri::fromString`
- `League\Uri\Uri::createFromComponents` use `League\Uri\Uri::fromComponents`
- `League\Uri\Uri::createFromUri` use `League\Uri\Uri::fromUri`
- `League\Uri\Uri::createFromBaseUri` use `League\Uri\Uri::fromBaseUri`
- `League\Uri\Uri::createFromServer` use `League\Uri\Uri::fromServer`
- `League\Uri\Uri::createFromWindowsPath` use `League\Uri\Uri::fromWindowsPath`
- `League\Uri\Uri::createFromUnixPath` use `League\Uri\Uri::fromUnixPath`
- `League\Uri\Uri::createFromDataPath` use `League\Uri\Uri::fromDataPath`
- `League\Uri\Http::createFromString` use `League\Uri\Http::fromString`
- `League\Uri\Http::createFromComponents` use `League\Uri\Http::fromComponents`
- `League\Uri\Http::createFromUri` use `League\Uri\Http::fromUri`
- `League\Uri\Http::createFromBaseUri` use `League\Uri\Http::fromBaseUri`
- `League\Uri\Http::createFromServer` use `League\Uri\Http::fromServer`
- `League\Uri\UriTemplate\Template::createFromString` use `League\Uri\UriTemplate\Template::fromString`

### Remove

- Support for `__set_state`
- `League\Uri\UriTemplate\VariableBag::all`
- Support for `PSR-7` v1

## [6.8.0](https://github.com/thephpleague/uri/compare/6.7.2...6.8.0) - 2022-09-13

Expand Down
2 changes: 1 addition & 1 deletion UriResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private static function resolvePathAndQuery(
}

/**
* Relativizes an URI according to a base URI.
* Relativizes a URI according to a base URI.
*
* This method MUST retain the state of the submitted URI instance, and return
* an URI instance of the same type that contains the applied modifications.
Expand Down
41 changes: 13 additions & 28 deletions UriTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,10 @@ final class UriTemplate
* @throws SyntaxError if the template syntax is invalid
* @throws TemplateCanNotBeExpanded if the template variables are invalid
*/
public function __construct(
Template|Stringable|string $template,
VariableBag|iterable $defaultVariables = new VariableBag()
) {
$this->template = $template instanceof Template ? $template : Template::fromString($template);
$this->defaultVariables = $this->filterVariables($defaultVariables);
}

/**
* Filters out variables for the given template.
*/
private function filterVariables(VariableBag|iterable $inputVariables): VariableBag
public function __construct(Template|Stringable|string $template, iterable $defaultVariables = [])
{
if (!$inputVariables instanceof VariableBag) {
$inputVariables = new VariableBag($inputVariables);
}

$variableBag = new VariableBag();
foreach ($this->template->variableNames as $name) {
if (isset($inputVariables[$name])) {
$variableBag[$name] = $inputVariables[$name];
}
}

return $variableBag;
$this->template = $template instanceof Template ? $template : Template::fromString($template);
$this->defaultVariables = VariableBag::fromTemplate($this->template, $defaultVariables);
}

/**
Expand All @@ -77,20 +56,26 @@ private function filterVariables(VariableBag|iterable $inputVariables): Variable
* If present, variables whose name is not part of the current template
* possible variable names are removed.
*/
public function withDefaultVariables(VariableBag|iterable $defaultDefaultVariables): self
public function withDefaultVariables(iterable $defaultVariables): self
{
return new self($this->template, $defaultDefaultVariables);
$variables = VariableBag::fromTemplate($this->template, $defaultVariables);
if ($variables == $this->defaultVariables) {
return $this;
}

return new self($this->template, $defaultVariables);
}

/**
* @throws TemplateCanNotBeExpanded if the variable contains nested array values
* @throws UriException if the resulting expansion can not be converted to a UriInterface instance
*/
public function expand(VariableBag|iterable $variables = new VariableBag()): UriInterface
public function expand(iterable $variables = []): UriInterface
{
return Uri::fromString(
$this->template->expand(
$this->filterVariables($variables)->replace($this->defaultVariables)
VariableBag::fromTemplate($this->template, $variables)
->replace($this->defaultVariables)
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion UriTemplate/VarSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use function preg_match;

/**
* @internal The class exposes the internal representation of an Var Specifier
* @internal The class exposes the internal representation of a Var Specifier
* @link https://www.rfc-editor.org/rfc/rfc6570#section-2.3
*/
final class VarSpecifier
Expand Down
44 changes: 32 additions & 12 deletions UriTemplate/VariableBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@

use ArrayAccess;
use Countable;
use IteratorAggregate;
use League\Uri\Exceptions\TemplateCanNotBeExpanded;
use Stringable;
use Traversable;
use function is_bool;
use function is_object;
use function is_scalar;

/**
* @internal The class exposes the internal representation of variable bags
*
* @implements ArrayAccess<string, string|bool|int|float|array<string|bool|int|float>>
* @phpstan-type InputValue string|bool|int|float|array<string|bool|int|float>
* @implements ArrayAccess<string, InputValue>
* @implements IteratorAggregate<string, InputValue>
*/
final class VariableBag implements ArrayAccess, Countable
final class VariableBag implements ArrayAccess, Countable, IteratorAggregate
{
/**
* @var array<string,string|array<string>>
Expand All @@ -43,11 +46,36 @@ public function __construct(iterable $variables = [])
}
}

public static function fromTemplate(Template $template, iterable $inputVariables): self
{
if (!$inputVariables instanceof VariableBag) {
$inputVariables = new VariableBag($inputVariables);
}

$variableBag = [];
foreach ($template->variableNames as $name) {
if (isset($inputVariables[$name])) {
$variableBag[$name] = $inputVariables[$name];
}
}

if ($inputVariables->variables === $variableBag) {
return $inputVariables;
}

return new self($variableBag);
}

public function count(): int
{
return count($this->variables);
}

public function getIterator(): Traversable
{
yield from $this->variables;
}

public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->variables);
Expand All @@ -68,14 +96,6 @@ public function offsetGet(mixed $offset): mixed
return $this->fetch($offset);
}

/**
* @return array<string,string|array<string>>
*/
public function all(): array
{
return $this->variables;
}

/**
* Tells whether the bag is empty or not.
*/
Expand Down Expand Up @@ -119,7 +139,7 @@ private function normalizeValue(Stringable|array|string|float|int|bool|null $val
{
return match (true) {
is_bool($value) => true === $value ? '1' : '0',
(null === $value || is_scalar($value) || is_object($value)) => (string) $value,
(null === $value || is_scalar($value) || $value instanceof Stringable) => (string) $value,
!$isNestedListAllowed => throw TemplateCanNotBeExpanded::dueToNestedListOfValue($name),
default => array_map(fn ($var): array|string => self::normalizeValue($var, $name, false), $value),
};
Expand Down
2 changes: 1 addition & 1 deletion UriTemplate/VariableBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function testItCanBeInstantiatedWithAnIterable(
): void {
$bag = new VariableBag($iterable);

self::assertEquals($expected, $bag->all());
self::assertSame($isEmpty, $bag->isEmpty());
self::assertSame(!$isEmpty, $bag->isNotEmpty());
self::assertCount($count, $bag);
self::assertEquals($expected, iterator_to_array($bag));
}

public static function provideValidIterable(): iterable
Expand Down
10 changes: 5 additions & 5 deletions UriTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function testGetDefaultVariables(): void
];

$uriTemplate = new UriTemplate($template, $variables);
self::assertSame($expectedVariables, $uriTemplate->defaultVariables->all());
self::assertSame($expectedVariables, [...$uriTemplate->defaultVariables]);
self::assertFalse($uriTemplate->defaultVariables->isEmpty());

$uriTemplateEmpty = new UriTemplate($template, []);
self::assertSame([], $uriTemplateEmpty->defaultVariables->all());
self::assertSame([], [...$uriTemplateEmpty->defaultVariables]);
self::assertTrue($uriTemplateEmpty->defaultVariables->isEmpty());
}

Expand All @@ -79,9 +79,9 @@ public function testWithDefaultVariables(): void
$altTemplate = $uriTemplate->withDefaultVariables($variables);
$newAltTemplate = $uriTemplate->withDefaultVariables($newAltVariables);

self::assertSame($altTemplate->defaultVariables->all(), $uriTemplate->defaultVariables->all());
self::assertSame($newAltTemplate->defaultVariables->all(), $uriTemplate->defaultVariables->all());
self::assertNotSame($newTemplate->defaultVariables->all(), $uriTemplate->defaultVariables->all());
self::assertEquals($altTemplate->defaultVariables, $uriTemplate->defaultVariables);
self::assertEquals($newAltTemplate->defaultVariables, $uriTemplate->defaultVariables);
self::assertNotEquals($newTemplate->defaultVariables, $uriTemplate->defaultVariables);
}

/**
Expand Down

0 comments on commit db2bec0

Please sign in to comment.