Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.88% |
31 / 32 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Cms | |
96.88% |
31 / 32 |
|
85.71% |
6 / 7 |
13 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| __get | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| nodes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| node | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| menu | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| nodeFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray; |
| 6 | |
| 7 | use Cosray\Exception\RuntimeException; |
| 8 | use Cosray\Finder\Menu; |
| 9 | use Cosray\Finder\Node; |
| 10 | use Cosray\Finder\Nodes; |
| 11 | use Cosray\Finder\Render; |
| 12 | use Cosray\Node\Factory; |
| 13 | use Cosray\Node\Types; |
| 14 | |
| 15 | /** |
| 16 | * @property-read Nodes $nodes |
| 17 | * @property-read Node $node |
| 18 | */ |
| 19 | class Cms |
| 20 | { |
| 21 | private readonly Factory $nodeFactory; |
| 22 | |
| 23 | public function __construct( |
| 24 | private readonly Context $context, |
| 25 | private readonly Types $types, |
| 26 | ) { |
| 27 | $uid = $context->config->uid; |
| 28 | $this->nodeFactory = new Factory( |
| 29 | $context->container, |
| 30 | types: $this->types, |
| 31 | uid: new Uid($uid->alphabet, $uid->length), |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | public function __get($key): Nodes|Node|Menu |
| 36 | { |
| 37 | return match ($key) { |
| 38 | 'nodes' => new Nodes($this->context, $this, $this->nodeFactory, $this->types), |
| 39 | 'node' => new Node($this->context, $this, $this->nodeFactory, $this->types), |
| 40 | default => throw new RuntimeException('Property not supported'), |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | public function nodes( |
| 45 | string $query = '', |
| 46 | ): Nodes { |
| 47 | return new Nodes($this->context, $this, $this->nodeFactory, $this->types)->filter($query); |
| 48 | } |
| 49 | |
| 50 | public function node( |
| 51 | string $query, |
| 52 | array $types = [], |
| 53 | int $limit = 0, |
| 54 | string $order = '', |
| 55 | ): array { |
| 56 | $finder = new Nodes($this->context, $this, $this->nodeFactory, $this->types)->filter($query); |
| 57 | |
| 58 | if ($types !== []) { |
| 59 | $finder->types(...$types); |
| 60 | } |
| 61 | |
| 62 | if ($order !== '') { |
| 63 | $finder->order($order); |
| 64 | } |
| 65 | |
| 66 | if ($limit > 0) { |
| 67 | $finder->limit($limit); |
| 68 | } |
| 69 | |
| 70 | return iterator_to_array($finder); |
| 71 | } |
| 72 | |
| 73 | public function menu(string $menu): Menu |
| 74 | { |
| 75 | return new Menu($this->context, $menu); |
| 76 | } |
| 77 | |
| 78 | public function render( |
| 79 | string $id, |
| 80 | array $templateContext = [], |
| 81 | ?bool $deleted = false, |
| 82 | ?bool $published = true, |
| 83 | ): Render { |
| 84 | return new Render( |
| 85 | $this->context, |
| 86 | $this, |
| 87 | $this->nodeFactory, |
| 88 | $this->types, |
| 89 | $id, |
| 90 | $templateContext, |
| 91 | $deleted, |
| 92 | $published, |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | public function nodeFactory(): Factory |
| 97 | { |
| 98 | return $this->nodeFactory; |
| 99 | } |
| 100 | } |