1 | <?php |
||
12 | class Detector |
||
13 | { |
||
14 | /** @var array Xml Data */ |
||
15 | private static $_xmlData; |
||
16 | public static $isInitialized = false; |
||
17 | |||
18 | private static function initialize($pathToData = 'auto') |
||
36 | |||
37 | public static function analyse($uaString='UA') |
||
|
|||
38 | { |
||
39 | $ua = $uaString; |
||
40 | if($uaString == 'UA') |
||
41 | { |
||
42 | $ua = $_SERVER['HTTP_USER_AGENT']; |
||
43 | } |
||
44 | |||
45 | if(!self::$isInitialized) |
||
46 | self::initialize(); |
||
47 | $xml = self::$_xmlData; |
||
48 | |||
49 | $detectorResult = new DetectorResult(); |
||
50 | $detectorResult->uaString = $ua; |
||
51 | $ns = '\\EndorphinStudio\\Detector\\'; |
||
52 | |||
53 | foreach($xml as $key => $item) |
||
54 | { |
||
55 | $data = self::analysePart($xml,$key,$ua); |
||
56 | $classname = $ns.$key; |
||
57 | if($data !== null) |
||
58 | { |
||
59 | $object = new $classname($data); |
||
60 | if($key == 'OS' || $key == 'Browser') |
||
61 | { |
||
62 | $object->setVersion(self::getVersion($data, $ua)); |
||
63 | } |
||
64 | if($key == 'Robot') |
||
65 | { |
||
66 | if($object->getName() != D_NA) |
||
67 | { |
||
68 | $detectorResult->isBot = true; |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | else |
||
73 | { |
||
74 | $object = $classname::initEmpty(); |
||
75 | } |
||
76 | $detectorResult->$key = $object; |
||
77 | } |
||
78 | |||
79 | $detectorResult = self::checkRules($detectorResult); |
||
80 | $detectorResult = self::checkModelName($detectorResult); |
||
81 | |||
82 | return $detectorResult; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param array $xmlData Xml data array |
||
87 | * @param string $key Key in data array |
||
88 | * @param string $uaString User agent |
||
89 | * @return \SimpleXMLElement xml element |
||
90 | */ |
||
91 | private static function analysePart($xmlData,$key,$uaString) |
||
104 | |||
105 | /** |
||
106 | * @param \SimpleXMLElement $xmlItem xmlItem |
||
107 | * @param string $uaString User agent |
||
108 | * @return string Version |
||
109 | */ |
||
110 | private static function getVersion(\SimpleXMLElement $xmlItem,$uaString) |
||
141 | |||
142 | /** |
||
143 | * @param $version Windows number version |
||
144 | * @return string Windows version |
||
145 | */ |
||
146 | private static function getWindowsVersion($version) |
||
171 | |||
172 | /** |
||
173 | * @param DetectorResult $result Detector result |
||
174 | * @return DetectorResult Final result |
||
175 | */ |
||
176 | private static function checkRules(DetectorResult $result) |
||
177 | { |
||
178 | $Rules = DetectorRule::loadRulesFromFile(); |
||
179 | foreach($Rules as $rule) |
||
180 | { |
||
181 | $objectType = $rule->getObjectType(); |
||
182 | $objectProperty = $rule->getObjectProperty(); |
||
183 | $targetType = $rule->getTargetType(); |
||
184 | $targetValue = $rule->isTargetValue(); |
||
185 | $func = 'get'.$objectProperty; |
||
186 | if($result->$objectType !== null) |
||
187 | { |
||
188 | if ($result->$objectType->$func() == $rule->getObjectPropertyValue()) { |
||
189 | $result->$targetType = $targetValue; |
||
190 | break; |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | return $result; |
||
195 | } |
||
196 | |||
197 | private static function checkModelName(DetectorResult $result) |
||
211 | } |
||
212 | define('D_NA','N\A'); |
||
213 |
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: