Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
91.67% |
11 / 12 |
CRAP | |
0.00% |
0 / 1 |
| NodeUrls | |
95.24% |
20 / 21 |
|
91.67% |
11 / 12 |
14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| edit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| discard | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| paths | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| blocks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| createPaths | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createBlocks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| path | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| origin | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| url | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Panel; |
| 6 | |
| 7 | final class NodeUrls |
| 8 | { |
| 9 | /** @param array<string, string> $extra query parameters every link carries along */ |
| 10 | public function __construct( |
| 11 | private readonly string $panelPath, |
| 12 | private readonly string $from = '', |
| 13 | private readonly ?CollectionQuery $listing = null, |
| 14 | private readonly array $extra = [], |
| 15 | ) {} |
| 16 | |
| 17 | public function edit(string $uid): string |
| 18 | { |
| 19 | return $this->url($this->path($uid), $this->origin()); |
| 20 | } |
| 21 | |
| 22 | public function delete(string $uid): string |
| 23 | { |
| 24 | return $this->url($this->path($uid) . '/delete', $this->origin()); |
| 25 | } |
| 26 | |
| 27 | public function discard(string $uid): string |
| 28 | { |
| 29 | return $this->url($this->path($uid) . '/discard', $this->origin()); |
| 30 | } |
| 31 | |
| 32 | public function paths(string $uid): string |
| 33 | { |
| 34 | return $this->path($uid) . '/paths'; |
| 35 | } |
| 36 | |
| 37 | public function blocks(string $uid): string |
| 38 | { |
| 39 | return $this->path($uid) . '/blocks'; |
| 40 | } |
| 41 | |
| 42 | public function create(string $type, ?string $parent = null): string |
| 43 | { |
| 44 | return $this->url($this->path('create') . '/' . rawurlencode($type), [ |
| 45 | ...$this->origin(), |
| 46 | 'parent' => $parent, |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | public function createPaths(string $type, ?string $parent = null): string |
| 51 | { |
| 52 | return $this->url($this->path('create') . '/' . rawurlencode($type) . '/paths', ['parent' => $parent]); |
| 53 | } |
| 54 | |
| 55 | public function createBlocks(string $type, ?string $parent = null): string |
| 56 | { |
| 57 | return $this->url($this->path('create') . '/' . rawurlencode($type) . '/blocks', ['parent' => $parent]); |
| 58 | } |
| 59 | |
| 60 | private function path(string $uid): string |
| 61 | { |
| 62 | return $this->panelPath . '/node/' . rawurlencode($uid); |
| 63 | } |
| 64 | |
| 65 | /** @return array<string, mixed> */ |
| 66 | private function origin(): array |
| 67 | { |
| 68 | return [ |
| 69 | 'from' => $this->from, |
| 70 | 'list' => $this->listing?->editorParams(), |
| 71 | ...$this->extra, |
| 72 | ]; |
| 73 | } |
| 74 | |
| 75 | /** @param array<string, mixed> $params */ |
| 76 | private function url(string $path, array $params): string |
| 77 | { |
| 78 | $params = array_filter($params, static fn(mixed $value): bool => $value !== null && $value !== ''); |
| 79 | $query = http_build_query($params, '', '&', PHP_QUERY_RFC3986); |
| 80 | |
| 81 | return $query === '' ? $path : $path . '?' . $query; |
| 82 | } |
| 83 | } |