Completed
Push — master ( beccd2...66b328 )
by Vitaly
02:25
created

LessNode::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 15.04.16 at 12:47
5
 */
6
namespace samsonframework\html2less;
7
8
/**
9
 * Internal HTML DOM node tree.
10
 *
11
 * @author    Vitaly Egorov <[email protected]>
12
 * @copyright 2016 SamsonOS
13
 */
14
class LessNode
15
{
16
    /** @var string Selector for node */
17
    public $selector;
18
    /** @var $this Pointer to parent node */
19
    public $parent;
20
    /** @var $this [] Collection of nested nodes */
21
    public $children = array();
22
23
    /**
24
     * Create LESS Node
25
     *
26
     * @param string $selector Forced LESS node selector
27
     * @param        $this     $parent   Pointer to parent node
28
     */
29
    public function __construct($selector, self &$parent = null)
30
    {
31
        // Pointer to parent LESS Node
32
        $this->parent = &$parent;
33
        $this->selector = $selector;
34
35
        // Add this node to parent
36
        if (null !== $parent) {
37
            // Store child by selector to avoid duplication
38
            $parent->children[$this->selector] = $this;
39
        }
40
    }
41
42
    /**
43
     * Get child node by selector.
44
     *
45
     * @param string $selector Node selector
46
     *
47
     * @return null|$this Node or null if not found
48
     */
49
    public function getChild($selector)
50
    {
51
        if (array_key_exists($selector, $this->children)) {
52
            return $this->children[$selector];
53
        }
54
55
        return null;
56
    }
57
58
    /**
59
     * @return string Object string representation
60
     */
61
    public function __toString()
62
    {
63
        return $this->selector;
64
    }
65
}
66