Skip to content

Commit b3d84cf

Browse files
committed
feat: modify the each function of class elements
1 parent 52bbdea commit b3d84cf

File tree

1 file changed

+67
-78
lines changed

1 file changed

+67
-78
lines changed

src/Dom/Elements.php

Lines changed: 67 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @package QL\Dom
1616
*
1717
* @method Elements toReference(&$var)
18-
* @method Elements documentFragment($state=null)
18+
* @method Elements documentFragment($state = null)
1919
* @method Elements toRoot()
2020
* @method Elements getDocumentIDRef(&$documentID)
2121
* @method Elements getDocument()
@@ -26,49 +26,49 @@
2626
* @method bool isXHTML()
2727
* @method bool isXML()
2828
* @method string serialize()
29-
* @method array serializeArray($submit=null)
30-
* @method \DOMElement|\DOMElement[] get($index=null,$callback1=null,$callback2=null,$callback3=null)
31-
* @method string|array getString($index=null,$callback1=null,$callback2=null,$callback3=null)
32-
* @method string|array getStrings($index=null,$callback1=null,$callback2=null,$callback3=null)
33-
* @method Elements newInstance($newStack=null)
34-
* @method Elements find($selectors,$context=null,$noHistory=false)
35-
* @method Elements|bool is($selector,$nodes=null)
36-
* @method Elements filterCallback($callback,$_skipHistory=false)
37-
* @method Elements filter($selectors,$_skipHistory=false)
38-
* @method Elements load($url,$data=null,$callback=null)
39-
* @method Elements trigger($type,$data=[])
40-
* @method Elements triggerHandler($type,$data=[])
41-
* @method Elements bind($type,$data,$callback=null)
42-
* @method Elements unbind($type=null,$callback=null)
43-
* @method Elements change($callback=null)
44-
* @method Elements submit($callback=null)
45-
* @method Elements click($callback=null)
29+
* @method array serializeArray($submit = null)
30+
* @method \DOMElement|\DOMElement[] get($index = null, $callback1 = null, $callback2 = null, $callback3 = null)
31+
* @method string|array getString($index = null, $callback1 = null, $callback2 = null, $callback3 = null)
32+
* @method string|array getStrings($index = null, $callback1 = null, $callback2 = null, $callback3 = null)
33+
* @method Elements newInstance($newStack = null)
34+
* @method Elements find($selectors, $context = null, $noHistory = false)
35+
* @method Elements|bool is($selector, $nodes = null)
36+
* @method Elements filterCallback($callback, $_skipHistory = false)
37+
* @method Elements filter($selectors, $_skipHistory = false)
38+
* @method Elements load($url, $data = null, $callback = null)
39+
* @method Elements trigger($type, $data = [])
40+
* @method Elements triggerHandler($type, $data = [])
41+
* @method Elements bind($type, $data, $callback = null)
42+
* @method Elements unbind($type = null, $callback = null)
43+
* @method Elements change($callback = null)
44+
* @method Elements submit($callback = null)
45+
* @method Elements click($callback = null)
4646
* @method Elements wrapAllOld($wrapper)
4747
* @method Elements wrapAll($wrapper)
48-
* @method Elements wrapAllPHP($codeBefore,$codeAfter)
48+
* @method Elements wrapAllPHP($codeBefore, $codeAfter)
4949
* @method Elements wrap($wrapper)
50-
* @method Elements wrapPHP($codeBefore,$codeAfter)
50+
* @method Elements wrapPHP($codeBefore, $codeAfter)
5151
* @method Elements wrapInner($wrapper)
52-
* @method Elements wrapInnerPHP($codeBefore,$codeAfter)
52+
* @method Elements wrapInnerPHP($codeBefore, $codeAfter)
5353
* @method Elements contents()
5454
* @method Elements contentsUnwrap()
5555
* @method Elements switchWith($markup)
5656
* @method Elements eq($num)
5757
* @method Elements size()
5858
* @method Elements length()
5959
* @method int count()
60-
* @method Elements end($level=1)
60+
* @method Elements end($level = 1)
6161
* @method Elements _clone()
6262
* @method Elements replaceWithPHP($code)
6363
* @method Elements replaceWith($content)
6464
* @method Elements replaceAll($selector)
65-
* @method Elements remove($selector=null)
66-
* @method Elements|string markup($markup=null,$callback1=null,$callback2=null,$callback3=null)
67-
* @method string markupOuter($callback1=null,$callback2=null,$callback3=null)
68-
* @method Elements|string html($html=null,$callback1=null,$callback2=null,$callback3=null)
69-
* @method Elements|string xml($xml=null,$callback1=null,$callback2=null,$callback3=null)
70-
* @method string htmlOuter($callback1=null,$callback2=null,$callback3=null)
71-
* @method string xmlOuter($callback1=null,$callback2=null,$callback3=null)
65+
* @method Elements remove($selector = null)
66+
* @method Elements|string markup($markup = null, $callback1 = null, $callback2 = null, $callback3 = null)
67+
* @method string markupOuter($callback1 = null, $callback2 = null, $callback3 = null)
68+
* @method Elements|string html($html = null, $callback1 = null, $callback2 = null, $callback3 = null)
69+
* @method Elements|string xml($xml = null, $callback1 = null, $callback2 = null, $callback3 = null)
70+
* @method string htmlOuter($callback1 = null, $callback2 = null, $callback3 = null)
71+
* @method string xmlOuter($callback1 = null, $callback2 = null, $callback3 = null)
7272
* @method Elements php($code)
7373
* @method string markupPHP($code)
7474
* @method string markupOuterPHP()
@@ -86,54 +86,52 @@
8686
* @method Elements after($content)
8787
* @method Elements afterPHP($content)
8888
* @method Elements insertAfter($seletor)
89-
* @method Elements insert($target,$type)
89+
* @method Elements insert($target, $type)
9090
* @method int index($subject)
91-
* @method Elements slice($start,$end=null)
91+
* @method Elements slice($start, $end = null)
9292
* @method Elements reverse()
93-
* @method Elements|string text($text=null,$callback1=null,$callback2=null,$callback3=null)
94-
* @method Elements plugin($class,$file=null)
95-
* @method Elements _next($selector=null)
96-
* @method Elements _prev($selector=null)
97-
* @method Elements prev($selector=null)
98-
* @method Elements prevAll($selector=null)
99-
* @method Elements nextAll($selector=null)
100-
* @method Elements siblings($selector=null)
101-
* @method Elements not($selector=null)
102-
* @method Elements add($selector=null)
103-
* @method Elements parent($selector=null)
104-
* @method Elements parents($selector=null)
105-
* @method Elements stack($nodeTypes=null)
106-
* @method Elements|string attr($attr=null,$value=null)
107-
* @method Elements attrPHP($attr,$code)
93+
* @method Elements|string text($text = null, $callback1 = null, $callback2 = null, $callback3 = null)
94+
* @method Elements plugin($class, $file = null)
95+
* @method Elements _next($selector = null)
96+
* @method Elements _prev($selector = null)
97+
* @method Elements prev($selector = null)
98+
* @method Elements prevAll($selector = null)
99+
* @method Elements nextAll($selector = null)
100+
* @method Elements siblings($selector = null)
101+
* @method Elements not($selector = null)
102+
* @method Elements add($selector = null)
103+
* @method Elements parent($selector = null)
104+
* @method Elements parents($selector = null)
105+
* @method Elements stack($nodeTypes = null)
106+
* @method Elements|string attr($attr = null, $value = null)
107+
* @method Elements attrPHP($attr, $code)
108108
* @method Elements removeAttr($attr)
109-
* @method Elements|string val($val=null)
109+
* @method Elements|string val($val = null)
110110
* @method Elements andSelf()
111111
* @method Elements addClass($className)
112112
* @method Elements addClassPHP($className)
113113
* @method bool hasClass($className)
114114
* @method Elements removeClass($className)
115115
* @method Elements toggleClass($className)
116116
* @method Elements _empty()
117-
* @method Elements each($callback,$param1=null,$param2=null,$param3=null)
118-
* @method Elements callback($callback,$param1=null,$param2=null,$param3=null)
119-
* @method string data($key,$value=null)
117+
* @method Elements callback($callback, $param1 = null, $param2 = null, $param3 = null)
118+
* @method string data($key, $value = null)
120119
* @method Elements removeData($key)
121120
* @method void rewind()
122121
* @method Elements current()
123122
* @method int key()
124-
* @method Elements next($cssSelector=null)
123+
* @method Elements next($cssSelector = null)
125124
* @method bool valid()
126125
* @method bool offsetExists($offset)
127126
* @method Elements offsetGet($offset)
128-
* @method void offsetSet($offset,$value)
127+
* @method void offsetSet($offset, $value)
129128
* @method string whois($oneNode)
130129
* @method Elements dump()
131130
* @method Elements dumpWhois()
132131
* @method Elements dumpLength()
133-
* @method Elements dumpTree($html,$title)
132+
* @method Elements dumpTree($html, $title)
134133
* @method dumpDie()
135134
*/
136-
137135
class Elements
138136
{
139137
/**
@@ -152,15 +150,15 @@ public function __construct(phpQueryObject $elements)
152150

153151
public function __get($name)
154152
{
155-
return property_exists($this->elements,$name)?$this->elements->$name:$this->elements->attr($name);
153+
return property_exists($this->elements, $name) ? $this->elements->$name : $this->elements->attr($name);
156154
}
157155

158156
public function __call($name, $arguments)
159157
{
160-
$obj = call_user_func_array([$this->elements,$name],$arguments);
161-
if($obj instanceof phpQueryObject){
158+
$obj = call_user_func_array([$this->elements, $name], $arguments);
159+
if ($obj instanceof phpQueryObject) {
162160
$obj = new self($obj);
163-
}else if(is_string($obj)){
161+
} else if (is_string($obj)) {
164162
$obj = trim($obj);
165163
}
166164
return $obj;
@@ -169,27 +167,18 @@ public function __call($name, $arguments)
169167
/**
170168
* Iterating elements
171169
*
172-
* @param callable $callback
170+
* @param callable $callback
173171
*
174172
* @return $this
175173
*/
176174
public function each(callable $callback)
177175
{
178-
$break = false;
179-
$this->elements->each(function ($dom) use ($callback, &$break) {
180-
if ( ! $dom || $break) {
181-
return;
182-
}
183-
$orig = $dom;
184-
$dom = new Elements(pq($dom));
185-
if (false === call_user_func($callback, $dom)) {
186-
$dom = $orig;
187-
$break = true;
188-
} else {
189-
$dom = $dom->getDOMDocument();
176+
foreach ($this->elements as $key => $element) {
177+
$break = $callback(new self(pq($element)), $key);
178+
if ($break === false) {
179+
break;
190180
}
191-
unset($orig);
192-
});
181+
}
193182

194183
return $this;
195184
}
@@ -203,7 +192,7 @@ public function each(callable $callback)
203192
public function map($callback)
204193
{
205194
$collection = collect();
206-
$this->elements->each(function($dom) use(& $collection,$callback){
195+
$this->elements->each(function ($dom) use (& $collection, $callback) {
207196
$collection->push($callback(new self(pq($dom))));
208197
});
209198
return $collection;
@@ -217,7 +206,7 @@ public function map($callback)
217206
*/
218207
public function attrs($attr)
219208
{
220-
return $this->map(function($item) use($attr){
209+
return $this->map(function ($item) use ($attr) {
221210
return $item->attr($attr);
222211
});
223212
}
@@ -229,7 +218,7 @@ public function attrs($attr)
229218
*/
230219
public function texts()
231220
{
232-
return $this->map(function($item){
221+
return $this->map(function ($item) {
233222
return trim($item->text());
234223
});
235224
}
@@ -241,7 +230,7 @@ public function texts()
241230
*/
242231
public function htmls()
243232
{
244-
return $this->map(function($item){
233+
return $this->map(function ($item) {
245234
return trim($item->html());
246235
});
247236
}
@@ -253,7 +242,7 @@ public function htmls()
253242
*/
254243
public function htmlOuters()
255244
{
256-
return $this->map(function($item){
245+
return $this->map(function ($item) {
257246
return trim($item->htmlOuter());
258247
});
259248
}

0 commit comments

Comments
 (0)