-
Notifications
You must be signed in to change notification settings - Fork 6
/
CleanParamParserCore.php
106 lines (98 loc) · 2.75 KB
/
CleanParamParserCore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* vipnytt/RobotsTxtParser
*
* @link https://github.com/VIPnytt/RobotsTxtParser
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
*/
namespace vipnytt\RobotsTxtParser\Parser\Directives;
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
use vipnytt\RobotsTxtParser\Parser\UriParser;
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
/**
* Class CleanParamParserCore
*
* @package vipnytt\RobotsTxtParser\Parser\Directives
*/
abstract class CleanParamParserCore implements ParserInterface, RobotsTxtInterface
{
/**
* Clean-param array
* @var string[][]
*/
protected $cleanParam = [];
/**
* CleanParamParserCore constructor.
*/
public function __construct()
{
}
/**
* Add
*
* @param string $line
* @return bool
*/
public function add($line)
{
// split into parameter and path
$array = array_map('trim', mb_split('\s+', $line, 2));
if (isset($array[1])) {
// strip any invalid characters from path prefix
$uriParser = new UriParser(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1]));
$path = rtrim($uriParser->encode(), '*');
}
$path = empty($path) ? '/' : $path;
$param = array_map('trim', explode('&', $array[0]));
foreach ($param as $key) {
$this->cleanParam[$key][] = $path;
}
return true;
}
/**
* Render
*
* @param RenderHandler $handler
* @return bool
*/
public function render(RenderHandler $handler)
{
ksort($this->cleanParam);
return $handler->getLevel() >= 3 ? $this->renderExtensive($handler) : $this->renderCompressed($handler);
}
/**
* Render extensive
*
* @param RenderHandler $handler
* @return bool
*/
private function renderExtensive(RenderHandler $handler)
{
foreach ($this->cleanParam as $param => $paths) {
foreach ($paths as $path) {
$handler->add(self::DIRECTIVE_CLEAN_PARAM, $param . ' ' . $path);
}
}
return true;
}
/**
* Render compressed
*
* @param RenderHandler $handler
* @return bool
*/
private function renderCompressed(RenderHandler $handler)
{
$pair = $this->cleanParam;
while (($currentPair = current($pair)) !== false) {
$equalParams = array_keys($pair, $currentPair);
foreach ($currentPair as $path) {
$handler->add(self::DIRECTIVE_CLEAN_PARAM, implode('&', $equalParams) . ' ' . $path);
}
foreach ($equalParams as $param) {
unset($pair[$param]);
}
}
return true;
}
}