fix: data callback

This commit is contained in:
Jaeger 2020-04-01 22:03:50 +08:00
parent c16826a573
commit faea883c6f
7 changed files with 58 additions and 45 deletions

View File

@ -32,6 +32,9 @@
}, },
"require-dev": { "require-dev": {
"symfony/var-dumper": "^3.3", "symfony/var-dumper": "^3.3",
"phpunit/phpunit": "^7.5" "phpunit/phpunit": "^8.5"
},
"scripts": {
"test": "./vendor/bin/phpunit"
} }
} }

View File

@ -64,7 +64,7 @@ class Query
*/ */
public function getData(Closure $callback = null) public function getData(Closure $callback = null)
{ {
return is_null($callback) ? $this->data : $this->data->map($callback); return $this->handleData($this->data, $callback);
} }
/** /**
@ -139,10 +139,23 @@ class Query
public function query(Closure $callback = null) public function query(Closure $callback = null)
{ {
$this->data = $this->getList(); $this->data = $this->getList();
$callback && $this->data = $this->data->map($callback); $this->data = $this->handleData($this->data, $callback);
return $this->ql; return $this->ql;
} }
public function handleData(Collection $data, $callback)
{
if (is_callable($callback)) {
if (empty($this->range)) {
$data = collect($callback($data->all(), null));
} else {
$data = $data->map($callback);
}
}
return $data;
}
protected function getList() protected function getList()
{ {
$data = []; $data = [];
@ -257,10 +270,8 @@ class Query
{ {
$tagArr = preg_split("/\s+/", $tags_str, -1, PREG_SPLIT_NO_EMPTY); $tagArr = preg_split("/\s+/", $tags_str, -1, PREG_SPLIT_NO_EMPTY);
$tags = array(array(), array()); $tags = array(array(), array());
foreach($tagArr as $tag) foreach ($tagArr as $tag) {
{ if (preg_match('/-(.+)/', $tag, $arr)) {
if(preg_match('/-(.+)/', $tag,$arr))
{
array_push($tags[1], $arr[1]); array_push($tags[1], $arr[1]);
} else { } else {
array_push($tags[0], $tag); array_push($tags[0], $tag);
@ -278,8 +289,7 @@ class Query
protected function removeTags($html, $tags) protected function removeTags($html, $tags)
{ {
$tag_str = ''; $tag_str = '';
if(count($tags)) if (count($tags)) {
{
foreach ($tags as $tag) { foreach ($tags as $tag) {
$tag_str .= $tag_str ? ',' . $tag : $tag; $tag_str .= $tag_str ? ',' . $tag : $tag;
} }

View File

@ -17,7 +17,7 @@ class FindTest extends TestCaseBase
protected $html; protected $html;
protected $ql; protected $ql;
public function setUp() protected function setUp(): void
{ {
$this->html = $this->getSnippet('snippet-1'); $this->html = $this->getSnippet('snippet-1');
$this->ql = QueryList::html($this->html); $this->ql = QueryList::html($this->html);

View File

@ -18,7 +18,7 @@ class RulesTest extends TestCaseBase
protected $html; protected $html;
protected $ql; protected $ql;
public function setUp() protected function setUp(): void
{ {
$this->html = $this->getSnippet('snippet-2'); $this->html = $this->getSnippet('snippet-2');
$this->ql = QueryList::html($this->html); $this->ql = QueryList::html($this->html);

View File

@ -18,7 +18,7 @@ class HttpTest extends TestCaseBase
{ {
protected $urls; protected $urls;
public function setUp() protected function setUp(): void
{ {
$this->urls = [ $this->urls = [
'http://httpbin.org/get?name=php', 'http://httpbin.org/get?name=php',

View File

@ -16,7 +16,7 @@ class InstanceTest extends TestCaseBase
{ {
protected $html; protected $html;
public function setUp() protected function setUp(): void
{ {
$this->html = $this->getSnippet('snippet-1'); $this->html = $this->getSnippet('snippet-1');
} }
@ -38,11 +38,11 @@ class InstanceTest extends TestCaseBase
public function get_new_object() public function get_new_object()
{ {
$ql = (new QueryList())->html($this->html); $ql = (new QueryList())->html($this->html);
$ql2 = new QueryList(); $ql2 = (new QueryList())->html('');
$this->assertNotEquals($ql->getHtml(),$ql2->getHtml()); $this->assertNotEquals($ql->getHtml(),$ql2->getHtml());
$ql = QueryList::range('')->html($this->html); $ql = QueryList::range('')->html($this->html);
$ql2 = QueryList::range(''); $ql2 = QueryList::range('')->html('');
$this->assertNotEquals($ql->getHtml(),$ql2->getHtml()); $this->assertNotEquals($ql->getHtml(),$ql2->getHtml());
} }
} }

View File

@ -16,7 +16,7 @@ class MethodTest extends TestCaseBase
{ {
protected $html; protected $html;
public function setUp() protected function setUp(): void
{ {
$this->html = $this->getSnippet('snippet-1'); $this->html = $this->getSnippet('snippet-1');
} }
@ -30,7 +30,7 @@ class MethodTest extends TestCaseBase
$qlHtml = QueryList::pipe(function(QueryList $ql) use($html){ $qlHtml = QueryList::pipe(function(QueryList $ql) use($html){
$ql->setHtml($html); $ql->setHtml($html);
return $ql; return $ql;
})->getHtml(); })->getHtml(false);
$this->assertEquals($html,$qlHtml); $this->assertEquals($html,$qlHtml);
} }
} }