|
1
|
|
|
<?php |
|
2
|
|
|
namespace UserAgentParser\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use UserAgentParser\Exception; |
|
5
|
|
|
use UserAgentParser\Model; |
|
6
|
|
|
|
|
7
|
|
|
class DonatjUAParser extends AbstractProvider |
|
8
|
|
|
{ |
|
9
|
1 |
|
public function getName() |
|
10
|
|
|
{ |
|
11
|
1 |
|
return 'DonatjUAParser'; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
2 |
|
public function getComposerPackageName() |
|
15
|
|
|
{ |
|
16
|
2 |
|
return 'donatj/phpuseragentparser'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $resultRaw |
|
22
|
|
|
* |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
2 |
|
private function hasResult(array $resultRaw) |
|
26
|
|
|
{ |
|
27
|
2 |
|
if ($this->isRealResult($resultRaw['browser'])) { |
|
28
|
1 |
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @param Model\Browser $browser |
|
37
|
|
|
* @param array $resultRaw |
|
38
|
|
|
*/ |
|
39
|
1 |
|
private function hydrateBrowser(Model\Browser $browser, array $resultRaw) |
|
40
|
|
|
{ |
|
41
|
1 |
|
if ($this->isRealResult($resultRaw['browser']) === true) { |
|
42
|
1 |
|
$browser->setName($resultRaw['browser']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
if ($this->isRealResult($resultRaw['version']) === true) { |
|
46
|
1 |
|
$browser->getVersion()->setComplete($resultRaw['version']); |
|
47
|
|
|
} |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function parse($userAgent, array $headers = []) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$resultRaw = parse_user_agent($userAgent); |
|
53
|
|
|
|
|
54
|
2 |
|
if ($this->hasResult($resultRaw) !== true) { |
|
55
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
* Hydrate the model |
|
60
|
|
|
*/ |
|
61
|
1 |
|
$result = new Model\UserAgent(); |
|
62
|
1 |
|
$result->setProviderResultRaw($resultRaw); |
|
63
|
|
|
|
|
64
|
|
|
/* |
|
65
|
|
|
* Bot detection - is currently not possible! |
|
66
|
|
|
*/ |
|
67
|
|
|
|
|
68
|
|
|
/* |
|
69
|
|
|
* Browser |
|
70
|
|
|
*/ |
|
71
|
1 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
|
72
|
|
|
|
|
73
|
|
|
/* |
|
74
|
|
|
* operatingSystem |
|
75
|
|
|
* |
|
76
|
|
|
* @todo $resultRaw['platform'] has sometimes informations about the OS or the device |
|
77
|
|
|
* ... maybe split it or how do that? |
|
78
|
|
|
*/ |
|
79
|
|
|
|
|
80
|
1 |
|
return $result; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.