Site::getOutputProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Renderer;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page as CollectionPage;
18
19
/**
20
 * Site renderer class.
21
 *
22
 * This class implements the \ArrayAccess interface to allow access to site
23
 * properties using array syntax. It provides access to various site-related
24
 * properties such as pages, menus, taxonomies, and language settings.
25
 * It also provides methods to retrieve specific pages and collections of pages
26
 * based on the current language or all pages regardless of their translation.
27
 */
28
class Site implements \ArrayAccess
29
{
30
    /**
31
     * Builder object.
32
     * @var Builder
33
     */
34
    protected $builder;
35
    /**
36
     * Configuration object.
37
     * @var \Cecil\Config
38
     */
39
    protected $config;
40
    /**
41
     * Current language code.
42
     * @var string
43
     */
44
    protected $language;
45
46
    public function __construct(Builder $builder, string $language)
47
    {
48
        $this->builder = $builder;
49
        $this->config = $this->builder->getConfig();
50
        $this->language = $language;
51
    }
52
53
    /**
54
     * Implement ArrayAccess.
55
     *
56
     * @param mixed $offset
57
     *
58
     * @return bool
59
     */
60
    #[\ReturnTypeWillChange]
61
    public function offsetExists($offset): bool
62
    {
63
        // special cases
64
        switch ($offset) {
65
            case 'menus':
66
            case 'taxonomies':
67
            case 'home':
68
            case 'debug':
69
                return true;
70
        }
71
72
        return $this->config->has($offset);
73
    }
74
75
    /**
76
     * Implements \ArrayAccess.
77
     *
78
     * @param mixed $offset
79
     *
80
     * @return mixed|null
81
     */
82
    #[\ReturnTypeWillChange]
83
    public function offsetGet($offset)
84
    {
85
        // If it's a built-in variable: dot not fetch data from config raw
86
        switch ($offset) {
87
            case 'pages':
88
                return $this->getPages();
89
            case 'menus':
90
                return $this->builder->getMenus($this->language);
91
            case 'taxonomies':
92
                return $this->builder->getTaxonomies($this->language);
93
            case 'data':
94
                return $this->builder->getData($this->language);
95
            case 'static':
96
                return $this->builder->getStatic();
97
            case 'language':
98
                return new Language($this->config, $this->language);
99
            case 'home':
100
                return 'index';
101
            case 'debug':
102
                return $this->builder->isDebug();
103
        }
104
105
        return $this->config->get($offset, $this->language);
106
    }
107
108
    /**
109
     * Implements \ArrayAccess.
110
     *
111
     * @param mixed $offset
112
     * @param mixed $value
113
     *
114
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
115
     */
116
    #[\ReturnTypeWillChange]
117
    public function offsetSet($offset, $value): void
118
    {
119
    }
120
121
    /**
122
     * Implements \ArrayAccess.
123
     *
124
     * @param mixed $offset
125
     *
126
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
127
     */
128
    #[\ReturnTypeWillChange]
129
    public function offsetUnset($offset): void
130
    {
131
    }
132
133
    /**
134
     * Returns a page for the provided language or the current one provided.
135
     *
136
     * @throws \DomainException
137
     */
138
    public function getPage(string $id, ?string $language = null): ?CollectionPage
139
    {
140
        $pageId = $id;
141
        $language = $language ?? $this->language;
142
143
        if ($language !== null && $language != $this->config->getLanguageDefault()) {
144
            $pageId = "$language/$id";
145
        }
146
147
        if ($this->builder->getPages()->has($pageId) === false) {
148
            // if multilingual == false
149
            if ($this->builder->getPages()->has($id) && $this->builder->getPages()->get($id)->getVariable('multilingual') === false) {
150
                return $this->builder->getPages()->get($id);
151
            }
152
153
            return null;
154
        }
155
156
        return $this->builder->getPages()->get($pageId);
157
    }
158
159
    /**
160
     * Returns all pages, in the current language.
161
     */
162
    public function getPages(): \Cecil\Collection\Page\Collection
163
    {
164
        return $this->builder->getPages()->filter(function (CollectionPage $page) {
165
            // We should fix case of virtual pages without language
166
            if (
167
                $page->getVariable('language') === null
168
                && $this->language == $this->config->getLanguageDefault()
169
            ) {
170
                return true;
171
            }
172
173
            return $page->getVariable('language') == $this->language;
174
        });
175
    }
176
177
    /**
178
     * Returns all pages, regardless of their translation.
179
     */
180
    public function getAllPages(): \Cecil\Collection\Page\Collection
181
    {
182
        return $this->builder->getPages();
183
    }
184
185
    /**
186
     * Alias of getAllPages().
187
     */
188
    public function getPagesIntl(): \Cecil\Collection\Page\Collection
189
    {
190
        return $this->getAllPages();
191
    }
192
193
    /**
194
     * Returns current time.
195
     */
196
    public function getTime(): int
197
    {
198
        return time();
199
    }
200
201
    /**
202
     * Returns the property value(s) of an output format.
203
     */
204
    public function getOutputProperty(string $name, string $property): string|array|null
205
    {
206
        return $this->config->getOutputFormatProperty($name, $property);
207
    }
208
}
209