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