添加 each function 并和 Collection 保持一致,返回 false 时中断循环。

This commit is contained in:
Edwin Xu 2020-03-13 12:32:48 +08:00
parent 6ee6a26aee
commit 67f0052c5d

View File

@ -166,6 +166,34 @@ class Elements
return $obj; return $obj;
} }
/**
* Iterating elements
*
* @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();
}
unset($orig);
});
return $this;
}
/** /**
* Iterating elements * Iterating elements
* *
@ -184,7 +212,7 @@ class Elements
/** /**
* Gets the attributes of all the elements * Gets the attributes of all the elements
* *
* @param $attr HTML attribute name * @param string $attr HTML attribute name
* @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
*/ */
public function attrs($attr) public function attrs($attr)