Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

URIElement.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher;
8
9 use eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest;
10 use eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer;
11 use eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher;
12 use LogicException;
13
14 class URIElement implements VersatileMatcher, URILexer
15 {
17 private $request;
18
24 private $elementNumber;
25
31 private $uriElements;
32
38 public function __construct($elementNumber)
39 {
40 if (is_array($elementNumber)) {
41 // DI config parser will create an array with 'value' => number
42 $elementNumber = current($elementNumber);
43 }
44
45 $this->elementNumber = (int)$elementNumber;
46 }
47
48 public function __sleep()
49 {
50 return ['elementNumber', 'uriElements'];
51 }
52
58 public function match()
59 {
60 try {
61 return implode('_', $this->getURIElements());
62 } catch (LogicException $e) {
63 return false;
64 }
65 }
66
74 protected function getURIElements()
75 {
76 if (isset($this->uriElements)) {
77 return $this->uriElements;
78 } elseif (!isset($this->request)) {
79 return [];
80 }
81
82 $elements = array_slice(
83 explode('/', $this->request->pathinfo),
84 1,
85 $this->elementNumber
86 );
87
88 // If one of the elements is empty, we do not match.
89 foreach ($elements as $element) {
90 if ($element === '') {
91 throw new LogicException('One of the URI elements was empty');
92 }
93 }
94
95 if (count($elements) !== $this->elementNumber) {
96 throw new LogicException('The number of provided elements to consider is different than the number of elements found in the URI');
97 }
98
99 return $this->uriElements = $elements;
100 }
101
102 public function getName()
103 {
104 return 'uri:element';
105 }
106
112 public function setRequest(SimplifiedRequest $request)
113 {
114 $this->request = $request;
115 }
116
120 public function getRequest()
121 {
122 return $this->request;
123 }
124
132 public function analyseURI($uri)
133 {
134 $uriElements = '/' . implode('/', $this->getURIElements());
135 if ($uri == $uriElements) {
136 $uri = '';
137 } elseif (strpos($uri, $uriElements) === 0) {
138 $uri = mb_substr($uri, mb_strlen($uriElements));
139 }
140
141 return $uri;
142 }
143
151 public function analyseLink($linkUri)
152 {
153 // Joining slash between uriElements and actual linkUri must be present, except if $linkUri is empty.
154 $joiningSlash = empty($linkUri) ? '' : '/';
155 $linkUri = ltrim($linkUri, '/');
156 $uriElements = implode('/', $this->getURIElements());
157
158 return "/{$uriElements}{$joiningSlash}{$linkUri}";
159 }
160
173 public function reverseMatch($siteAccessName)
174 {
175 $elements = $this->elementNumber > 1 ? explode('_', $siteAccessName) : [$siteAccessName];
176 if (count($elements) !== $this->elementNumber) {
177 return null;
178 }
179
180 $pathinfo = '/' . implode('/', $elements) . '/' . ltrim($this->request->pathinfo, '/');
181 $this->request->setPathinfo($pathinfo);
182
183 return $this;
184 }
185 }
eZ\Publish\Core\MVC\Symfony\Routing\SimplifiedRequest
Definition: SimplifiedRequest.php:21
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement
Definition: URIElement.php:15
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\analyseURI
analyseURI($uri)
Analyses $uri and removes the siteaccess part, if needed.
Definition: URIElement.php:132
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\match
match()
Returns matching Siteaccess.
Definition: URIElement.php:58
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\$elementNumber
$elementNumber
Definition: URIElement.php:24
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\setRequest
setRequest(SimplifiedRequest $request)
Injects the request object to match against.
Definition: URIElement.php:112
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\analyseLink
analyseLink($linkUri)
Analyses $linkUri when generating a link to a route, in order to have the siteaccess part back in the...
Definition: URIElement.php:151
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\$uriElements
$uriElements
Definition: URIElement.php:31
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\getRequest
getRequest()
Definition: URIElement.php:120
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\reverseMatch
reverseMatch($siteAccessName)
Returns matcher object corresponding to $siteAccessName or null if non applicable.
Definition: URIElement.php:173
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\__sleep
__sleep()
Definition: URIElement.php:48
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\getURIElements
getURIElements()
Returns URI elements as an array.
Definition: URIElement.php:74
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\$request
$request
Definition: URIElement.php:17
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\__construct
__construct($elementNumber)
Constructor.
Definition: URIElement.php:38
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher\URIElement\getName
getName()
Returns the matcher's name.
Definition: URIElement.php:102
eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher
Interface for SiteAccess matchers.
Definition: Publish/Core/MVC/Symfony/SiteAccess/Matcher.php:15
eZ\Publish\Core\MVC\Symfony\SiteAccess\URILexer
Interface for SiteAccess matchers that need to alter the URI after matching.
Definition: URILexer.php:14
eZ\Publish\Core\MVC\Symfony\SiteAccess\VersatileMatcher
Interface for SiteAccess matchers.
Definition: VersatileMatcher.php:16
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.