Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Cms
96.88% covered (success)
96.88%
31 / 32
85.71% covered (warning)
85.71%
6 / 7
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 nodes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 node
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 menu
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 nodeFactory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray;
6
7use Cosray\Exception\RuntimeException;
8use Cosray\Finder\Menu;
9use Cosray\Finder\Node;
10use Cosray\Finder\Nodes;
11use Cosray\Finder\Render;
12use Cosray\Node\Factory;
13use Cosray\Node\Types;
14
15/**
16 * @property-read Nodes $nodes
17 * @property-read Node $node
18 */
19class 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}