QueryList/src/Dom/Elements.php

70 lines
1.5 KiB
PHP
Raw Normal View History

2017-09-19 17:48:48 +08:00
<?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);
2017-09-19 19:06:16 +08:00
if($obj instanceof phpQueryObject){
$obj = new self($obj);
}else if(is_string($obj)){
$obj = trim($obj);
}
return $obj;
2017-09-19 17:48:48 +08:00
}
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);
});
}
2017-09-19 19:06:16 +08:00
public function texts()
{
return $this->map(function($item){
return trim($item->text());
});
}
2017-09-19 17:48:48 +08:00
2017-09-19 19:06:16 +08:00
public function htmls()
{
return $this->map(function($item){
return trim($item->html());
});
}
2017-09-19 17:48:48 +08:00
}