add comments

This commit is contained in:
Jaeger 2017-10-09 01:48:56 +08:00
parent c0ed870dc8
commit f2c6ce7385
4 changed files with 114 additions and 3 deletions

View File

@ -25,12 +25,24 @@ class Config
} }
/**
* Get the Config instance
*
* @return null|Config
*/
public static function getInstance() public static function getInstance()
{ {
self::$instance || self::$instance = new self(); self::$instance || self::$instance = new self();
return self::$instance; return self::$instance;
} }
/**
* Global installation plugin
*
* @param $plugins
* @param array ...$opt
* @return $this
*/
public function use($plugins,...$opt) public function use($plugins,...$opt)
{ {
if(is_string($plugins)){ if(is_string($plugins)){
@ -41,6 +53,13 @@ class Config
return $this; return $this;
} }
/**
* Global binding custom method
*
* @param string $name
* @param Closure $provider
* @return $this
*/
public function bind(string $name, Closure $provider) public function bind(string $name, Closure $provider)
{ {
$this->binds[$name] = $provider; $this->binds[$name] = $provider;

View File

@ -167,6 +167,12 @@ class Elements
return $obj; return $obj;
} }
/**
* Iterating elements
*
* @param $callback
* @return \Illuminate\Support\Collection
*/
public function map($callback) public function map($callback)
{ {
$collection = collect(); $collection = collect();
@ -176,6 +182,12 @@ class Elements
return $collection; return $collection;
} }
/**
* Gets the attributes of all the elements
*
* @param $attr HTML attribute name
* @return \Illuminate\Support\Collection
*/
public function attrs($attr) public function attrs($attr)
{ {
return $this->map(function($item) use($attr){ return $this->map(function($item) use($attr){
@ -183,6 +195,11 @@ class Elements
}); });
} }
/**
* Gets the text of all the elements
*
* @return \Illuminate\Support\Collection
*/
public function texts() public function texts()
{ {
return $this->map(function($item){ return $this->map(function($item){
@ -190,6 +207,11 @@ class Elements
}); });
} }
/**
* Gets the html of all the elements
*
* @return \Illuminate\Support\Collection
*/
public function htmls() public function htmls()
{ {
return $this->map(function($item){ return $this->map(function($item){

View File

@ -30,12 +30,19 @@ class Query
$this->ql = $ql; $this->ql = $ql;
} }
/**
* @return mixed
*/
public function getHtml() public function getHtml()
{ {
return $this->html; return $this->html;
} }
/**
* @param $html
* @param null $charset
* @return QueryList
*/
public function setHtml($html, $charset = null) public function setHtml($html, $charset = null)
{ {
$this->html = value($html); $this->html = value($html);
@ -43,22 +50,49 @@ class Query
return $this->ql; return $this->ql;
} }
/**
* Get crawl results
*
* @param Closure|null $callback
* @return Collection|static
*/
public function getData(Closure $callback = null) public function getData(Closure $callback = null)
{ {
return is_null($callback) ? $this->data : $this->data->map($callback); return is_null($callback) ? $this->data : $this->data->map($callback);
} }
/**
* @param Collection $data
*/
public function setData(Collection $data) public function setData(Collection $data)
{ {
$this->data = $data; $this->data = $data;
} }
/**
* Searches for all elements that match the specified expression.
*
* @param $selector A string containing a selector expression to match elements against.
* @return Elements
*/
public function find($selector) public function find($selector)
{ {
return (new Dom($this->document))->find($selector); return (new Dom($this->document))->find($selector);
} }
/**
* Set crawl rule
*
* $rules = [
* 'rule_name1' => ['selector','HTML attribute | text | html','Tag filter list','callback'],
* 'rule_name2' => ['selector','HTML attribute | text | html','Tag filter list','callback'],
* // ...
* ]
*
* @param array $rules
* @return QueryList
*/
public function rules(array $rules) public function rules(array $rules)
{ {
$this->rules = $rules; $this->rules = $rules;
@ -66,12 +100,23 @@ class Query
} }
public function range($range) /**
* Set the slice area for crawl list
*
* @param $selector
* @return QueryList
*/
public function range($selector)
{ {
$this->range = $range; $this->range = $selector;
return $this->ql; return $this->ql;
} }
/**
* Remove HTML head,try to solve the garbled
*
* @return QueryList
*/
public function removeHead() public function removeHead()
{ {
$html = preg_replace('/<head.+?>.+<\/head>/is','<head></head>',$this->html); $html = preg_replace('/<head.+?>.+<\/head>/is','<head></head>',$this->html);
@ -79,6 +124,12 @@ class Query
return $this->ql; return $this->ql;
} }
/**
* Execute the query rule
*
* @param Closure|null $callback
* @return QueryList
*/
public function query(Closure $callback = null) public function query(Closure $callback = null)
{ {
$this->data = $this->getList(); $this->data = $this->getList();

View File

@ -73,22 +73,41 @@ class QueryList
$this->destruct(); $this->destruct();
} }
/**
* Get the QueryList instance
*
* @return QueryList
*/
public static function getInstance() public static function getInstance()
{ {
$instance = new self(); $instance = new self();
return $instance; return $instance;
} }
/**
* Get the Config instance
* @return null|Config
*/
public static function config() public static function config()
{ {
return Config::getInstance(); return Config::getInstance();
} }
/**
* Destruction of resources
*/
public function destruct() public function destruct()
{ {
phpQuery::$documents = []; phpQuery::$documents = [];
} }
/**
* Bind a custom method to the QueryList object
*
* @param string $name Invoking the name
* @param Closure $provide Called method
* @return $this
*/
public function bind(string $name,Closure $provide) public function bind(string $name,Closure $provide)
{ {
$this->kernel->bind($name,$provide); $this->kernel->bind($name,$provide);