|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Serhii Nekhaienko <[email protected]> |
|
4
|
|
|
* @license GPL |
|
5
|
|
|
* @copyright Serhii Nekhaienko © 2018 |
|
6
|
|
|
* @version 4.0.2 |
|
7
|
|
|
* @project endorphin-studio/browser-detector |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace EndorphinStudio\Detector\Detection; |
|
11
|
|
|
|
|
12
|
|
|
use EndorphinStudio\Detector\Data\Model; |
|
13
|
|
|
|
|
14
|
|
|
class ModelDetector extends AbstractDetection |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string Key in config (os, device, etc.) |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $configKey = 'model'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Setup result of object |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function setupResultObject() |
|
25
|
|
|
{ |
|
26
|
|
|
$name = $this->additionalInfo['name']; |
|
27
|
|
|
if (!\array_key_exists($name, $this->config)) { |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
$patternList = $this->config[$name]; |
|
31
|
|
|
foreach ($patternList as $series => $data) { |
|
32
|
|
|
if(array_key_exists('pattern', $data)) { |
|
33
|
|
|
$this->detectModelByPattern($series,$data['pattern']); |
|
34
|
|
|
} elseif (array_key_exists('models', $data)) { |
|
35
|
|
|
$this->detectModelByModelList($series, $data['models']); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function detectModelByPattern(string $series, string $pattern) |
|
41
|
|
|
{ |
|
42
|
|
|
$pattern = sprintf('/%s/', $pattern); |
|
43
|
|
|
$result = \preg_match($pattern, $this->detector->getUserAgent(), $model); |
|
44
|
|
|
if ($result) { |
|
45
|
|
|
$model = count($model) > 1 ? sprintf('%s%s', $series, end($model)) : $series; |
|
46
|
|
|
$this->resultObject->setModel($model); |
|
47
|
|
|
$this->resultObject->setSeries($series); |
|
48
|
|
|
return $result; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function detectModelByModelList(string $series, array $data) |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($data as $model => $pattern) { |
|
55
|
|
|
$pattern = sprintf('/%s/', $pattern); |
|
56
|
|
|
$result = \preg_match($pattern, $this->detector->getUserAgent()); |
|
57
|
|
|
if ($result) { |
|
58
|
|
|
$model = sprintf('%s%s', $series, $model); |
|
59
|
|
|
$this->resultObject->setModel($model); |
|
60
|
|
|
$this->resultObject->setSeries($series); |
|
61
|
|
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Detect method |
|
68
|
|
|
* @param array $additional Additional Info |
|
69
|
|
|
*/ |
|
70
|
|
|
public function detect(array $additional = []) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->additionalInfo = $additional; |
|
73
|
|
|
if ($this->configKey !== 'none') { |
|
74
|
|
|
$this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), $this->configKey); |
|
75
|
|
|
$this->resultObject = new Model(); |
|
76
|
|
|
} |
|
77
|
|
|
$this->setupResultObject(); |
|
78
|
|
|
if ($this->resultObject->getModel()) { |
|
|
|
|
|
|
79
|
|
|
$this->detector->getResultObject()->getDevice()->setModel($this->resultObject); |
|
80
|
|
|
$this->detector->getResultObject()->getDevice()->setHasModel(true); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: