Passed
Push — nested-sections ( 2d40b2...7dbf34 )
by Arnaud
12:03 queued 05:37
created

Site::getOutputProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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