From b3d84cf05736e1df8921fa0f5ef19be92dd48770 Mon Sep 17 00:00:00 2001 From: Jaeger Date: Sun, 15 Mar 2020 14:17:18 +0800 Subject: [PATCH] feat: modify the each function of class elements --- src/Dom/Elements.php | 145 ++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 78 deletions(-) diff --git a/src/Dom/Elements.php b/src/Dom/Elements.php index c57d20f..245121f 100644 --- a/src/Dom/Elements.php +++ b/src/Dom/Elements.php @@ -15,7 +15,7 @@ * @package QL\Dom * * @method Elements toReference(&$var) - * @method Elements documentFragment($state=null) + * @method Elements documentFragment($state = null) * @method Elements toRoot() * @method Elements getDocumentIDRef(&$documentID) * @method Elements getDocument() @@ -26,30 +26,30 @@ * @method bool isXHTML() * @method bool isXML() * @method string serialize() - * @method array serializeArray($submit=null) - * @method \DOMElement|\DOMElement[] get($index=null,$callback1=null,$callback2=null,$callback3=null) - * @method string|array getString($index=null,$callback1=null,$callback2=null,$callback3=null) - * @method string|array getStrings($index=null,$callback1=null,$callback2=null,$callback3=null) - * @method Elements newInstance($newStack=null) - * @method Elements find($selectors,$context=null,$noHistory=false) - * @method Elements|bool is($selector,$nodes=null) - * @method Elements filterCallback($callback,$_skipHistory=false) - * @method Elements filter($selectors,$_skipHistory=false) - * @method Elements load($url,$data=null,$callback=null) - * @method Elements trigger($type,$data=[]) - * @method Elements triggerHandler($type,$data=[]) - * @method Elements bind($type,$data,$callback=null) - * @method Elements unbind($type=null,$callback=null) - * @method Elements change($callback=null) - * @method Elements submit($callback=null) - * @method Elements click($callback=null) + * @method array serializeArray($submit = null) + * @method \DOMElement|\DOMElement[] get($index = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method string|array getString($index = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method string|array getStrings($index = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method Elements newInstance($newStack = null) + * @method Elements find($selectors, $context = null, $noHistory = false) + * @method Elements|bool is($selector, $nodes = null) + * @method Elements filterCallback($callback, $_skipHistory = false) + * @method Elements filter($selectors, $_skipHistory = false) + * @method Elements load($url, $data = null, $callback = null) + * @method Elements trigger($type, $data = []) + * @method Elements triggerHandler($type, $data = []) + * @method Elements bind($type, $data, $callback = null) + * @method Elements unbind($type = null, $callback = null) + * @method Elements change($callback = null) + * @method Elements submit($callback = null) + * @method Elements click($callback = null) * @method Elements wrapAllOld($wrapper) * @method Elements wrapAll($wrapper) - * @method Elements wrapAllPHP($codeBefore,$codeAfter) + * @method Elements wrapAllPHP($codeBefore, $codeAfter) * @method Elements wrap($wrapper) - * @method Elements wrapPHP($codeBefore,$codeAfter) + * @method Elements wrapPHP($codeBefore, $codeAfter) * @method Elements wrapInner($wrapper) - * @method Elements wrapInnerPHP($codeBefore,$codeAfter) + * @method Elements wrapInnerPHP($codeBefore, $codeAfter) * @method Elements contents() * @method Elements contentsUnwrap() * @method Elements switchWith($markup) @@ -57,18 +57,18 @@ * @method Elements size() * @method Elements length() * @method int count() - * @method Elements end($level=1) + * @method Elements end($level = 1) * @method Elements _clone() * @method Elements replaceWithPHP($code) * @method Elements replaceWith($content) * @method Elements replaceAll($selector) - * @method Elements remove($selector=null) - * @method Elements|string markup($markup=null,$callback1=null,$callback2=null,$callback3=null) - * @method string markupOuter($callback1=null,$callback2=null,$callback3=null) - * @method Elements|string html($html=null,$callback1=null,$callback2=null,$callback3=null) - * @method Elements|string xml($xml=null,$callback1=null,$callback2=null,$callback3=null) - * @method string htmlOuter($callback1=null,$callback2=null,$callback3=null) - * @method string xmlOuter($callback1=null,$callback2=null,$callback3=null) + * @method Elements remove($selector = null) + * @method Elements|string markup($markup = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method string markupOuter($callback1 = null, $callback2 = null, $callback3 = null) + * @method Elements|string html($html = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method Elements|string xml($xml = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method string htmlOuter($callback1 = null, $callback2 = null, $callback3 = null) + * @method string xmlOuter($callback1 = null, $callback2 = null, $callback3 = null) * @method Elements php($code) * @method string markupPHP($code) * @method string markupOuterPHP() @@ -86,27 +86,27 @@ * @method Elements after($content) * @method Elements afterPHP($content) * @method Elements insertAfter($seletor) - * @method Elements insert($target,$type) + * @method Elements insert($target, $type) * @method int index($subject) - * @method Elements slice($start,$end=null) + * @method Elements slice($start, $end = null) * @method Elements reverse() - * @method Elements|string text($text=null,$callback1=null,$callback2=null,$callback3=null) - * @method Elements plugin($class,$file=null) - * @method Elements _next($selector=null) - * @method Elements _prev($selector=null) - * @method Elements prev($selector=null) - * @method Elements prevAll($selector=null) - * @method Elements nextAll($selector=null) - * @method Elements siblings($selector=null) - * @method Elements not($selector=null) - * @method Elements add($selector=null) - * @method Elements parent($selector=null) - * @method Elements parents($selector=null) - * @method Elements stack($nodeTypes=null) - * @method Elements|string attr($attr=null,$value=null) - * @method Elements attrPHP($attr,$code) + * @method Elements|string text($text = null, $callback1 = null, $callback2 = null, $callback3 = null) + * @method Elements plugin($class, $file = null) + * @method Elements _next($selector = null) + * @method Elements _prev($selector = null) + * @method Elements prev($selector = null) + * @method Elements prevAll($selector = null) + * @method Elements nextAll($selector = null) + * @method Elements siblings($selector = null) + * @method Elements not($selector = null) + * @method Elements add($selector = null) + * @method Elements parent($selector = null) + * @method Elements parents($selector = null) + * @method Elements stack($nodeTypes = null) + * @method Elements|string attr($attr = null, $value = null) + * @method Elements attrPHP($attr, $code) * @method Elements removeAttr($attr) - * @method Elements|string val($val=null) + * @method Elements|string val($val = null) * @method Elements andSelf() * @method Elements addClass($className) * @method Elements addClassPHP($className) @@ -114,26 +114,24 @@ * @method Elements removeClass($className) * @method Elements toggleClass($className) * @method Elements _empty() - * @method Elements each($callback,$param1=null,$param2=null,$param3=null) - * @method Elements callback($callback,$param1=null,$param2=null,$param3=null) - * @method string data($key,$value=null) + * @method Elements callback($callback, $param1 = null, $param2 = null, $param3 = null) + * @method string data($key, $value = null) * @method Elements removeData($key) * @method void rewind() * @method Elements current() * @method int key() - * @method Elements next($cssSelector=null) + * @method Elements next($cssSelector = null) * @method bool valid() * @method bool offsetExists($offset) * @method Elements offsetGet($offset) - * @method void offsetSet($offset,$value) + * @method void offsetSet($offset, $value) * @method string whois($oneNode) * @method Elements dump() * @method Elements dumpWhois() * @method Elements dumpLength() - * @method Elements dumpTree($html,$title) + * @method Elements dumpTree($html, $title) * @method dumpDie() */ - class Elements { /** @@ -152,15 +150,15 @@ public function __construct(phpQueryObject $elements) public function __get($name) { - return property_exists($this->elements,$name)?$this->elements->$name:$this->elements->attr($name); + return property_exists($this->elements, $name) ? $this->elements->$name : $this->elements->attr($name); } public function __call($name, $arguments) { - $obj = call_user_func_array([$this->elements,$name],$arguments); - if($obj instanceof phpQueryObject){ + $obj = call_user_func_array([$this->elements, $name], $arguments); + if ($obj instanceof phpQueryObject) { $obj = new self($obj); - }else if(is_string($obj)){ + } else if (is_string($obj)) { $obj = trim($obj); } return $obj; @@ -169,27 +167,18 @@ public function __call($name, $arguments) /** * Iterating elements * - * @param callable $callback + * @param callable $callback * * @return $this */ public function each(callable $callback) { - $break = false; - $this->elements->each(function ($dom) use ($callback, &$break) { - if ( ! $dom || $break) { - return; - } - $orig = $dom; - $dom = new Elements(pq($dom)); - if (false === call_user_func($callback, $dom)) { - $dom = $orig; - $break = true; - } else { - $dom = $dom->getDOMDocument(); + foreach ($this->elements as $key => $element) { + $break = $callback(new self(pq($element)), $key); + if ($break === false) { + break; } - unset($orig); - }); + } return $this; } @@ -203,7 +192,7 @@ public function each(callable $callback) public function map($callback) { $collection = collect(); - $this->elements->each(function($dom) use(& $collection,$callback){ + $this->elements->each(function ($dom) use (& $collection, $callback) { $collection->push($callback(new self(pq($dom)))); }); return $collection; @@ -217,7 +206,7 @@ public function map($callback) */ public function attrs($attr) { - return $this->map(function($item) use($attr){ + return $this->map(function ($item) use ($attr) { return $item->attr($attr); }); } @@ -229,7 +218,7 @@ public function attrs($attr) */ public function texts() { - return $this->map(function($item){ + return $this->map(function ($item) { return trim($item->text()); }); } @@ -241,7 +230,7 @@ public function texts() */ public function htmls() { - return $this->map(function($item){ + return $this->map(function ($item) { return trim($item->html()); }); } @@ -253,7 +242,7 @@ public function htmls() */ public function htmlOuters() { - return $this->map(function($item){ + return $this->map(function ($item) { return trim($item->htmlOuter()); }); }