1
1
<?php
2
2
3
- class InvalidMethodException extends Exception {}
4
- class DeviceNotDetectedException extends Exception {}
3
+ namespace Holmes ;
4
+
5
+ use BadMethodCallException ;
6
+
7
+ class DeviceNotDetectedException extends \Exception {}
5
8
6
9
/**
7
10
* Holmes
@@ -10,10 +13,12 @@ class DeviceNotDetectedException extends Exception {}
10
13
*/
11
14
class Holmes
12
15
{
13
- // regex and patterns from php-mobile-detect
14
- private static $ devices = array (
16
+ /**
17
+ * @var array $devices regex and patterns from php-mobile-detect
18
+ */
19
+ protected static $ devices = array (
15
20
"android " => "android.*mobile " ,
16
- "androidtablet " => "android' + 'chrome/[.0-9]* (?!mobile) " ,
21
+ "androidtablet " => "android' + 'chrome\ /[.0-9]* (?!mobile) " ,
17
22
"blackberry " => "blackberry " ,
18
23
"blackberrytablet " => "rim tablet os " ,
19
24
"iphone " => "(iphone|ipod) " ,
@@ -27,25 +32,44 @@ class Holmes
27
32
"generic " => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini) "
28
33
);
29
34
35
+ /**
36
+ * Magic tester method.
37
+ *
38
+ * @param string $name method name
39
+ * @param array $arguments method arguments
40
+ * @return boolean static::isDevice result
41
+ */
30
42
public static function __callStatic ($ name , $ arguments )
31
43
{
32
- $ device = array_pop (explode ('_ ' , $ name ));
33
- if (array_key_exists ($ device , self ::$ devices ))
44
+ $ parts = preg_split ('/(?=[A-Z])/ ' , $ name );
45
+ $ device = strtolower (array_pop ($ parts ));
46
+
47
+ if (array_key_exists ($ device , static ::$ devices ))
34
48
{
35
- return self :: is_device ($ device );
49
+ return static :: isDevice ($ device );
36
50
}
37
51
else
38
52
{
39
- throw new InvalidMethodException ('Invalid method called. ' );
53
+ throw new BadMethodCallException ('Invalid method called. ' );
40
54
}
41
55
}
42
56
43
- public static function is_tablet ()
57
+ /**
58
+ * Detect wether it's a tablet device
59
+ *
60
+ * @return boolean wether it's a tablet device
61
+ */
62
+ public static function isTablet ()
44
63
{
45
- return static ::is_androidtablet () || static ::is_ipad ();
64
+ return static ::isAndroidtablet () || static ::isIpad ();
46
65
}
47
66
48
- public static function is_mobile ()
67
+ /**
68
+ * Detect wether it's a mobile device
69
+ *
70
+ * @return boolean wether it's a mobile device
71
+ */
72
+ public static function isMobile ()
49
73
{
50
74
$ accept = $ _SERVER ['HTTP_ACCEPT ' ];
51
75
@@ -57,21 +81,33 @@ public static function is_mobile()
57
81
{
58
82
return true ;
59
83
}
60
- else
84
+
85
+ foreach (array_keys (static ::$ devices ) as $ device )
61
86
{
62
- foreach ( array_keys ( self :: $ devices ) as $ device )
87
+ if ( static :: isDevice ( $ device) )
63
88
{
64
- if ( self :: is_device ( $ device )) return true ;
89
+ return true ;
65
90
}
66
91
}
92
+
67
93
return false ;
68
94
}
69
95
70
- public static function get_device ($ default = false )
96
+ /**
97
+ * Retrieve the device type
98
+ *
99
+ * @param boolean $default default device
100
+ * @return string device name
101
+ * @throws Holmes\DeviceNotDetectedException
102
+ */
103
+ public static function getDevice ($ default = false )
71
104
{
72
- foreach (array_keys (self ::$ devices ) as $ device )
105
+ foreach (array_keys (static ::$ devices ) as $ device )
73
106
{
74
- if (self ::is_device ($ device )) return $ device ;
107
+ if (static ::isDevice ($ device ))
108
+ {
109
+ return $ device ;
110
+ }
75
111
}
76
112
77
113
if ($ default === false )
@@ -82,9 +118,16 @@ public static function get_device($default = false)
82
118
return $ default ;
83
119
}
84
120
85
- protected static function is_device ($ device )
121
+ /**
122
+ * Detect wether it's a certaim device
123
+ *
124
+ * @param string $device device name
125
+ * @return boolean wether it's a device
126
+ */
127
+ protected static function isDevice ($ device )
86
128
{
87
129
$ ua = $ _SERVER ['HTTP_USER_AGENT ' ];
88
- return (bool )preg_match ("/ " . self ::$ devices [$ device ] . "/i " , $ ua );
130
+
131
+ return (bool ) preg_match ('/ ' . static ::$ devices [$ device ] . '/i ' , $ ua );
89
132
}
90
133
}
0 commit comments