Passed
Push — i18n ( a159ba )
by Arnaud
04:36
created

Site::offsetGet()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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