Passed
Pull Request — master (#5)
by
unknown
02:54
created

AbstractValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
namespace LesserPhp\Compiler\Value;
3
4
/**
5
 * lesserphp
6
 * https://www.maswaba.de/lesserphp
7
 *
8
 * LESS CSS compiler, adapted from http://lesscss.org
9
 *
10
 * Copyright 2013, Leaf Corcoran <[email protected]>
11
 * Copyright 2016, Marcus Schwarz <[email protected]>
12
 * Copyright 2017, Stefan Pöhner <[email protected]>
13
 * Licensed under MIT or GPLv3, see LICENSE
14
 *
15
 * @package LesserPhp
16
 */
17
use LesserPhp\Compiler;
18
use LesserPhp\Library\Coerce;
19
20
abstract class AbstractValue
21
{
22
    /** @var Compiler */
23
    protected $compiler;
24
    /** @var Coerce */
25
    protected $coerce;
26
27
    protected $options = [
28
        'numberPrecision' => null,
29
        'compressColors'  => false,
30
    ];
31
32
    /**
33
     * AbstractValue constructor.
34
     *
35
     * @param Compiler $compiler
36
     * @param Coerce   $coerce
37
     * @param array    $options
38
     */
39 52
    public function __construct(Compiler $compiler, Coerce $coerce, array $options = [])
40
    {
41 52
        $this->compiler = $compiler;
42 52
        $this->coerce   = $coerce;
43 52
        $this->options  = array_replace($this->options, $options);
44 52
    }
45
46
    /**
47
     * @param Compiler $compiler
48
     * @param Coerce   $coerce
49
     * @param array    $options
50
     * @param array    $value
51
     *
52
     * @return self
53
     */
54 53
    public static function factory(Compiler $compiler, Coerce $coerce, array $options, array $value)
55
    {
56 53
        $nameParts      = explode('_', $value[0]);
57 53
        $camelCase      = array_reduce($nameParts, function ($carry, $item) {
58 53
            return $carry.ucfirst($item);
59 53
        }, '');
60 53
        $valueClassName = 'LesserPhp\Compiler\Value\\'.$camelCase.'Value';
61
62 53
        if (class_exists($valueClassName)) {
63 52
            $valueClass = new $valueClassName($compiler, $coerce, $options);
64 52
            if ($valueClass instanceof self) {
65 52
                $valueClass->initializeFromOldFormat($value);
0 ignored issues
show
Deprecated Code introduced by
The method LesserPhp\Compiler\Value...itializeFromOldFormat() has been deprecated.

This method has been deprecated.

Loading history...
66
67 52
                return $valueClass;
68
            }
69
        }
70
71 1
        throw new \UnexpectedValueException('unknown value type: '.$value[0]);
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    abstract public function getCompiled();
78
79
    /**
80
     * Initialize value from old array format.
81
     *
82
     * @param array $value
83
     *
84
     * @return void
85
     * @deprecated
86
     */
87
    abstract public function initializeFromOldFormat(array $value);
88
}