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