From fe749f08c2f2f1230fb8a9527df117f9dad75962 Mon Sep 17 00:00:00 2001 From: Jaeger Date: Tue, 19 Sep 2017 17:48:48 +0800 Subject: [PATCH] add Dom --- composer.json | 3 ++- src/Dom.php | 15 ------------- src/Dom/Dom.php | 30 +++++++++++++++++++++++++ src/Dom/Elements.php | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/QueryList.php | 6 +++-- 5 files changed, 89 insertions(+), 18 deletions(-) delete mode 100644 src/Dom.php create mode 100644 src/Dom/Dom.php create mode 100644 src/Dom/Elements.php 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 @@ - + * 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 @@ + + * 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 @@ document)->find($selector); + return (new Dom($this->document))->find($selector); }