Passed
Push — feat/site-page ( 8479b7 )
by Arnaud
04:36
created

Site::getPage()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 2
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
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
    public function __construct(Builder $builder, string $language)
34
    {
35
        $this->builder = $builder;
36
        $this->config = $this->builder->getConfig();
37
        $this->language = $language;
38
    }
39
40
    /**
41
     * Implement ArrayAccess.
42
     *
43
     * @param mixed $offset
44
     *
45
     * @return bool
46
     */
47
    #[\ReturnTypeWillChange]
48
    public function offsetExists($offset)
49
    {
50
        // special cases
51
        switch ($offset) {
52
            case 'home':
53
            case 'menus':
54
                return true;
55
        }
56
57
        return $this->config->has($offset);
58
    }
59
60
    /**
61
     * Implements ArrayAccess.
62
     *
63
     * @param mixed $offset
64
     *
65
     * @return mixed|null
66
     */
67
    #[\ReturnTypeWillChange]
68
    public function offsetGet($offset)
69
    {
70
        // special cases
71
        switch ($offset) {
72
            case 'menus':
73
                return $this->builder->getMenus($this->language);
74
            case 'taxonomies':
75
                return $this->builder->getTaxonomies();
76
            case 'language':
77
                return new Language($this->config, $this->language);
78
            case 'data':
79
                return $this->builder->getData();
80
            case 'static':
81
                return $this->builder->getStatic();
82
            case 'home':
83
                return $this->language != $this->config->getLanguageDefault() ? \sprintf('index.%s', $this->language) : 'index';
84
        }
85
86
        return $this->config->get($offset, $this->language);
87
    }
88
89
    /**
90
     * Implements ArrayAccess.
91
     *
92
     * @param mixed $offset
93
     * @param mixed $value
94
     */
95
    #[\ReturnTypeWillChange]
96
    public function offsetSet($offset, $value)
97
    {
98
    }
99
100
    /**
101
     * Implements ArrayAccess.
102
     *
103
     * @param mixed $offset
104
     */
105
    #[\ReturnTypeWillChange]
106
    public function offsetUnset($offset)
107
    {
108
    }
109
110
    /**
111
     * Returns a page in the current language or the one provided.
112
     *
113
     * @throws \DomainException
114
     */
115
    public function getPage(string $id, string $language = null): ?CollectionPage
116
    {
117
        $pageId = $id;
118
        if ($language === null && $this->language != $this->config->getLanguageDefault()) {
119
            $pageId = \sprintf('%s.%s', $id, $this->language);
120
        }
121
        if ($language !== null && $language != $this->config->getLanguageDefault()) {
122
            $pageId = \sprintf('%s.%s', $id, $language);
123
        }
124
125
        return $this->builder->getPages()->get($pageId);
126
    }
127
128
    /**
129
     * Returns all pages, in the current language.
130
     */
131
    public function getPages(): \Cecil\Collection\Page\Collection
132
    {
133
        return $this->builder->getPages()->filter(function (CollectionPage $page) {
134
            // We should fix case of virtual pages without language
135
            if ($page->getLanguage() === null && $this->language == $this->config->getLanguageDefault()) {
0 ignored issues
show
introduced by
The condition $page->getLanguage() === null is always false.
Loading history...
136
                return true;
137
            }
138
139
            return $page->getLanguage() == $this->language;
140
        });
141
    }
142
143
    /**
144
     * Returns all pages, regardless of their translation.
145
     */
146
    public function getAllPages(): \Cecil\Collection\Page\Collection
147
    {
148
        return $this->builder->getPages();
149
    }
150
151
    /**
152
     * Alias of getAllPages().
153
     */
154
    public function getPagesIntl(): \Cecil\Collection\Page\Collection
155
    {
156
        return $this->getAllPages();
157
    }
158
159
    /**
160
     * Return current time.
161
     */
162
    public function getTime(): int
163
    {
164
        return time();
165
    }
166
}
167