Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
18 / 21 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Draft | |
85.71% |
18 / 21 |
|
75.00% |
6 / 8 |
11.35 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| uid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| published | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hidden | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 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 Draft |
| 11 | { |
| 12 | public function __construct( |
| 13 | public readonly object $node, |
| 14 | private array $data, |
| 15 | ) {} |
| 16 | |
| 17 | public function uid(string $uid): self |
| 18 | { |
| 19 | $this->data['uid'] = $uid; |
| 20 | |
| 21 | return $this; |
| 22 | } |
| 23 | |
| 24 | public function published(bool $published = true): self |
| 25 | { |
| 26 | $this->data['published'] = $published; |
| 27 | |
| 28 | return $this; |
| 29 | } |
| 30 | |
| 31 | public function hidden(bool $hidden = true): self |
| 32 | { |
| 33 | $this->data['hidden'] = $hidden; |
| 34 | |
| 35 | return $this; |
| 36 | } |
| 37 | |
| 38 | public function parent(?string $uid): self |
| 39 | { |
| 40 | $this->data['parent'] = $uid; |
| 41 | |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Sets an explicit URL path for one locale instead of the one |
| 47 | * generated from the route, e.g. to preserve a legacy URL. |
| 48 | */ |
| 49 | public function path(string $locale, string $path): self |
| 50 | { |
| 51 | $path = trim($path); |
| 52 | |
| 53 | if ($path === '') { |
| 54 | throw new ValueError('A node path must not be empty'); |
| 55 | } |
| 56 | |
| 57 | if (!str_starts_with($path, '/')) { |
| 58 | $path = '/' . $path; |
| 59 | } |
| 60 | |
| 61 | $this->data['paths'][$locale] = $path; |
| 62 | |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | public function fieldMeta(string $field, string $key, mixed $value): self |
| 67 | { |
| 68 | if (!isset($this->data['content'][$field])) { |
| 69 | throw new NoSuchField("Draft does not have a field named '{$field}'"); |
| 70 | } |
| 71 | |
| 72 | $this->data['content'][$field]['meta'][$key] = $value; |
| 73 | |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | public function data(): array |
| 78 | { |
| 79 | return $this->data; |
| 80 | } |
| 81 | } |