-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathPostfixInflector.php
More file actions
85 lines (73 loc) · 2.37 KB
/
PostfixInflector.php
File metadata and controls
85 lines (73 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace BeSimple\I18nRoutingBundle\Routing\RouteGenerator\NameInflector;
use Symfony\Component\Routing\RouteCollection;
/**
* A route name inflector that appends the locale to the routes name.
*/
class PostfixInflector implements RouteNameInflectorInterface
{
const INFIX = '.be-simple-i18n.';
/**
* @inheritdoc
*/
public function inflect($name, $locale)
{
return $name . self::INFIX . $locale;
}
/**
* Reverse the inflection on an inflected route name.
*
* @param $name
* @param $locale
* @return string
*/
public function unInflect($name, $locale)
{
$truncateHere = strpos($name, self::INFIX);
return substr($name, 0, $truncateHere);
}
/**
* Checks if $name has been inflected by this inflector.
*
* @param $name
* @param $locale
* @return bool
*/
public function isInflected($name, $locale = '')
{
return false !== strpos($name, self::INFIX);
}
/**
* Checks if the constraints defined in the route definition are actually met.
*
* @param $name
* @param $locale
* @param RouteCollection $routeCollection
* @return bool
*/
public function isValidMatch($name, $locale, RouteCollection $routeCollection = null)
{
$matchedRoute = $this->unInflect($name, $locale);
// locale does not match postfixed locale
if ($locale !== ($otherLocale = substr($name, - strlen($locale)))) {
if (is_null($routeCollection)) {
return false;
}
// check if no another registered route does match, and then throw an exception
$otherRoute = $this->inflect($matchedRoute, $locale);
$allRoutes = $routeCollection->getIterator();
// there is no valid route for the other locale
if ( ! key_exists($otherRoute, $allRoutes)) {
return false;
}
// there is a valid route for the other locale, but does the pathinfo match?
$originalPathInfo = $allRoutes[$name]->getPath();
$otherPathInfo = $allRoutes[$otherRoute]->getPath();
if ($originalPathInfo !== $otherPathInfo) {
// mismatch
return false;
}
}
return true;
}
}