Passed
Push — site ( 1b9975 )
by Arnaud
04:55
created

Site::getAllPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Renderer;
12
13
use Cecil\Builder;
14
15
/**
16
 * Class Site.
17
 */
18
class Site implements \ArrayAccess
19
{
20
    /** @var Builder Builder object. */
21
    protected $builder;
22
    /** @var \Cecil\Config */
23
    protected $config;
24
    /** @var string Current language. */
25
    protected $language;
26
27
    /**
28
     * @param Builder $builder
29
     * @param string  $language
30
     */
31
    public function __construct(Builder $builder, string $language)
32
    {
33
        $this->builder = $builder;
34
        $this->config = $this->builder->getConfig();
35
        $this->language = $language;
36
    }
37
38
    /**
39
     * Implement ArrayAccess.
40
     *
41
     * @param mixed $offset
42
     *
43
     * @return bool
44
     */
45
    public function offsetExists($offset)
46
    {
47
        // special cases
48
        switch ($offset) {
49
            case 'home':
50
            case 'menus':
51
                return true;
52
        }
53
54
        return $this->config->has($offset);
55
    }
56
57
    /**
58
     * Implements ArrayAccess.
59
     *
60
     * @param mixed $offset
61
     *
62
     * @return mixed|null
63
     */
64
    public function offsetGet($offset)
65
    {
66
        // special cases
67
        switch ($offset) {
68
            case 'menus':
69
                return $this->builder->getMenus($this->language);
70
            case 'taxonomies':
71
                return $this->builder->getTaxonomies();
72
            case 'language':
73
                return new Language($this->config, $this->language);
74
            case 'data':
75
                return $this->builder->getData();
76
            case 'static':
77
                return $this->builder->getStatic();
78
            case 'home':
79
                return $this->language !== $this->config->getLanguageDefault() ? sprintf('index.%s', $this->language) : 'index';
80
        }
81
82
        return $this->config->get($offset, $this->language);
83
    }
84
85
    /**
86
     * Implements ArrayAccess.
87
     *
88
     * @param mixed $offset
89
     * @param mixed $value
90
     */
91
    public function offsetSet($offset, $value)
92
    {
93
    }
94
95
    /**
96
     * Implements ArrayAccess.
97
     *
98
     * @param mixed $offset
99
     */
100
    public function offsetUnset($offset)
101
    {
102
    }
103
104
    /**
105
     * Returns all pages, in the current language.
106
     *
107
     * @return \Cecil\Collection\Page\Collection
108
     */
109
    public function getPages(): \Cecil\Collection\Page\Collection
110
    {
111
        return $this->builder->getPages()->filter(function ($page) {
112
            // We should fix case of virtual pages without language
113
            if ($page->getVariable('language') === null && $this->language === $this->config->getLanguageDefault()) {
114
                return true;
115
            }
116
117
            return $page->getVariable('language') == $this->language;
118
        });
119
    }
120
121
    /**
122
     * Returns all pages, regardless of their translation.
123
     *
124
     * @return \Cecil\Collection\Page\Collection
125
     */
126
    public function getAllPages(): \Cecil\Collection\Page\Collection
127
    {
128
        return $this->builder->getPages();
129
    }
130
131
    /**
132
     * Alias of getAllPages().
133
     */
134
    public function getPagesIntl(): \Cecil\Collection\Page\Collection
135
    {
136
        return $this->getAllPages();
137
    }
138
139
    /**
140
     * Return current time.
141
     *
142
     * @return int
143
     */
144
    public function getTime(): int
145
    {
146
        return time();
147
    }
148
}
149