Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

VariableTrait::setVariable()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.1795
c 0
b 0
f 0
cc 8
nc 12
nop 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Page;
10
11
use Cecil\Exception\Exception;
12
13
/**
14
 * Helper to set and get page variables.
15
 *
16
 * @property array properties
17
 */
18
trait VariableTrait
19
{
20
    abstract public function offsetExists($offset);
21
22
    abstract public function offsetGet($offset);
23
24
    abstract public function offsetSet($offset, $value);
25
26
    abstract public function offsetUnset($offset);
27
28
    /**
29
     * Set variables.
30
     *
31
     * @param array $variables
32
     *
33
     * @throws Exception
34
     *
35
     * @return $this
36
     */
37
    public function setVariables($variables)
38
    {
39
        if (!is_array($variables)) {
40
            return $this;
41
        }
42
        foreach ($variables as $key => $value) {
43
            $this->setVariable($key, $value);
44
        }
45
46
        return $this;
47
    }
48
49
    /**
50
     * Get variables.
51
     *
52
     * @return array
53
     */
54
    public function getVariables()
55
    {
56
        return $this->properties;
57
    }
58
59
    /**
60
     * Set a variable.
61
     *
62
     * @param $name
63
     * @param $value
64
     *
65
     * @throws Exception
66
     *
67
     * @return $this
68
     */
69
    public function setVariable($name, $value)
70
    {
71
        switch ($name) {
72
            case 'date':
73
                try {
74
                    if ($value instanceof \DateTime) {
75
                        $this->offsetSet('date', $value);
76
                    } else {
77
                        if (is_numeric($value)) {
78
                            $this->offsetSet('date', (new \DateTime())->setTimestamp($value));
79
                        } else {
80
                            if (is_string($value)) {
81
                                $this->offsetSet('date', new \DateTime($value));
82
                            }
83
                        }
84
                    }
85
                } catch (Exception $e) {
86
                    throw new Exception(sprintf("Expected date string in page ID: '%s'", $this->getId()));
87
                }
88
                break;
89
            case 'draft':
90
                if ($value === true) {
91
                    $this->offsetSet('published', false);
92
                }
93
                break;
94
            default:
95
                $this->offsetSet($name, $value);
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Is variable exist?
103
     *
104
     * @param $name
105
     *
106
     * @return bool
107
     */
108
    public function hasVariable($name)
109
    {
110
        return $this->offsetExists($name);
111
    }
112
113
    /**
114
     * Get a variable.
115
     *
116
     * @param string $name
117
     *
118
     * @return mixed|null
119
     */
120
    public function getVariable($name)
121
    {
122
        if ($this->offsetExists($name)) {
123
            return $this->offsetGet($name);
124
        }
125
    }
126
127
    /**
128
     * Unset a variable.
129
     *
130
     * @param $name
131
     *
132
     * @return $this
133
     */
134
    public function unVariable($name)
135
    {
136
        if ($this->offsetExists($name)) {
137
            $this->offsetUnset($name);
138
        }
139
140
        return $this;
141
    }
142
}
143