1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EndorphinStudio\Detector\Detection; |
4
|
|
|
|
5
|
|
|
use EndorphinStudio\Detector\Tools; |
6
|
|
|
|
7
|
|
|
class RobotDetector extends AbstractDetection |
8
|
|
|
{ |
9
|
|
|
protected $configKey = 'robot'; |
10
|
|
|
|
11
|
|
|
/** @var array */ |
12
|
|
|
private $homepages; |
13
|
|
|
|
14
|
|
|
protected function setupResultObject() |
15
|
|
|
{ |
16
|
|
|
$this->homepages = $this->config['homepages']; |
17
|
|
|
$this->config = $this->config['types']; |
18
|
|
|
$object = $this->detectByConfig(); |
19
|
|
|
foreach ($object as $key => $value) { |
20
|
|
|
Tools::runSetter($this->resultObject, $key, $value); |
21
|
|
|
} |
22
|
|
|
if (\array_key_exists('name', $object) && $object['name']) { |
23
|
|
|
$this->detector->getResultObject()->setIsRobot(true); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function detectByConfig(): array |
28
|
|
|
{ |
29
|
|
|
foreach ($this->config as $type => $companyList) { |
30
|
|
|
$data = $this->detectByType($type); |
31
|
|
|
if ($data) { |
|
|
|
|
32
|
|
|
return \array_merge($data, ['homepage' => \array_key_exists($data['owner'], $this->homepages) ? $this->homepages[$data['owner']] : null, 'type' => $type]); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function detectByType($key = 'none'): array |
39
|
|
|
{ |
40
|
|
|
foreach ($this->config[$key] as $companyName => $patternList) { |
41
|
|
|
$data = $this->detectByPattern($patternList); |
42
|
|
|
if ($data) { |
43
|
|
|
return \array_merge($data, ['owner' => $companyName]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
return []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function detectByPattern(array $patternList) |
50
|
|
|
{ |
51
|
|
|
foreach ($patternList as $name => $pattern) { |
52
|
|
|
$pattern = $this->getPattern($pattern); |
53
|
|
|
|
54
|
|
|
if (preg_match($pattern, $this->detector->getUserAgent())) { |
55
|
|
|
return ['name' => $name]; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getPattern(string $pattern): string |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return sprintf('/%s/', $pattern); |
64
|
|
|
} |
65
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.