• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 15789667681

20 Jun 2025 11:43PM UTC coverage: 82.623%. Remained the same
15789667681

push

github

ArnaudLigny
doc: better API doc

3119 of 3775 relevant lines covered (82.62%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.23
/src/Renderer/Site.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
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;
1✔
49
        $this->config = $this->builder->getConfig();
1✔
50
        $this->language = $language;
1✔
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':
1✔
66
            case 'taxonomies':
1✔
67
            case 'home':
1✔
68
            case 'debug':
1✔
69
                return true;
1✔
70
        }
71

72
        return $this->config->has($offset);
1✔
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':
1✔
88
                return $this->getPages();
1✔
89
            case 'menus':
1✔
90
                return $this->builder->getMenus($this->language);
1✔
91
            case 'taxonomies':
1✔
92
                return $this->builder->getTaxonomies($this->language);
1✔
93
            case 'data':
1✔
94
                return $this->builder->getData($this->language);
1✔
95
            case 'static':
1✔
96
                return $this->builder->getStatic();
1✔
97
            case 'language':
1✔
98
                return new Language($this->config, $this->language);
1✔
99
            case 'home':
1✔
100
                return 'index';
1✔
101
            case 'debug':
1✔
102
                return $this->builder->isDebug();
×
103
        }
104

105
        return $this->config->get($offset, $this->language);
1✔
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;
1✔
141
        $language = $language ?? $this->language;
1✔
142

143
        if ($language !== null && $language != $this->config->getLanguageDefault()) {
1✔
144
            $pageId = "$language/$id";
1✔
145
        }
146

147
        if ($this->builder->getPages()->has($pageId) === false) {
1✔
148
            // if multilingual == false
149
            if ($this->builder->getPages()->has($id) && $this->builder->getPages()->get($id)->getVariable('multilingual') === false) {
1✔
150
                return $this->builder->getPages()->get($id);
×
151
            }
152

153
            return null;
1✔
154
        }
155

156
        return $this->builder->getPages()->get($pageId);
1✔
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) {
1✔
165
            // We should fix case of virtual pages without language
166
            if (
167
                $page->getVariable('language') === null
1✔
168
                && $this->language == $this->config->getLanguageDefault()
1✔
169
            ) {
170
                return true;
×
171
            }
172

173
            return $page->getVariable('language') == $this->language;
1✔
174
        });
1✔
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();
1✔
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();
1✔
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);
1✔
207
    }
208
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc