Skip to content

Commit

Permalink
feat: modify the each function of class elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jae-jae committed Mar 15, 2020
1 parent 52bbdea commit b3d84cf
Showing 1 changed file with 67 additions and 78 deletions.
145 changes: 67 additions & 78 deletions src/Dom/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -26,49 +26,49 @@
* @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)
* @method Elements eq($num)
* @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()
Expand All @@ -86,54 +86,52 @@
* @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)
* @method bool hasClass($className)
* @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
{
/**
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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);
});
}
Expand All @@ -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());
});
}
Expand All @@ -241,7 +230,7 @@ public function texts()
*/
public function htmls()
{
return $this->map(function($item){
return $this->map(function ($item) {
return trim($item->html());
});
}
Expand All @@ -253,7 +242,7 @@ public function htmls()
*/
public function htmlOuters()
{
return $this->map(function($item){
return $this->map(function ($item) {
return trim($item->htmlOuter());
});
}
Expand Down

0 comments on commit b3d84cf

Please sign in to comment.