Skip to content

Commit 920640c

Browse files
committed
feat: add path modifier
1 parent 0964bc4 commit 920640c

4 files changed

Lines changed: 223 additions & 489 deletions

File tree

src/Interfaces/PathInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace MaplePHP\Http\Interfaces;
4+
5+
use Psr\Http\Message\UriInterface;
6+
7+
interface PathInterface
8+
{
9+
}

src/Interfaces/UrlInterface.php

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/Path.php

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MaplePHP\Http;
6+
7+
use MaplePHP\Http\Interfaces\PathInterface;
8+
9+
class Path implements PathInterface
10+
{
11+
private $parts;
12+
private $vars;
13+
14+
public function __construct(array $parts)
15+
{
16+
$this->parts = $parts;
17+
$this->vars = $this->partsToVars($parts);
18+
}
19+
20+
/**
21+
* With URI path type key
22+
*
23+
* @param null|string|array $type
24+
* @return static
25+
*/
26+
public function withType(null|string|array $type): self
27+
{
28+
if (is_string($type)) {
29+
$type = [$type];
30+
}
31+
if ($type === null) {
32+
$type = [];
33+
}
34+
35+
$inst = clone $this;
36+
$parts = [];
37+
$vars = $this->partsToVars($inst->parts, $type, function ($key, $item) use (&$parts, $type) {
38+
$parts[$key] = $item;
39+
});
40+
$inst->parts = $parts;
41+
$inst->vars = $vars;
42+
return $inst;
43+
}
44+
45+
/**
46+
* Same as withType except that you Need to select a part
47+
* @param string|array $type
48+
* @return static
49+
*/
50+
public function select(string|array $type): self
51+
{
52+
return $this->withType($type);
53+
}
54+
55+
/**
56+
* Same as withType except it will only reset
57+
* @return static
58+
*/
59+
public function reset(): self
60+
{
61+
return $this->withType(null);
62+
}
63+
64+
/**
65+
* Append to URI path
66+
*
67+
* @param array|string $arr
68+
* @return static
69+
*/
70+
public function append(array|string $arr): self
71+
{
72+
$inst = clone $this;
73+
if (is_string($arr)) {
74+
$arr = [$arr];
75+
}
76+
77+
$inst->vars = array_merge($inst->vars, $arr);
78+
return $inst;
79+
}
80+
81+
/**
82+
* Prepend to URI path
83+
*
84+
* @param array|string $arr
85+
* @return static
86+
*/
87+
public function prepend(array|string $arr): self
88+
{
89+
$inst = clone $this;
90+
if (is_string($arr)) {
91+
$arr = [$arr];
92+
}
93+
94+
$inst->vars = array_merge($arr, $inst->vars);
95+
return $inst;
96+
}
97+
98+
/**
99+
* Get vars/path as array
100+
* @return array
101+
*/
102+
public function vars(): array
103+
{
104+
return $this->vars;
105+
}
106+
107+
/**
108+
* Get vars/path as array
109+
* @return array
110+
*/
111+
public function parts(): array
112+
{
113+
return $this->parts;
114+
}
115+
116+
/**
117+
* Get expected slug from path
118+
* @return string
119+
*/
120+
public function get(): string
121+
{
122+
return $this->last();
123+
}
124+
125+
/**
126+
* Get expected slug from path
127+
* @return string
128+
*/
129+
public function current(): string
130+
{
131+
return $this->last();
132+
}
133+
134+
/**
135+
* Get last path item
136+
*
137+
* @return string
138+
*/
139+
public function last(): string
140+
{
141+
if ($this->vars === null) {
142+
$this->vars = $this->getVars();
143+
}
144+
return end($this->vars);
145+
}
146+
147+
/**
148+
* Get first path item
149+
*
150+
* @return string
151+
*/
152+
public function first(): string
153+
{
154+
if ($this->vars === null) {
155+
$this->vars = $this->getVars();
156+
}
157+
return reset($this->vars);
158+
}
159+
160+
/**
161+
* Get travers to prev path item
162+
*
163+
* @return string
164+
*/
165+
public function prev(): string
166+
{
167+
if ($this->vars === null) {
168+
$this->end();
169+
}
170+
return prev($this->vars);
171+
}
172+
173+
/**
174+
* Get travers to next path item
175+
*
176+
* @return string
177+
*/
178+
public function next(): string
179+
{
180+
if ($this->vars === null) {
181+
$this->reset();
182+
}
183+
return next($this->vars);
184+
}
185+
186+
/**
187+
* Break parts down
188+
*
189+
* @param array $parts
190+
* @param array|null $type
191+
* @param callable|null $call
192+
* @return array
193+
*/
194+
protected function partsToVars(array $parts, ?array $type = null, ?callable $call = null): array
195+
{
196+
$vars = [];
197+
foreach ($parts as $key => $item) {
198+
199+
if ($type === null || in_array($key, $type)) {
200+
201+
if (is_array($item)) {
202+
$vars = array_merge($vars, $item);
203+
} else {
204+
$vars[] = $item;
205+
}
206+
if ($call !== null) {
207+
$call($key, $item);
208+
}
209+
210+
}
211+
}
212+
return $vars;
213+
}
214+
}

0 commit comments

Comments
 (0)