55namespace MaplePHP \Http ;
66
77use MaplePHP \Http \Interfaces \PathInterface ;
8+ use Psr \Http \Message \RequestInterface ;
9+ use Psr \Http \Message \ServerRequestInterface ;
10+ use Psr \Http \Message \UriInterface ;
811
912class Path implements PathInterface
1013{
11- private $ parts ;
12- private $ vars ;
14+ private array $ parts ;
15+ private array $ vars ;
16+ private ServerRequestInterface |RequestInterface $ request ;
1317
14- public function __construct (array $ parts )
18+ public function __construct (array $ parts, ServerRequestInterface | RequestInterface $ request )
1519 {
1620 $ this ->parts = $ parts ;
1721 $ this ->vars = $ this ->partsToVars ($ parts );
22+ $ this ->request = $ request ;
1823 }
1924
25+ /**
26+ * Get PSR URI instance with whitelisted path and
27+ * cleared, query and fragments
28+ *
29+ * @return UriInterface
30+ */
31+ public function uri (): UriInterface
32+ {
33+ return $ this ->request ->getUri ()
34+ ->withPath (implode ("/ " , $ this ->vars ))
35+ ->withQuery ("" )
36+ ->withFragment ("" );
37+ }
38+
39+ /**
40+ * Get current full URL with whitelisted path
41+ *
42+ * @return string
43+ */
44+ public function url (): string
45+ {
46+ return $ this ->uri ()->getUri ();
47+ }
48+
2049 /**
2150 * With URI path type key
2251 *
@@ -44,6 +73,7 @@ public function withType(null|string|array $type): self
4473
4574 /**
4675 * Same as withType except that you Need to select a part
76+ *
4777 * @param string|array $type
4878 * @return static
4979 */
@@ -54,6 +84,7 @@ public function select(string|array $type): self
5484
5585 /**
5686 * Same as withType except it will only reset
87+ *
5788 * @return static
5889 */
5990 public function reset (): self
@@ -97,6 +128,7 @@ public function prepend(array|string $arr): self
97128
98129 /**
99130 * Get vars/path as array
131+ *
100132 * @return array
101133 */
102134 public function vars (): array
@@ -106,6 +138,7 @@ public function vars(): array
106138
107139 /**
108140 * Get vars/path as array
141+ *
109142 * @return array
110143 */
111144 public function parts (): array
@@ -115,21 +148,14 @@ public function parts(): array
115148
116149 /**
117150 * Get expected slug from path
118- * @return string
151+ *
152+ * @return array
119153 */
120- public function get (): string
154+ public function get (): array
121155 {
122- return $ this ->last ();
156+ return array_filter ( explode ( " / " , $ this ->last ()) );
123157 }
124158
125- /**
126- * Get expected slug from path
127- * @return string
128- */
129- public function current (): string
130- {
131- return $ this ->last ();
132- }
133159
134160 /**
135161 * Get last path item
@@ -138,10 +164,8 @@ public function current(): string
138164 */
139165 public function last (): string
140166 {
141- if ($ this ->vars === null ) {
142- $ this ->vars = $ this ->getVars ();
143- }
144- return end ($ this ->vars );
167+ $ end = end ($ this ->vars );
168+ return is_string ($ end ) ? $ end : '' ;
145169 }
146170
147171 /**
@@ -151,10 +175,8 @@ public function last(): string
151175 */
152176 public function first (): string
153177 {
154- if ($ this ->vars === null ) {
155- $ this ->vars = $ this ->getVars ();
156- }
157- return reset ($ this ->vars );
178+ $ reset = reset ($ this ->vars );
179+ return is_string ($ reset ) ? $ reset : '' ;
158180 }
159181
160182 /**
@@ -164,9 +186,6 @@ public function first(): string
164186 */
165187 public function prev (): string
166188 {
167- if ($ this ->vars === null ) {
168- $ this ->end ();
169- }
170189 return prev ($ this ->vars );
171190 }
172191
@@ -177,9 +196,6 @@ public function prev(): string
177196 */
178197 public function next (): string
179198 {
180- if ($ this ->vars === null ) {
181- $ this ->reset ();
182- }
183199 return next ($ this ->vars );
184200 }
185201
0 commit comments