-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFragment.php
More file actions
172 lines (114 loc) · 2.93 KB
/
Copy pathFragment.php
File metadata and controls
172 lines (114 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Pronode;
class Fragment {
/**
* Fragment id in hash form: 066bce79
* This is stored in tag's pn_fragment attribute:
* <tag pn_fragment="066bce79" pn_origin="{{origin}}">{{someVar}}</tag>
*/
public string $id;
/**
* Fragment start position inside Template contents (char index)
*/
public int $pos;
/**
* Fragment end position inside Template contents (char index)
*/
public int $end;
/**
* HTML contents
*/
public string $contents = '';
/**
* Marker occurences found inside that fragment
*/
public array $markers = [];
/**
* Parent Template pointer
*/
public Template $template;
public function __construct(Template $template, int $pos) {
$this->template = $template;
$this->pos = $pos;
}
/**
* Generates an unique Fragment identifier basing on Fragment->contents
*
* @param string $contents Fragment contents
*/
public function getId() {
return static :: getIdStatic($this->contents);
}
/**
* Generates an unique Fragment identifier basing on $contents string.
*
* @param string $contents Fragment contents
*/
public static function getIdStatic(string $contents) {
if (!$contents) {
throw new \ErrorException("Fragment contents must not be empty in order to encode an id");
}
// Strip pn_fragment and pn_origin attributes:
$stripped = preg_replace('/ pn_fragment=\"(.*?)\" pn_origin=\"(.*?)\"/', '', $contents);
return substr(md5($stripped), 0, 8); // 8 should be enough to avoid collisions
}
/**
* Returns array of nested Fragments inside current fragment
*
* @return Fragment[]
*/
public function findNested() : array {
$nested = [];
foreach ($this->template->fragments as $fragment) {
if ($fragment->pos > $this->pos && $fragment->end < $this->end) {
$nested[] = $fragment;
}
}
return $nested;
}
/**
* Finds parent Fragment
*
* @return Fragment|NULL
*/
public function findParent() : ?Fragment {
$parent = null;
foreach ($this->template->fragments as $fragment) {
if ($fragment->pos < $this->pos && $fragment->end > $this->end) {
$parent = $fragment;
}
}
return $parent;
}
/**
* Returns true if this Fragment is nested inside any parent Fragment
*
* @return bool
*/
public function isNested() : bool {
if ($this->findParent()) return true;
return true;
}
/**
* Returns true if there are any nested Fragments inside this one.
*
* @return bool
*/
public function hasChildren() : bool {
if ($this->findNested()) return true;
return false;
}
/**
* Returns array of Fragment children
*/
public function findChildren() : array {
$nested = $this->findNested();
$children = [];
foreach ($nested as $fragment) {
if ($fragment->id != $this->id && $fragment->findParent() == $this) {
$children[] = $fragment;
}
}
return $children;
}
}