Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop: (39 commits)
  CS
  fix readme
  only expose the Template class to the users
  remove wrapping the exception explaining why the match is not possible
  return an empty map instead of throwing when the url does not match the template
  remove the need to use variadics
  consider invalid types for expansions as non existing values
  use more precise type for the Map of values to expand
  remove the use of state to build template regex
  fix parsing level 4 composite
  force each expression to expose its expansion
  remove duplicated code
  no need to keep the lead and separator characters in memory
  no need to keep the separator in memory
  determine the separator to use based on the expansion
  use an enum instead of literal strings
  use enum instead of literal strings
  fix allowed names
  deduplicate the allowed characters in an expression name
  use Expansion enum instead of literal strings
  ...
  • Loading branch information
Baptouuuu committed Aug 6, 2022
2 parents df75f04 + 2ee0b32 commit 51c4da3
Show file tree
Hide file tree
Showing 61 changed files with 2,865 additions and 1,915 deletions.
47 changes: 10 additions & 37 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
php-version: ['7.4', '8.0']
php-version: ['8.1']
name: 'PHPUnit'
steps:
- name: Checkout
Expand All @@ -19,17 +19,8 @@ jobs:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install
- name: Composer
uses: "ramsey/composer-install@v2"
- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.clover
- uses: codecov/codecov-action@v1
Expand All @@ -39,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4', '8.0']
php-version: ['8.1']
name: 'Psalm'
steps:
- name: Checkout
Expand All @@ -49,24 +40,15 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install
- name: Composer
uses: "ramsey/composer-install@v2"
- name: Psalm
run: vendor/bin/psalm --shepherd
cs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4']
php-version: ['8.1']
name: 'CS'
steps:
- name: Checkout
Expand All @@ -76,16 +58,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Composer
uses: "ramsey/composer-install@v2"
- name: CS
run: vendor/bin/php-cs-fixer fix --diff --dry-run --diff-format udiff
run: vendor/bin/php-cs-fixer fix --diff --dry-run
File renamed without changes.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use Innmind\Immutable\Map;
use Innmind\Url\Url;

$url = Template::of('http://example.com/dictionary/{term:1}/{term}')->expand(
Map::of('string', 'scalar|array')
('term', 'dog')
Map::of(['term', 'dog']),
);
$url instanceof Url; // true
$url->toString(); // http://example.com/dictionary/d/dog
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"authors": [
{
"name": "Baptiste Langlade",
"email": "langlade.baptiste@gmail.com"
"email": "baptiste.langlade@hey.com"
}
],
"support": {
"issues": "http://github.com/Innmind/UrlTemplate/issues"
},
"require": {
"php": "~7.4|~8.0",
"innmind/url": "~3.0",
"innmind/immutable": "~3.1"
"php": "~8.1",
"innmind/url": "~4.1",
"innmind/immutable": "~4.3"
},
"autoload": {
"psr-4": {
Expand All @@ -33,6 +33,6 @@
"phpunit/phpunit": "~9.0",
"vimeo/psalm": "~4.4",
"innmind/black-box": "^4.16",
"innmind/coding-standard": "^1.1"
"innmind/coding-standard": "~2.0"
}
}
12 changes: 0 additions & 12 deletions src/Exception/ExpressionLimitCantBeNegative.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/ExtractionNotSupported.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/OnlyScalarCanBeExpandedForExpression.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/RuntimeException.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Exception/UrlDoesntMatchTemplate.php

This file was deleted.

19 changes: 12 additions & 7 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@

namespace Innmind\UrlTemplate;

use Innmind\UrlTemplate\{
Expression\Name,
Exception\DomainException,
};
use Innmind\Immutable\{
Map,
Str,
Maybe,
};

/**
* @psalm-immutable
* @internal
*/
interface Expression
{
/**
* @throws DomainException
* @psalm-pure
*
* @return Maybe<self>
*/
public static function of(Str $string): self;
public static function of(Str $string): Maybe;

public function expansion(): Expression\Expansion;

/**
* @param Map<string, scalar|array> $variables
* @param Map<non-empty-string, string|list<string>|list<array{string, string}>> $variables
*/
public function expand(Map $variables): string;
public function regex(): string;
Expand Down
143 changes: 143 additions & 0 deletions src/Expression/Expansion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
declare(strict_types = 1);

namespace Innmind\UrlTemplate\Expression;

use Innmind\Immutable\Str;

/**
* @psalm-immutable
* @internal
*/
enum Expansion
{
case simple;
case reserved;
case fragment;
case label;
case path;
case parameter;
case query;
case queryContinuation;

/**
* @psalm-pure
*/
public static function matchesLevel4(Str $value): bool
{
return $value->matches(\sprintf(
'~^\{[\+#\./;\?&]?%s(\*|:\d*)?(,%s(\*|:\d*)?)*\}$~',
Name::characters(),
Name::characters(),
));
}

public function clean(Str $value): Str
{
$drop = match ($this) {
self::simple => 1,
default => 2,
};

return $value->drop($drop)->dropEnd(1);
}

public function cleanExplode(Str $value): Str
{
return $this->clean($value)->dropEnd(1);
}

public function matches(Str $value): bool
{
return $value->matches(\sprintf(
'~^\{%s%s\}$~',
$this->regex(),
Name::characters(),
));
}

public function matchesExplode(Str $value): bool
{
return $value->matches(\sprintf(
'~^\{%s%s\*\}$~',
$this->regex(),
Name::characters(),
));
}

public function matchesLimit(Str $value): bool
{
return $value->matches(\sprintf(
'~^\{%s%s:\d+\}$~',
$this->regex(),
Name::characters(),
));
}

public function matchesMany(Str $value): bool
{
return $value->matches(\sprintf(
'~^\{%s%s(,%s)*\}$~',
$this->regex(),
Name::characters(),
Name::characters(),
));
}

public function continuation(): self
{
return match ($this) {
self::query => self::queryContinuation,
default => $this,
};
}

public function separator(): string
{
return match ($this) {
self::simple => ',',
self::reserved => ',',
self::fragment => ',',
default => '',
};
}

public function separatorRegex(): string
{
return match ($this->separator()) {
'' => '',
',' => '\\,',
};
}

public function explodeSeparator(): string
{
return match ($this) {
self::label => '.',
self::path => '/',
default => ',',
};
}

public function regex(): string
{
return match ($this) {
self::simple => '',
default => '\\'.$this->toString(),
};
}

public function toString(): string
{
return match ($this) {
self::simple => '',
self::reserved => '+',
self::fragment => '#',
self::label => '.',
self::path => '/',
self::parameter => ';',
self::query => '?',
self::queryContinuation => '&',
};
}
}
Loading

0 comments on commit 51c4da3

Please sign in to comment.