I'd like to suggest that these methods be added to the project
public function getUseragent() {
return $this->userAgent;
}
Sometimes it's useful to be able to pull back exactly which useragent was matched, this is because of the proxy / spoofing that goes on. At the moment the userAgent property is protected so it needs a getter.
I'm working on a project where I need to quickly determine information about a device. I've worked on much bigger projects like this in the past collating information from either the WURFL or Device Atlas (depending on what the organisation was using) to then group handsets together. So a similar aim to the roadmap for this project. This new project has no existing device detection library, have no money to pay for Device Atlas and the new wURFL license means it's not feasible for them to use as a source, hence why I have been looking at Mobile-Detect.
In order to achieve my aims, I've written a couple of quick methods to help me pull back information, on the whole they wrap a central protected method which does the guts of the work
/**
* getPropetyDetail()
* Returns the value of the property of the identified device
* @param string $method
* @return string
* @throws \UnexpectedValueException
*/
protected function getPropetyDetail( $method) {
if ( ! method_exists($this, $method)) {
throw new UnexpectedValueException( 'Method name ' . $method .' was not expected' );
}
foreach($this->$method() as $name => $regex) {
$check = $this->{'is'.$name}();
if ($check) return $name;
}
return '';
}
/**
* getBrand()
* Returns the name of the "brand"
* @return string
* @throws \UnexpectedValueException
*/
public function getBrand() {
return $this->getPropetyDetail('getRules');
}
/**
* getOs()
* Returns the name of the "OS"
* @return string
* @throws \UnexpectedValueException
*/
public function getOs() {
return $this->getPropetyDetail('getOperatingSystems');
}
/**
* getTabletMake()
* Returns the make of the tablet
* @return string
* @throws \UnexpectedValueException
*/
public function getTabletMake() {
return $this->getPropetyDetail('getTabletDevices');
}
/**
* getPhoneMake()
* Returns the make of the phone
* @return string
* @throws \UnexpectedValueException
*/
public function getPhoneMake() {
return $this->getPropetyDetail('getPhoneDevices');
}
I've also created one to return version information
/**
* getVersion
* Returns a stdClass with properties of each version which has been detected
* @return stdClass
*/
public function getVersion() {
$return = new stdClass();
foreach($this->getProperties() as $name => $regex) {
$check = $this->version($name);
if ($check) $return->$name = $check;
}
return $return;
}
Finally, I put together another method to wrap these and return everything in one object
public function getDetails() {
$return = new stdClass();
$return->type = 'unknown';
$return->useragent = $this->getUseragent();
// Work out the device details
$return->isBrand = $this->getBrand();
$return->isMobile = $this->isMobile();
$return->isTablet = $this->isTablet();
// Get the names
$return->mobile = $this->getPhoneMake();
$return->tablet = $this->gettabletMake();
// General
$return->os = $this->getOs();
// Work out the version of the OS. Due to the regex used, tweaks need to be made
$return->versions = $this->getVersion();
if (isset($return->versions->{$return->isBrand}) ) {
// Works for iPhone and Blackberry
$return->version = $return->versions->{$return->isBrand};
} elseif ( 'WindowsPhoneOS' == $return->os && isset($return->versions->{'Windows Phone OS'}) ) {
// Windows
$return->version = $return->versions->{'Windows Phone OS'};
} elseif ('AndroidOS' == $return->os && isset($return->versions->Android)) {
// Android
$return->version = $return->versions->Android;
} elseif ('SymbianOS' == $return->os && isset($return->versions->Symbian)) {
// Symbian
$return->version = $return->versions->Symbian;
} elseif (isset($return->versions->Mobile) ) {
// A fallback not really good enough
$return->version = $return->versions->Mobile;
} else {
$return->version = 0;
}
// Determine type of device
if ($return->isTablet) {
$return->type = 'tablet';
} elseif ($return->isMobile) {
$return->type = 'mobile';
} elseif ($return->isBrand) {
$return->type = 'isBrand';
}
return $return;
}
This results in the following output
stdClass::__set_state(array(
'type' => 'mobile',
'useragent' => 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; SGH-i677)',
'isBrand' => 'Samsung',
'isMobile' => true,
'isTablet' => false,
'mobile' => 'Samsung',
'tablet' => '',
'os' => 'WindowsPhoneOS',
'versions' =>
stdClass::__set_state(array(
'Mobile' => 9,
'IEMobile' => 9,
'MSIE' => 9,
'Trident' => 5,
'Windows Phone OS' => 7.5,
)),
'version' => 7.5,
))