| @@ 13-93 (lines=81) @@ | ||
| 10 | * |
|
| 11 | * @package vipnytt\RobotsTxtParser\Directives |
|
| 12 | */ |
|
| 13 | class Allow implements DirectiveInterface, RobotsTxtInterface |
|
| 14 | { |
|
| 15 | use UrlToolbox; |
|
| 16 | use ObjectTools; |
|
| 17 | ||
| 18 | const SUB_DIRECTIVES = [ |
|
| 19 | self::DIRECTIVE_CLEAN_PARAM, |
|
| 20 | self::DIRECTIVE_HOST, |
|
| 21 | ]; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Directive |
|
| 25 | */ |
|
| 26 | const DIRECTIVE = 'Allow'; |
|
| 27 | ||
| 28 | protected $array = []; |
|
| 29 | protected $parent; |
|
| 30 | ||
| 31 | protected $cleanParam; |
|
| 32 | protected $host; |
|
| 33 | ||
| 34 | ||
| 35 | public function __construct($array, $parent = null) |
|
| 36 | { |
|
| 37 | $this->array = $array; |
|
| 38 | $this->cleanParam = new CleanParam([], self::DIRECTIVE); |
|
| 39 | $this->host = new Host([], self::DIRECTIVE); |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Add |
|
| 44 | * |
|
| 45 | * @param string $line |
|
| 46 | * @return bool |
|
| 47 | */ |
|
| 48 | public function add($line) |
|
| 49 | { |
|
| 50 | $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
|
| 51 | switch ($pair['directive']) { |
|
| 52 | case self::DIRECTIVE_CLEAN_PARAM: |
|
| 53 | return $this->cleanParam->add($pair['value']); |
|
| 54 | case self::DIRECTIVE_HOST: |
|
| 55 | return $this->host->add($pair['value']); |
|
| 56 | } |
|
| 57 | return $this->addPath($line); |
|
| 58 | } |
|
| 59 | ||
| 60 | protected function addPath($rule) |
|
| 61 | { |
|
| 62 | // Return an array of paths |
|
| 63 | if (isset($this->array['path']) && in_array($rule, $this->array['path'])) { |
|
| 64 | return false; |
|
| 65 | } |
|
| 66 | $this->array['path'][] = $rule; |
|
| 67 | return true; |
|
| 68 | } |
|
| 69 | ||
| 70 | /** |
|
| 71 | * Check |
|
| 72 | * |
|
| 73 | * @param string $url |
|
| 74 | * @return bool |
|
| 75 | */ |
|
| 76 | public function check($url) |
|
| 77 | { |
|
| 78 | $path = $this->getPath($url); |
|
| 79 | return ( |
|
| 80 | $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) || |
|
| 81 | $this->cleanParam->check($path) || |
|
| 82 | $this->host->check($url) |
|
| 83 | ); |
|
| 84 | } |
|
| 85 | ||
| 86 | public function export() |
|
| 87 | { |
|
| 88 | $result = $this->array |
|
| 89 | + $this->cleanParam->export() |
|
| 90 | + $this->host->export(); |
|
| 91 | return empty($result) ? [] : [self::DIRECTIVE => $result]; |
|
| 92 | } |
|
| 93 | } |
|
| 94 | ||
| @@ 13-92 (lines=80) @@ | ||
| 10 | * |
|
| 11 | * @package vipnytt\RobotsTxtParser\Directives |
|
| 12 | */ |
|
| 13 | class Disallow implements DirectiveInterface, RobotsTxtInterface |
|
| 14 | { |
|
| 15 | use UrlToolbox; |
|
| 16 | use ObjectTools; |
|
| 17 | ||
| 18 | const SUB_DIRECTIVES = [ |
|
| 19 | self::DIRECTIVE_CLEAN_PARAM, |
|
| 20 | self::DIRECTIVE_HOST, |
|
| 21 | ]; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * Directive |
|
| 25 | */ |
|
| 26 | const DIRECTIVE = 'Disallow'; |
|
| 27 | ||
| 28 | protected $array = []; |
|
| 29 | protected $parent; |
|
| 30 | ||
| 31 | protected $cleanParam; |
|
| 32 | protected $host; |
|
| 33 | ||
| 34 | public function __construct($array, $parent = null) |
|
| 35 | { |
|
| 36 | $this->array = $array; |
|
| 37 | $this->cleanParam = new CleanParam([], self::DIRECTIVE); |
|
| 38 | $this->host = new Host([], self::DIRECTIVE); |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * Add |
|
| 43 | * |
|
| 44 | * @param string $line |
|
| 45 | * @return bool |
|
| 46 | */ |
|
| 47 | public function add($line) |
|
| 48 | { |
|
| 49 | $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES); |
|
| 50 | switch ($pair['directive']) { |
|
| 51 | case self::DIRECTIVE_CLEAN_PARAM: |
|
| 52 | return $this->cleanParam->add($pair['value']); |
|
| 53 | case self::DIRECTIVE_HOST: |
|
| 54 | return $this->host->add($pair['value']); |
|
| 55 | } |
|
| 56 | return $this->addPath($line); |
|
| 57 | } |
|
| 58 | ||
| 59 | protected function addPath($rule) |
|
| 60 | { |
|
| 61 | // Return an array of paths |
|
| 62 | if (isset($this->array['path']) && in_array($rule, $this->array['path'])) { |
|
| 63 | return false; |
|
| 64 | } |
|
| 65 | $this->array['path'][] = $rule; |
|
| 66 | return true; |
|
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * Check |
|
| 71 | * |
|
| 72 | * @param string $url |
|
| 73 | * @return bool |
|
| 74 | */ |
|
| 75 | public function check($url) |
|
| 76 | { |
|
| 77 | $path = $this->getPath($url); |
|
| 78 | return ( |
|
| 79 | $this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) || |
|
| 80 | $this->cleanParam->check($path) || |
|
| 81 | $this->host->check($url) |
|
| 82 | ); |
|
| 83 | } |
|
| 84 | ||
| 85 | public function export() |
|
| 86 | { |
|
| 87 | $result = $this->array |
|
| 88 | + $this->cleanParam->export() |
|
| 89 | + $this->host->export(); |
|
| 90 | return empty($result) ? [] : [self::DIRECTIVE => $result]; |
|
| 91 | } |
|
| 92 | } |
|
| 93 | ||