1 | <?php |
||
23 | class UserAgentParser implements ParserInterface, RobotsTxtInterface |
||
24 | { |
||
25 | use DirectiveParserTrait; |
||
26 | |||
27 | /** |
||
28 | * Sub directives white list |
||
29 | */ |
||
30 | const SUB_DIRECTIVES = [ |
||
31 | self::DIRECTIVE_ALLOW => 'allow', |
||
32 | self::DIRECTIVE_CACHE_DELAY => 'cacheDelay', |
||
33 | self::DIRECTIVE_COMMENT => 'comment', |
||
34 | self::DIRECTIVE_CRAWL_DELAY => 'crawlDelay', |
||
35 | self::DIRECTIVE_DISALLOW => 'disallow', |
||
36 | self::DIRECTIVE_NO_INDEX => 'noIndex', |
||
37 | self::DIRECTIVE_REQUEST_RATE => 'requestRate', |
||
38 | self::DIRECTIVE_ROBOT_VERSION => 'robotVersion', |
||
39 | self::DIRECTIVE_VISIT_TIME => 'visitTime', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * Base uri |
||
44 | * @var string |
||
45 | */ |
||
46 | private $base; |
||
47 | |||
48 | /** |
||
49 | * User-agent handler |
||
50 | * @var SubDirectiveHandler[] |
||
51 | */ |
||
52 | private $handler = []; |
||
53 | |||
54 | /** |
||
55 | * Current User-agent(s) |
||
56 | * @var string[] |
||
57 | */ |
||
58 | private $current = []; |
||
59 | |||
60 | /** |
||
61 | * Append User-agent |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $append = false; |
||
65 | |||
66 | /** |
||
67 | * UserAgent constructor. |
||
68 | * |
||
69 | * @param string $base |
||
70 | */ |
||
71 | public function __construct($base) |
||
76 | |||
77 | /** |
||
78 | * Add sub-directive handler |
||
79 | * |
||
80 | * @param string $group |
||
81 | * @return bool |
||
82 | */ |
||
83 | private function handlerAdd($group) |
||
91 | |||
92 | /** |
||
93 | * Add line |
||
94 | * |
||
95 | * @param string $line |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function add($line) |
||
115 | |||
116 | /** |
||
117 | * Set new User-agent |
||
118 | * |
||
119 | * @param string $group |
||
120 | * @return bool |
||
121 | */ |
||
122 | private function set($group) |
||
133 | |||
134 | /** |
||
135 | * Render |
||
136 | * |
||
137 | * @param RenderHandler $handler |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function render(RenderHandler $handler) |
||
144 | |||
145 | /** |
||
146 | * Render extensive |
||
147 | * |
||
148 | * @param RenderHandler $handler |
||
149 | * @return bool |
||
150 | */ |
||
151 | private function renderExtensive(RenderHandler $handler) |
||
161 | |||
162 | /** |
||
163 | * User-agent list |
||
164 | * |
||
165 | * @return string[] |
||
166 | */ |
||
167 | public function getUserAgents() |
||
173 | |||
174 | /** |
||
175 | * Add sub-directives to the RenderHandler |
||
176 | * |
||
177 | * @param string $userAgent |
||
178 | * @param RenderHandler $handler |
||
179 | */ |
||
180 | private function renderAdd($userAgent, RenderHandler $handler) |
||
198 | |||
199 | /** |
||
200 | * Render compressed |
||
201 | * |
||
202 | * @param RenderHandler $handler |
||
203 | * @return bool |
||
204 | */ |
||
205 | private function renderCompressed(RenderHandler $handler) |
||
221 | |||
222 | /** |
||
223 | * Export |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function export() |
||
240 | |||
241 | /** |
||
242 | * Client |
||
243 | * |
||
244 | * @param string $product |
||
245 | * @param float|int|string|null $version |
||
246 | * @param int|null $statusCode |
||
247 | * @return UserAgentClient |
||
248 | */ |
||
249 | public function client($product = self::USER_AGENT, $version = null, $statusCode = null) |
||
261 | } |
||
262 |