Skip to content

Commit

Permalink
Merge pull request #53 from sunrise-php/release/v2.3.0
Browse files Browse the repository at this point in the history
Named patterns
  • Loading branch information
fenric committed Mar 17, 2020
2 parents 3335df3 + 594ba20 commit f3ac5e9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions functions/path_parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
'_' => 1,
];

/**
* Named patters
*
* @var array
*/
const NAMED_PATTERNS = [
'@slug' => '[\da-z-]+',
'@uuid' => '[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}',
];

/**
* Parses the given path
*
Expand Down Expand Up @@ -169,6 +179,10 @@ function path_parse(string $path) : array
);
}

if (isset(NAMED_PATTERNS[$attributes[$attributeIndex]['pattern']])) {
$attributes[$attributeIndex]['pattern'] = NAMED_PATTERNS[$attributes[$attributeIndex]['pattern']];
}

$cursorInPattern = false;

$attributes[$attributeIndex]['raw'] .= $char;
Expand Down
36 changes: 36 additions & 0 deletions tests/Functions/PathParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
use function Sunrise\Http\Router\path_parse;
use function chr;

/**
* Import constants
*/
use const Sunrise\Http\Router\NAMED_PATTERNS;

/**
* PathParseTest
*/
Expand Down Expand Up @@ -60,6 +65,37 @@ public function testParsePath() : void
$this->assertSame($expected, path_parse($path));
}

/**
* @return void
*/
public function testNamedPatterns() : void
{
$path = '/{foo<@slug>}/{bar<@uuid>}';

$expected = [
[
'raw' => '{foo<@slug>}',
'withParentheses' => null,
'name' => 'foo',
'pattern' => NAMED_PATTERNS['@slug'],
'isOptional' => false,
'startPosition' => 1,
'endPosition' => 12,
],
[
'raw' => '{bar<@uuid>}',
'withParentheses' => null,
'name' => 'bar',
'pattern' => NAMED_PATTERNS['@uuid'],
'isOptional' => false,
'startPosition' => 14,
'endPosition' => 25,
],
];

$this->assertSame($expected, path_parse($path));
}

/**
* @return void
*/
Expand Down

0 comments on commit f3ac5e9

Please sign in to comment.