Completed
Pull Request — master (#5)
by
unknown
02:59
created

AbstractValue   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 69
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A factory() 0 19 3
getCompiled() 0 1 ?
initializeFromOldFormat() 0 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
}