Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.65% |
22 / 23 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Prepared | |
95.65% |
22 / 23 |
|
88.89% |
8 / 9 |
12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| uid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| published | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hidden | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| parent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| path | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| fieldMeta | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Node; |
| 6 | |
| 7 | use Cosray\Exception\NoSuchField; |
| 8 | use ValueError; |
| 9 | |
| 10 | final class Prepared |
| 11 | { |
| 12 | /** @param array<string, mixed> $data */ |
| 13 | public function __construct( |
| 14 | public readonly object $node, |
| 15 | private array $data, |
| 16 | ) {} |
| 17 | |
| 18 | public function uid(string $uid): self |
| 19 | { |
| 20 | $this->data['uid'] = $uid; |
| 21 | |
| 22 | return $this; |
| 23 | } |
| 24 | |
| 25 | /** The node's own handle, as for `$cms->render('footer')`. */ |
| 26 | public function handle(string $handle): self |
| 27 | { |
| 28 | $this->data['handle'] = $handle; |
| 29 | |
| 30 | return $this; |
| 31 | } |
| 32 | |
| 33 | public function published(bool $published = true): self |
| 34 | { |
| 35 | $this->data['published'] = $published; |
| 36 | |
| 37 | return $this; |
| 38 | } |
| 39 | |
| 40 | public function hidden(bool $hidden = true): self |
| 41 | { |
| 42 | $this->data['hidden'] = $hidden; |
| 43 | |
| 44 | return $this; |
| 45 | } |
| 46 | |
| 47 | public function parent(?string $uid): self |
| 48 | { |
| 49 | $this->data['parent'] = $uid; |
| 50 | |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sets an explicit URL path for one locale instead of the one |
| 56 | * generated from the route, e.g. to preserve a legacy URL. |
| 57 | */ |
| 58 | public function path(string $locale, string $path): self |
| 59 | { |
| 60 | $path = trim($path); |
| 61 | |
| 62 | if ($path === '') { |
| 63 | throw new ValueError('A node path must not be empty'); |
| 64 | } |
| 65 | |
| 66 | if (!str_starts_with($path, '/')) { |
| 67 | $path = '/' . $path; |
| 68 | } |
| 69 | |
| 70 | $this->data['paths'][$locale] = $path; |
| 71 | |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | public function fieldMeta(string $field, string $key, mixed $value): self |
| 76 | { |
| 77 | if (!isset($this->data['content'][$field])) { |
| 78 | throw new NoSuchField("Node does not have a field named '{$field}'"); |
| 79 | } |
| 80 | |
| 81 | $this->data['content'][$field]['meta'][$key] = $value; |
| 82 | |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** @return array<string, mixed> */ |
| 87 | public function data(): array |
| 88 | { |
| 89 | return $this->data; |
| 90 | } |
| 91 | } |