Skip to content

Commit eac0cfa

Browse files
committed
feat: add path class for part traverse
1 parent df26efa commit eac0cfa

2 files changed

Lines changed: 148 additions & 28 deletions

File tree

src/Interfaces/PathInterface.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,112 @@
22

33
namespace MaplePHP\Http\Interfaces;
44

5+
56
use Psr\Http\Message\UriInterface;
67

78
interface PathInterface
89
{
10+
11+
/**
12+
* Get PSR URI instance with whitelisted path and
13+
* cleared, query and fragments
14+
*
15+
* @return UriInterface
16+
*/
17+
public function uri(): UriInterface;
18+
19+
/**
20+
* Get current full URL with whitelisted path
21+
*
22+
* @return string
23+
*/
24+
public function url(): string;
25+
26+
/**
27+
* With URI path type key
28+
*
29+
* @param null|string|array $type
30+
* @return static
31+
*/
32+
public function withType(null|string|array $type): self;
33+
34+
/**
35+
* Same as withType except that you Need to select a part
36+
*
37+
* @param string|array $type
38+
* @return static
39+
*/
40+
public function select(string|array $type): self;
41+
42+
/**
43+
* Same as withType except it will only reset
44+
*
45+
* @return static
46+
*/
47+
public function reset(): self;
48+
49+
/**
50+
* Append to URI path
51+
*
52+
* @param array|string $arr
53+
* @return static
54+
*/
55+
public function append(array|string $arr): self;
56+
57+
/**
58+
* Prepend to URI path
59+
*
60+
* @param array|string $arr
61+
* @return static
62+
*/
63+
public function prepend(array|string $arr): self;
64+
65+
/**
66+
* Get vars/path as array
67+
*
68+
* @return array
69+
*/
70+
public function vars(): array;
71+
72+
/**
73+
* Get vars/path as array
74+
*
75+
* @return array
76+
*/
77+
public function parts(): array;
78+
79+
/**
80+
* Get expected slug from path
81+
*
82+
* @return array
83+
*/
84+
public function get(): array;
85+
86+
/**
87+
* Get last path item
88+
*
89+
* @return string
90+
*/
91+
public function last(): string;
92+
93+
/**
94+
* Get first path item
95+
*
96+
* @return string
97+
*/
98+
public function first(): string;
99+
100+
/**
101+
* Get travers to prev path item
102+
*
103+
* @return string
104+
*/
105+
public function prev(): string;
106+
107+
/**
108+
* Get travers to next path item
109+
*
110+
* @return string
111+
*/
112+
public function next(): string;
9113
}

src/Path.php

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,47 @@
55
namespace MaplePHP\Http;
66

77
use MaplePHP\Http\Interfaces\PathInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ServerRequestInterface;
10+
use Psr\Http\Message\UriInterface;
811

912
class 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

Comments
 (0)