Passed
Branch code (e65214)
by Arnaud
03:34
created

Site::getAllPages()   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 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
23
    /** @var \Cecil\Config */
24
    protected $config;
25
26
    /** @var string Current language. */
27
    protected $language;
28
29 1
    public function __construct(Builder $builder, string $language)
30
    {
31 1
        $this->builder = $builder;
32 1
        $this->config = $this->builder->getConfig();
33 1
        $this->language = $language;
34 1
    }
35
36
    /**
37
     * Implement ArrayAccess.
38
     *
39
     * @param mixed $offset
40
     *
41
     * @return bool
42
     */
43 1
    public function offsetExists($offset)
44
    {
45
        // special cases
46
        switch ($offset) {
47 1
            case 'home':
48 1
            case 'menus':
49 1
                return true;
50
        }
51
52 1
        return $this->config->has($offset);
53
    }
54
55
    /**
56
     * Implements ArrayAccess.
57
     *
58
     * @param mixed $offset
59
     *
60
     * @return mixed|null
61
     */
62 1
    public function offsetGet($offset)
63
    {
64
        // special cases
65
        switch ($offset) {
66 1
            case 'menus':
67 1
                return $this->builder->getMenus($this->language);
68 1
            case 'taxonomies':
69 1
                return $this->builder->getTaxonomies();
70 1
            case 'language':
71 1
                return new Language($this->config, $this->language);
72 1
            case 'data':
73 1
                return $this->builder->getData();
74 1
            case 'static':
75 1
                return $this->builder->getStatic();
76 1
            case 'home':
77
                return $this->language !== $this->config->getLanguageDefault() ? sprintf('index.%s', $this->language) : 'index';
78
        }
79
80 1
        return $this->config->get($offset, $this->language);
81
    }
82
83
    /**
84
     * Implements ArrayAccess.
85
     *
86
     * @param mixed $offset
87
     * @param mixed $value
88
     */
89
    public function offsetSet($offset, $value)
90
    {
91
    }
92
93
    /**
94
     * Implements ArrayAccess.
95
     *
96
     * @param mixed $offset
97
     */
98
    public function offsetUnset($offset)
99
    {
100
    }
101
102
    /**
103
     * Returns all pages, in the current language.
104
     */
105 1
    public function getPages(): \Cecil\Collection\Page\Collection
106
    {
107
        return $this->builder->getPages()->filter(function ($page) {
108
            // We should fix case of virtual pages without language
109 1
            if ($page->getVariable('language') === null && $this->language === $this->config->getLanguageDefault()) {
110 1
                return true;
111
            }
112
113 1
            return $page->getVariable('language') == $this->language;
114 1
        });
115
    }
116
117
    /**
118
     * Returns all pages, regardless of their translation.
119
     */
120
    public function getAllPages(): \Cecil\Collection\Page\Collection
121
    {
122
        return $this->builder->getPages();
123
    }
124
125
    /**
126
     * Alias of getAllPages().
127
     */
128
    public function getPagesIntl(): \Cecil\Collection\Page\Collection
129
    {
130
        return $this->getAllPages();
131
    }
132
133
    /**
134
     * Return current time.
135
     */
136 1
    public function getTime(): int
137
    {
138 1
        return time();
139
    }
140
}
141