Completed
Push — master ( 642ad1...6a6cfd )
by Vitaly
02:16
created

Node::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 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
 * LESS tree node.
10
 *
11
 * @author    Vitaly Egorov <[email protected]>
12
 * @copyright 2016 SamsonOS
13
 */
14
class Node
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 1
    public function __construct($selector, self &$parent = null)
30
    {
31
        // Pointer to parent LESS Node
32 1
        $this->parent = &$parent;
33 1
        $this->selector = $selector;
34
35
        // Add this node to parent
36 1
        if (null !== $parent) {
37
            // Store child by selector to avoid duplication
38 1
            $parent->children[$this->selector] = $this;
39 1
        }
40 1
    }
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 1
    public function getChild($selector)
50
    {
51 1
        if (array_key_exists($selector, $this->children)) {
52 1
            return $this->children[$selector];
53
        }
54
55 1
        return null;
56
    }
57
58
    /**
59
     * @return string Object string representation
60
     */
61 1
    public function __toString()
62
    {
63 1
        return $this->selector;
64
    }
65
}
66