Completed
Push — master ( b38731...5d7e29 )
by Marcus
02:03
created

Block::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace LesserPhp;
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
18
class Block
19
{
20
    /**
21
     * @var Block|null
22
     */
23
    public $parent;
24
25
    /**
26
     * @var string|null
27
     */
28
    public $type;
29
30
    /**
31
     * @var int
32
     */
33
    public $id;
34
35
    /**
36
     * @var bool
37
     */
38
    public $isVararg = false;
39
40
    /**
41
     * @var array|null
42
     */
43
    public $tags;
44
45
    /**
46
     * @var array
47
     */
48
    public $props = [];
49
50
    /**
51
     * @var Block[]
52
     */
53
    public $children = [];
54
55
    /**
56
     * Position of this block.
57
     *
58
     * @var int
59
     */
60
    public $count;
61
62
    /**
63
     * Add a reference to the parser so
64
     * we can access the parser to throw errors
65
     * or retrieve the sourceName of this block.
66
     *
67
     * @var Parser
68
     */
69
    public $parser;
70
71
    /**
72
     * Block constructor.
73
     *
74
     * @param Parser     $parser
75
     * @param int        $id
76
     * @param int        $count
77
     * @param array|null $tags
78
     * @param Block|null $parent
79
     */
80
    public function __construct(Parser $parser, $id, $count, array $tags = null, Block $parent = null)
81
    {
82
        $this->parser = $parser;
83
        $this->id     = $id;
84
        $this->count  = $count;
85
        $this->type   = $this->getType();
86
        $this->tags   = $tags;
87
        $this->parent = $parent;
88
    }
89
90
    /**
91
     * @return string|null
92
     */
93
    public function getType()
94
    {
95
        return null;
96
    }
97
98
    /**
99
     * @param Parser      $parser
100
     * @param int         $id
101
     * @param int         $count
102
     * @param string|null $type
103
     * @param array|null  $tags
104
     * @param Block|null  $parent
105
     *
106
     * @return Block
107
     */
108
    public static function factory(Parser $parser, $id, $count, $type = null, array $tags = null, Block $parent = null)
109
    {
110
        if ($type === null) {
111
            $className = self::class;
112
        } else {
113
            $className = __NAMESPACE__ . '\Block\\' . ucfirst($type);
114
        }
115
116
        if (!class_exists($className)) {
117
            throw new \UnexpectedValueException("Unknown block type: $type");
118
        }
119
120
        $block = new $className($parser, $id, $count, $tags, $parent);
121
122
        return $block;
123
    }
124
}
125