1 | <?php namespace Arcanedev\LaravelTracker\Parsers; |
||
11 | class UserAgentParser implements UserAgentParserContract |
||
12 | { |
||
13 | /* ----------------------------------------------------------------- |
||
14 | | Properties |
||
15 | | ----------------------------------------------------------------- |
||
16 | */ |
||
17 | |||
18 | /** @var \UAParser\Parser */ |
||
19 | protected $parser; |
||
20 | |||
21 | /** @var string */ |
||
22 | public $basePath; |
||
23 | |||
24 | /* ----------------------------------------------------------------- |
||
25 | | Constructor |
||
26 | | ----------------------------------------------------------------- |
||
27 | */ |
||
28 | |||
29 | /** |
||
30 | * UserAgentParser constructor. |
||
31 | * |
||
32 | * @param \UAParser\Parser $parser |
||
33 | * @param string $basePath |
||
34 | * @param null $userAgent |
||
35 | */ |
||
36 | 15 | public function __construct($parser, $basePath, $userAgent = null) |
|
|
|||
37 | { |
||
38 | 15 | if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) { |
|
39 | $userAgent = $_SERVER['HTTP_USER_AGENT']; |
||
40 | } |
||
41 | |||
42 | 15 | $this->parser = $parser->parse($userAgent); |
|
43 | 15 | $this->basePath = $basePath; |
|
44 | 15 | } |
|
45 | |||
46 | /* ----------------------------------------------------------------- |
||
47 | | Main Methods |
||
48 | | ----------------------------------------------------------------- |
||
49 | */ |
||
50 | |||
51 | /** |
||
52 | * Get the OS version. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 9 | public function getOperatingSystemVersion() |
|
62 | |||
63 | /** |
||
64 | * Get the OS Family. |
||
65 | * |
||
66 | * @return string|null |
||
67 | */ |
||
68 | 9 | public function getOperatingSystemFamily() |
|
77 | |||
78 | /** |
||
79 | * Get the browser. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 9 | public function getBrowser() |
|
87 | |||
88 | /** |
||
89 | * Get the user agent version. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 9 | public function getUserAgentVersion() |
|
99 | |||
100 | /** |
||
101 | * Get the original user agent. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 9 | public function getOriginalUserAgent() |
|
109 | |||
110 | /** |
||
111 | * Get the user agent. |
||
112 | * |
||
113 | * @return \UAParser\Result\UserAgent |
||
114 | */ |
||
115 | 9 | public function getUserAgent() |
|
119 | |||
120 | /** |
||
121 | * Get the operating system. |
||
122 | * |
||
123 | * @return \UAParser\Result\OperatingSystem |
||
124 | */ |
||
125 | 9 | public function getOperatingSystem() |
|
129 | |||
130 | /** |
||
131 | * Get the device. |
||
132 | * |
||
133 | * @return \UAParser\Result\Device |
||
134 | */ |
||
135 | public function getDevice() |
||
139 | |||
140 | /* ----------------------------------------------------------------- |
||
141 | | Other Methods |
||
142 | | ----------------------------------------------------------------- |
||
143 | */ |
||
144 | |||
145 | /** |
||
146 | * Prepare the version. |
||
147 | * |
||
148 | * @param array $version |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 12 | protected function prepareVersion(array $version) |
|
156 | } |
||
157 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: