|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Nodes; |
| 4 | + |
| 5 | +use Kirby\Cms\HasSiblings; |
| 6 | +use Kirby\Uuid\Uuid; |
| 7 | +use Kirby\Toolkit\Str; |
| 8 | + |
| 9 | +class Node |
| 10 | +{ |
| 11 | + use HasSiblings; |
| 12 | + |
| 13 | + protected array $nodes = []; |
| 14 | + |
| 15 | + public function __construct( |
| 16 | + protected string|int $id, |
| 17 | + protected Nodes $siblings, |
| 18 | + protected string|null $uuid = null, |
| 19 | + protected array $data = [], |
| 20 | + protected Node|null $parent = null, |
| 21 | + ) { |
| 22 | + $this->uuid ??= Uuid::generate(); |
| 23 | + } |
| 24 | + |
| 25 | + public function data(): array |
| 26 | + { |
| 27 | + return $this->data; |
| 28 | + } |
| 29 | + |
| 30 | + public function find(string $id): Nodes|Node|null |
| 31 | + { |
| 32 | + if (str_contains($id, '/') === true) { |
| 33 | + $ids = Str::split($id, '/'); |
| 34 | + $nodes = $this->nodes($ids[0]); |
| 35 | + |
| 36 | + return $nodes->find(implode('/', array_slice($ids, 1))); |
| 37 | + } |
| 38 | + |
| 39 | + return $this->nodes($id); |
| 40 | + } |
| 41 | + |
| 42 | + public static function from( |
| 43 | + array $data, |
| 44 | + string $id, |
| 45 | + Node|null $parent = null, |
| 46 | + Nodes|null $siblings = null, |
| 47 | + string|null $uuid = null, |
| 48 | + ): static { |
| 49 | + return new static( |
| 50 | + data: $data, |
| 51 | + id: $id, |
| 52 | + parent: $parent, |
| 53 | + siblings: $siblings ?? new Nodes(), |
| 54 | + uuid: $uuid ?? $data['uuid'] ?? $data['id'] ?? null, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function id(): string |
| 59 | + { |
| 60 | + return $this->id; |
| 61 | + } |
| 62 | + |
| 63 | + public function nodes(string $id, string $format = 'auto'): Nodes |
| 64 | + { |
| 65 | + return $this->nodes[$id] ??= Nodes::from( |
| 66 | + data: $this->data[$id] ?? $this->data['content'][$id] ?? null, |
| 67 | + format: $format, |
| 68 | + id: $id, |
| 69 | + parent: $this, |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function parent(): Node|null |
| 74 | + { |
| 75 | + return $this->parent; |
| 76 | + } |
| 77 | + |
| 78 | + public function path(): string |
| 79 | + { |
| 80 | + return $this->siblings->path() . '/' . $this->id(); |
| 81 | + } |
| 82 | + |
| 83 | + protected function siblingsCollection(): Nodes |
| 84 | + { |
| 85 | + return $this->siblings; |
| 86 | + } |
| 87 | + |
| 88 | + public function toArray(): array |
| 89 | + { |
| 90 | + $nodes = []; |
| 91 | + |
| 92 | + foreach ($this->nodes as $id => $node) { |
| 93 | + $nodes[$id] = $node->toArray(); |
| 94 | + } |
| 95 | + |
| 96 | + return [ |
| 97 | + ...$this->data, |
| 98 | + ...$nodes, |
| 99 | + 'uuid' => $this->uuid() |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + public function update(array $data): static |
| 104 | + { |
| 105 | + $this->data = [ |
| 106 | + ...$this->data, |
| 107 | + ...$data, |
| 108 | + ]; |
| 109 | + |
| 110 | + return $this; |
| 111 | + } |
| 112 | + |
| 113 | + public function uuid(): string |
| 114 | + { |
| 115 | + return $this->uuid; |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments