-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* next: (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
Showing
61 changed files
with
2,865 additions
and
1,915 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => '&', | ||
}; | ||
} | ||
} |
Oops, something went wrong.