1 | <?php |
||
12 | class Detector |
||
13 | { |
||
14 | /** @var array Xml Data */ |
||
15 | public static $xmlData; |
||
16 | |||
17 | private static function init($pathToData = 'auto') |
||
34 | |||
35 | public static function analyse($uaString='UA') |
||
|
|||
36 | { |
||
37 | $ua = $uaString; |
||
38 | if($uaString == 'UA') |
||
39 | { |
||
40 | $ua = $_SERVER['HTTP_USER_AGENT']; |
||
41 | } |
||
42 | |||
43 | Detector::init(); |
||
44 | $xml = Detector::$xmlData; |
||
45 | |||
46 | $detectorResult = new DetectorResult(); |
||
47 | $detectorResult->uaString = $ua; |
||
48 | $ns = '\\EndorphinStudio\\Detector\\'; |
||
49 | |||
50 | foreach($xml as $key => $item) |
||
51 | { |
||
52 | $data = self::analysePart($xml,$key,$ua); |
||
53 | $classname = $ns.$key; |
||
54 | if($data !== null) |
||
55 | { |
||
56 | $object = new $classname($data); |
||
57 | if($key == 'OS' || $key == 'Browser') |
||
58 | { |
||
59 | $object->setVersion(self::getVersion($data, $ua)); |
||
60 | } |
||
61 | } |
||
62 | else |
||
63 | { |
||
64 | $object = $classname::initEmpty(); |
||
65 | } |
||
66 | $detectorResult->$key = $object; |
||
67 | } |
||
68 | |||
69 | $detectorResult = self::checkRules($detectorResult); |
||
70 | |||
71 | return $detectorResult; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param array $xmlData Xml data array |
||
76 | * @param string $key Key in data array |
||
77 | * @param string $uaString User agent |
||
78 | * @return \SimpleXMLElement xml element |
||
79 | */ |
||
80 | private static function analysePart($xmlData,$key,$uaString) |
||
93 | |||
94 | /** |
||
95 | * @param \SimpleXMLElement $xmlItem xmlItem |
||
96 | * @param string $uaString User agent |
||
97 | * @return string Version |
||
98 | */ |
||
99 | private static function getVersion(\SimpleXMLElement $xmlItem,$uaString) |
||
130 | |||
131 | /** |
||
132 | * @param $version Windows number version |
||
133 | * @return string Windows version |
||
134 | */ |
||
135 | private static function getWindowsVersion($version) |
||
160 | |||
161 | /** |
||
162 | * @param DetectorResult $result Detector result |
||
163 | * @return DetectorResult Final result |
||
164 | */ |
||
165 | private static function checkRules(DetectorResult $result) |
||
185 | } |
||
186 | define('D_NA','N\A'); |
||
187 |
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: