Skip to content

Commit 3cb83d7

Browse files
committed
Version 2, I will not stfu ;)
1 parent ee43fb6 commit 3cb83d7

File tree

4 files changed

+81
-26
lines changed

4 files changed

+81
-26
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.markdown

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ Holmes is an easy to use mobile detection library based on php-mobile-detect
1111
## Usage
1212

1313
// Determine if request is from a mobile device
14-
Holmes::is_mobile(); // returns boolean
14+
Holmes\Holmes::is_mobile(); // returns boolean
1515

1616
// Determine the type of device
17-
$device = Holmes::get_device(); // returns string (or default)
17+
$device = Holmes\Holmes::get_device(); // returns string (or default)
1818

19-
// Holmes::get_device() will throw a DeviceNotDetectedException exception if no default is passed
19+
// Holmes\Holmes::get_device() will throw a DeviceNotDetectedException exception if no default is passed
2020
// and could not detect a mobile device. Passing a default will return the default in lieu of an
2121
// exception
2222

2323
// Determine if a specific device is being used
2424
// Any supported device below is acceptable
2525
// lowercased no spaces, obviously. <3
26-
Holmes::is_ipad();
27-
Holmes::is_blackberrytablet();
26+
Holmes\Holmes::is_ipad();
27+
Holmes\Holmes::is_blackberrytablet();
2828

2929
// Modern (Android/iPad) tablet
30-
Holmes::is_tablet();
30+
Holmes\Holmes::is_tablet();
3131
## Supported Device Types
3232

3333
* Android

composer.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "holmes/holmes",
3+
"description": "Mobile device detection",
4+
"minimum-stability": "dev",
5+
"require": {
6+
"php": ">=5.3.3"
7+
},
8+
"autoload": {
9+
"classmap": ["Holmes.php"]
10+
}
11+
}

holmes.php

+63-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

3-
class InvalidMethodException extends Exception {}
4-
class DeviceNotDetectedException extends Exception {}
3+
namespace Holmes;
4+
5+
use BadMethodCallException;
6+
7+
class DeviceNotDetectedException extends \Exception {}
58

69
/**
710
* Holmes
@@ -10,10 +13,12 @@ class DeviceNotDetectedException extends Exception {}
1013
*/
1114
class Holmes
1215
{
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(
1520
"android" => "android.*mobile",
16-
"androidtablet" => "android' + 'chrome/[.0-9]* (?!mobile)",
21+
"androidtablet" => "android' + 'chrome\/[.0-9]* (?!mobile)",
1722
"blackberry" => "blackberry",
1823
"blackberrytablet" => "rim tablet os",
1924
"iphone" => "(iphone|ipod)",
@@ -27,25 +32,44 @@ class Holmes
2732
"generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)"
2833
);
2934

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+
*/
3042
public static function __callStatic($name, $arguments)
3143
{
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))
3448
{
35-
return self::is_device($device);
49+
return static::isDevice($device);
3650
}
3751
else
3852
{
39-
throw new InvalidMethodException('Invalid method called.');
53+
throw new BadMethodCallException('Invalid method called.');
4054
}
4155
}
4256

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()
4463
{
45-
return static::is_androidtablet() || static::is_ipad();
64+
return static::isAndroidtablet() || static::isIpad();
4665
}
4766

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()
4973
{
5074
$accept = $_SERVER['HTTP_ACCEPT'];
5175

@@ -57,21 +81,33 @@ public static function is_mobile()
5781
{
5882
return true;
5983
}
60-
else
84+
85+
foreach (array_keys(static::$devices) as $device)
6186
{
62-
foreach (array_keys(self::$devices) as $device)
87+
if (static::isDevice($device))
6388
{
64-
if (self::is_device($device)) return true;
89+
return true;
6590
}
6691
}
92+
6793
return false;
6894
}
6995

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)
71104
{
72-
foreach (array_keys(self::$devices) as $device)
105+
foreach (array_keys(static::$devices) as $device)
73106
{
74-
if (self::is_device($device)) return $device;
107+
if (static::isDevice($device))
108+
{
109+
return $device;
110+
}
75111
}
76112

77113
if ($default === false)
@@ -82,9 +118,16 @@ public static function get_device($default = false)
82118
return $default;
83119
}
84120

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)
86128
{
87129
$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);
89132
}
90133
}

0 commit comments

Comments
 (0)