Skip to content

Commit 91b486d

Browse files
committed
update advanced html dom
1 parent 8ed44de commit 91b486d

File tree

11 files changed

+102
-91
lines changed

11 files changed

+102
-91
lines changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
"license": "MIT",
1414
"type": "library",
1515
"require": {
16-
"php": "^7.0",
17-
"ext-dom": "*",
16+
"php": "^7.1",
1817
"ext-xml": "*"
1918
},
2019
"autoload": {
2120
"files": [
22-
"src/AdvancedHtmlDom/functions.php"
21+
"helpers/functions.php"
2322
],
2423
"psr-4": {
2524
"Bavix\\": "src/"

demo/simple.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
include dirname(__DIR__) . '/vendor/autoload.php';
44

5-
$dom = new \Deimos\AdvancedHtmlDom\AdvancedHtmlDom();
5+
$dom = new \Bavix\AdvancedHtmlDom\AdvancedHtmlDom();
66

77
$dom->loadFile('https://babichev.net/portfolio');
88

demo/simpleWithCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// test 1
66
$mt = microtime(true);
77

8-
$dom = new \Deimos\AdvancedHtmlDom\AdvancedHtmlDom();
9-
$dom->setCache(new \Deimos\AdvancedHtmlDom\CacheSystem\CacheStatic());
8+
$dom = new \Bavix\AdvancedHtmlDom\AdvancedHtmlDom();
9+
$dom->setCache(new \Bavix\AdvancedHtmlDom\CacheSystem\CacheStatic());
1010

1111
$dom->loadFile('https://babichev.net/portfolio');
1212
$dom->loadFile('https://babichev.net/portfolio');
@@ -19,7 +19,7 @@
1919
// test 2
2020
$mt = microtime(true);
2121

22-
$dom = new \Deimos\AdvancedHtmlDom\AdvancedHtmlDom();
22+
$dom = new \Bavix\AdvancedHtmlDom\AdvancedHtmlDom();
2323

2424
$dom->loadFile('https://babichev.net/portfolio');
2525
$dom->loadFile('https://babichev.net/portfolio');
File renamed without changes.

src/AdvancedHtmlDom/AHTMLNode.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,20 @@ public function after($html)
8787
*/
8888
public function decamelize($str)
8989
{
90-
$str = preg_replace_callback(
90+
$str = \preg_replace_callback(
9191
'/(^|[a-z])([A-Z])/',
92-
function ($matches)
93-
{
92+
function ($matches) {
9493
return
95-
strtolower(
96-
strlen($matches[1])
94+
\strtolower(
95+
\strlen($matches[1])
9796
? $matches[1] . '_' . $matches[2] :
9897
$matches[2]
9998
);
10099
},
101100
$str
102101
);
103102

104-
return preg_replace('/ /', '_', strtolower($str));
103+
return \preg_replace('/ /', '_', \strtolower($str));
105104
}
106105

107106
/**
@@ -134,19 +133,23 @@ public function flatten($key = null, $level = 1)
134133
if ($this->at('./preceding-sibling::' . $this->tag) || $this->at('./following-sibling::' . $this->tag) || ($key = $this->tag . 's'))
135134
{
136135
$count = $this->search('./preceding-sibling::' . $this->tag)->length + 1;
137-
$tag .= '_' . $count;
136+
$tag .= '_' . $count;
138137
}
139138

140139
if ($children->length == 0)
141140
{
142-
$ret[$this->decamelize(implode(' ', array_filter(array($key, $tag))))] = $this->text;
141+
$ret[$this->decamelize(\implode(' ', \array_filter(array($key, $tag))))] = $this->text;
143142
}
144143
else
145144
{
145+
$flatten = [];
146146
foreach ($children as $child)
147147
{
148-
$ret = array_merge($ret, $child->flatten(implode(' ', array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1));
148+
$flatten[] = $child->flatten(\implode(' ', \array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1);
149+
// $ret = array_merge($ret, $child->flatten(implode(' ', array_filter(array($key, $level <= 0 ? $tag : null))), $level - 1));
149150
}
151+
152+
$ret = \array_merge($ret, ...$flatten);
150153
}
151154

152155
return $ret;
@@ -228,7 +231,7 @@ public function offsetSet($key, $value)
228231
*/
229232
public function offsetUnset($offset)
230233
{
231-
trigger_error('offsetUnset not implemented', E_USER_WARNING);
234+
\trigger_error('offsetUnset not implemented', E_USER_WARNING);
232235
}
233236

234237
/**

src/AdvancedHtmlDom/AHTMLNodeList.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ public function offsetGet($offset)
6565
*/
6666
public function offsetSet($offset, $value)
6767
{
68-
trigger_error('offsetSet not implemented', E_USER_WARNING);
68+
\trigger_error('offsetSet not implemented', E_USER_WARNING);
6969
}
7070

7171
/**
7272
* @param mixed $offset
7373
*/
7474
public function offsetUnset($offset)
7575
{
76-
trigger_error('offsetUnset not implemented', E_USER_WARNING);
76+
\trigger_error('offsetUnset not implemented', E_USER_WARNING);
7777
}
7878

7979
/**
@@ -121,7 +121,7 @@ public function next()
121121
*/
122122
public function valid()
123123
{
124-
return $this->counter < $this->nodeList->length;
124+
return $this->nodeList && $this->counter < $this->nodeList->length;
125125
}
126126

127127
/**
@@ -163,6 +163,7 @@ public function map($c)
163163

164164

165165
//math methods
166+
166167
/**
167168
* @param $nl
168169
* @param string $op
@@ -185,13 +186,13 @@ public function doMath($nl, $op = 'plus')
185186
switch ($op)
186187
{
187188
case 'plus':
188-
$new_paths = array_unique(array_merge($paths, $other_paths));
189+
$new_paths = \array_unique(\array_merge($paths, $other_paths));
189190
break;
190191
case 'minus':
191-
$new_paths = array_diff($paths, $other_paths);
192+
$new_paths = \array_diff($paths, $other_paths);
192193
break;
193194
case 'intersect':
194-
$new_paths = array_intersect($paths, $other_paths);
195+
$new_paths = \array_intersect($paths, $other_paths);
195196
break;
196197
}
197198

@@ -230,6 +231,7 @@ public function intersect($nl)
230231

231232

232233
// magic methods
234+
233235
/**
234236
* @param $key
235237
* @param $values
@@ -238,7 +240,7 @@ public function intersect($nl)
238240
*/
239241
public function __call($key, $values)
240242
{
241-
$key = strtolower(str_replace('_', '', $key));
243+
$key = \strtolower(\str_replace('_', '', $key));
242244
switch ($key)
243245
{
244246
case 'to_a':
@@ -259,7 +261,7 @@ public function __call($key, $values)
259261
if(preg_match(TAG_REGEX, $key, $m)) return $this->find($m[1], 0);
260262
*/
261263

262-
if (preg_match(ATTRIBUTES_REGEX, $key, $m) || preg_match('/^((clean|trim|str).*)s$/', $key, $m))
264+
if (\preg_match(ATTRIBUTES_REGEX, $key, $m) || \preg_match('/^((clean|trim|str).*)s$/', $key, $m))
263265
{
264266
foreach ($this as $node)
265267
{
@@ -270,15 +272,15 @@ public function __call($key, $values)
270272
return $retval;
271273
}
272274

273-
if (preg_match(ATTRIBUTE_REGEX, $key, $m))
275+
if (\preg_match(ATTRIBUTE_REGEX, $key, $m))
274276
{
275277
foreach ($this as $node)
276278
{
277279
$arg = $m[1];
278280
$retval[] = $node->$arg;
279281
}
280282

281-
return implode('', $retval);
283+
return \implode('', $retval);
282284
}
283285

284286
// what now?
@@ -287,7 +289,7 @@ public function __call($key, $values)
287289
$retval[] = isset($values[0]) ? $node->$key($values[0]) : $node->$key();
288290
}
289291

290-
return implode('', $retval);
292+
return \implode('', $retval);
291293
}
292294

293295
/**

src/AdvancedHtmlDom/AdvancedHtmlBase.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function scan($re)
9898
*/
9999
public function clean($str)
100100
{
101-
return $this->trim(preg_replace('/\s+/', ' ', $str));
101+
return $this->trim(\preg_replace('/\s+/', ' ', $str));
102102
}
103103

104104
/**
@@ -108,14 +108,14 @@ public function clean($str)
108108
*/
109109
public function trim($str)
110110
{
111-
return trim($str);
111+
return \trim($str);
112112
}
113113

114114
/**
115115
* @param $css
116116
* @param null $index
117117
*
118-
* @return AHTMLNode[]|AHTMLNodeList
118+
* @return AHTMLNode|AHTMLNodeList
119119
*/
120120
public function find($css, $index = null)
121121
{
@@ -142,6 +142,7 @@ public function find($css, $index = null)
142142
}
143143

144144
// magic methods
145+
145146
/**
146147
* @param $key
147148
* @param $args
@@ -150,7 +151,7 @@ public function find($css, $index = null)
150151
*/
151152
public function __call($key, $args)
152153
{
153-
$key = strtolower(str_replace('_', '', $key));
154+
$key = \strtolower(\str_replace('_', '', $key));
154155
switch ($key)
155156
{
156157
case 'innertext':
@@ -251,25 +252,25 @@ public function __call($key, $args)
251252
}
252253

253254
// $doc->spans[x]
254-
if (preg_match(TAGS_REGEX, $key, $m))
255+
if (\preg_match(TAGS_REGEX, $key, $m))
255256
{
256257
return $this->find($m[1]);
257258
}
258-
if (preg_match(TAG_REGEX, $key, $m))
259+
if (\preg_match(TAG_REGEX, $key, $m))
259260
{
260261
return $this->find($m[1], 0);
261262
}
262263

263-
if (preg_match('/(clean|trim|str)(.*)/', $key, $m) && isset($m[2]))
264+
if (\preg_match('/(clean|trim|str)(.*)/', $key, $m) && isset($m[2]))
264265
{
265266
list($arg0, $arg1, $arg2) = $m;
266267

267268
return $this->$arg1($this->$arg2);
268269
}
269270

270-
if (!preg_match(ATTRIBUTE_REGEX, $key, $m))
271+
if (!\preg_match(ATTRIBUTE_REGEX, $key, $m))
271272
{
272-
trigger_error('Unknown method or property: ' . $key, E_USER_WARNING);
273+
\trigger_error('Unknown method or property: ' . $key, E_USER_WARNING);
273274
}
274275
if (!$this->node || $this->is_text)
275276
{

src/AdvancedHtmlDom/AdvancedHtmlDom.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function cache($url)
5454
{
5555
if (!$this->cache)
5656
{
57-
return file_get_contents($url);
57+
return \file_get_contents($url);
5858
}
5959

6060
return $this->cache->get($url);
@@ -69,7 +69,7 @@ public function load($html, $is_xml = false)
6969
$this->dom = new \DOMDocument();
7070
if ($is_xml)
7171
{
72-
@$this->dom->loadXML(preg_replace('/xmlns=".*?"/ ', '', $html));
72+
@$this->dom->loadXML(\preg_replace('/xmlns=".*?"/ ', '', $html));
7373
}
7474
else
7575
{
@@ -105,6 +105,7 @@ public function loadFile($file, $is_xml = false)
105105
}
106106

107107
// special cases
108+
108109
/**
109110
* @return mixed
110111
*/

0 commit comments

Comments
 (0)