diff --git a/composer.json b/composer.json index da0a761..ea31eb9 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "homepage": "http://querylist.cc", "require": { "PHP":">=5.6.0", - "jaeger/phpquery-single": "^0.9" + "jaeger/phpquery-single": "^0.9", + "illuminate/support": "^5.5" }, "suggest":{ "monolog/monolog":"Log support" diff --git a/src/Dom.php b/src/Dom.php deleted file mode 100644 index 6e3b1ff..0000000 --- a/src/Dom.php +++ /dev/null @@ -1,15 +0,0 @@ -<?php -/** - * Created by PhpStorm. - * User: iamhj - * Date: 2017/9/19 - * Time: 2:00 - */ - -namespace QL; - - -class Dom -{ - -} \ No newline at end of file diff --git a/src/Dom/Dom.php b/src/Dom/Dom.php new file mode 100644 index 0000000..c83bea3 --- /dev/null +++ b/src/Dom/Dom.php @@ -0,0 +1,30 @@ +<?php +/** + * Created by PhpStorm. + * User: Jaeger <JaegerCode@gmail.com> + * Date: 2017/9/19 + */ + +namespace QL\Dom; + +use phpQueryObject; + +class Dom +{ + + protected $document; + + /** + * Dom constructor. + */ + public function __construct(phpQueryObject $document) + { + $this->document = $document; + } + + public function find($selector) + { + $elements = $this->document->find($selector); + return new Elements($elements); + } +} \ No newline at end of file diff --git a/src/Dom/Elements.php b/src/Dom/Elements.php new file mode 100644 index 0000000..4df312f --- /dev/null +++ b/src/Dom/Elements.php @@ -0,0 +1,53 @@ +<?php +/** + * Created by PhpStorm. + * User: Jaeger <JaegerCode@gmail.com> + * Date: 2017/9/19 + */ + +namespace QL\Dom; + +use phpQueryObject; + +class Elements +{ + protected $elements; + + /** + * Elements constructor. + * @param $elements + */ + public function __construct(phpQueryObject $elements) + { + $this->elements = $elements; + } + + public function __get($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); + return $obj instanceof phpQueryObject?(new self($obj)):$obj; + } + + public function map($callback) + { + $collection = collect(); + $this->elements->each(function($dom) use(& $collection,$callback){ + $collection->push($callback(new self(pq($dom)))); + }); + return $collection; + } + + public function attrs($attr) + { + return $this->map(function($item) use($attr){ + return $item->attr($attr); + }); + } + + +} \ No newline at end of file diff --git a/src/QueryList.php b/src/QueryList.php index d1a93c8..632ca86 100644 --- a/src/QueryList.php +++ b/src/QueryList.php @@ -1,6 +1,7 @@ <?php namespace QL; use phpQuery; +use QL\Dom\Dom; /** * QueryList @@ -36,7 +37,8 @@ class QueryList } /** - * @param mixed $html + * @param $html + * @return $this */ public function setHtml($html) { @@ -47,7 +49,7 @@ class QueryList public function find($selector) { - return pq($this->document)->find($selector); + return (new Dom($this->document))->find($selector); }