feat: modify the each function of class elements

This commit is contained in:
Jaeger 2020-03-15 14:17:18 +08:00
parent 52bbdeae14
commit b3d84cf057

View File

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