Completed
Push — master ( 5d7e29...cc6f2d )
by Marcus
02:36
created

Property::factoryFromOldFormat()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 32
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 5
nop 2
1
<?php
2
3
namespace LesserPhp;
4
5
/**
6
 * lesserphp
7
 * https://www.maswaba.de/lesserphp
8
 *
9
 * LESS CSS compiler, adapted from http://lesscss.org
10
 *
11
 * Copyright 2013, Leaf Corcoran <[email protected]>
12
 * Copyright 2016, Marcus Schwarz <[email protected]>
13
 * Copyright 2017, Stefan Pöhner <[email protected]>
14
 * Licensed under MIT or GPLv3, see LICENSE
15
 *
16
 * @author  Stefan Pöhner <[email protected]>
17
 *
18
 * @package LesserPhp
19
 */
20
21
abstract class Property implements \ArrayAccess
22
{
23
    /**
24
     * @var Parser
25
     */
26
    protected $parser;
27
28
    /**
29
     * @var int|null
30
     */
31
    protected $pos;
32
33
    /**
34
     * @var mixed
35
     */
36
    protected $value1;
37
38
    /**
39
     * @var mixed
40
     */
41
    protected $value2;
42
43
    /**
44
     * @var mixed
45
     */
46
    protected $value3;
47
48
    /**
49
     * Property constructor.
50
     *
51
     * @param int|null $pos
52
     * @param mixed    $value1
53
     */
54
    public function __construct($pos, $value1)
55
    {
56
        $this->pos    = $pos;
57
        $this->value1 = $value1;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getType()
64
    {
65
        return strtolower(str_replace([__NAMESPACE__ . '\\', '\\', 'Property'], '', get_class($this)));
66
    }
67
68
    /**
69
     * @return int|null
70
     */
71
    public function getPos()
72
    {
73
        return $this->pos;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function hasPos()
80
    {
81
        return ($this->pos !== null);
82
    }
83
84
    /**
85
     * @return mixed
86
     */
87
    protected function getValue1()
88
    {
89
        return $this->value1;
90
    }
91
92
    /**
93
     * @param mixed $value
94
     */
95
    protected function setValue1($value)
96
    {
97
        $this->value1 = $value;
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    protected function getValue2()
104
    {
105
        return $this->value2;
106
    }
107
108
    /**
109
     * @param mixed $value
110
     */
111
    protected function setValue2($value)
112
    {
113
        $this->value2 = $value;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    protected function getValue3()
120
    {
121
        return $this->value3;
122
    }
123
124
    /**
125
     * @param mixed $value
126
     */
127
    protected function setValue3($value)
128
    {
129
        $this->value3 = $value;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function offsetExists($offset)
136
    {
137
        throw new \RuntimeException('Array access is deprecated! ');
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function offsetGet($offset)
144
    {
145
        throw new \RuntimeException('Array access is deprecated! ');
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function offsetSet($offset, $value)
152
    {
153
        throw new \RuntimeException('Array access is deprecated! ');
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function offsetUnset($offset)
160
    {
161
        throw new \RuntimeException('Array access is deprecated! ');
162
    }
163
164
    /**
165
     * @param string     $type
166
     * @param int|null   $pos
167
     * @param mixed      $value1
168
     * @param mixed|null $value2
169
     * @param mixed|null $value3
170
     *
171
     * @return self
172
     */
173
    public static function factory($type, $pos, $value1, $value2 = null, $value3 = null)
174
    {
175
        $type      = implode('', array_map('ucfirst', explode('_', $type)));
176
        $className = __NAMESPACE__ . '\Property\\' . ucfirst($type) . 'Property';
177
178
        if (!class_exists($className)) {
179
            throw new \UnexpectedValueException("Unknown property type: $type");
180
        }
181
182
        $property = new $className($pos, $value1);
183
        if (!$property instanceof self) {
184
            throw new \RuntimeException("$className must extend " . self::class);
185
        }
186
187
        $property->setValue2($value2);
188
        $property->setValue3($value3);
189
190
        return $property;
191
    }
192
193
    /**
194
     * @param array    $prop
195
     * @param int|null $pos
196
     *
197
     * @return Property
198
     */
199
    public static function factoryFromOldFormat(array $prop, $pos = null)
200
    {
201
        /*
202
         * A property array looks like this:
203
         * [-1] => optional position
204
         * [0]  => type
205
         * [1]  => value
206
         * [2]  => optional more information
207
         * [3]  => optional more information
208
         *
209
         * 0 and 1 are always present
210
         * only comments have the simplest form with only 1 value
211
         */
212
213
        if (!isset($prop[0], $prop[1])) {
214
            throw new \UnexpectedValueException('Too few property information given.');
215
        }
216
217
        $type   = $prop[0];
218
        $value1 = $prop[1];
219
        $value2 = null;
220
        $value3 = null;
221
222
        if (isset($prop[2])) {
223
            $value2 = $prop[2];
224
        }
225
        if (isset($prop[3])) {
226
            $value3 = $prop[3];
227
        }
228
229
        return self::factory($type, $pos, $value1, $value2, $value3);
230
    }
231
}
232