Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

DeviceDetector Feature: Based on structure of Chrome for Android UA #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Device
const IPAD = 'iPad';
const IPHONE = 'iPhone';
const WINDOWS_PHONE = 'Windows Phone';
const ANDROID_PHONE = 'Android Phone';

/**
* @var string
Expand Down
32 changes: 28 additions & 4 deletions src/DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static function detect(Device $device, UserAgent $userAgent)
self::checkIpad($device, $userAgent) ||
self::checkIphone($device, $userAgent) ||
self::checkWindowsPhone($device, $userAgent) ||
self::checkSamsungPhone($device, $userAgent)
self::checkSamsungPhone($device, $userAgent) ||
self::checkAndroidPhone($device, $userAgent)
);
}

Expand Down Expand Up @@ -79,16 +80,39 @@ private static function checkWindowsPhone(Device $device, UserAgent $userAgent)
}

/**
* Determine if the device is Windows Phone.
* Determine if the device is Samsung Phone.
*
* @param Device $device
* @param UserAgent $userAgent
* @return bool
*/
private static function checkSamsungPhone(Device $device, UserAgent $userAgent)
{
if (preg_match('/SAMSUNG SM-([^ ]*)/i', $userAgent->getUserAgentString(), $matches)) {
$device->setName(str_ireplace('SAMSUNG', 'Samsung', $matches[0]));
if (preg_match('/SAMSUNG SM-([^ ]*)/i', $userAgent->getUserAgentString(), $matches)) {
$device->setName(str_ireplace('SAMSUNG', 'Samsung', $matches[0]));
return true;
}

return false;
}


/**
* Determine if the device is an Android Phone if Chrome is in use, and returns the Model-Code in $device setName
*
* @param Device $device
* @param UserAgent $userAgent
* @return bool
*/
private static function checkAndroidPhone(Device $device, UserAgent $userAgent)
{
if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
if (preg_match('/Linux; (Android [0-9\.]*); (?<modell>.*)\) AppleWebKit/', $userAgent->getUserAgentString(), $matches)) {
$device->setName("Android_".preg_replace('/ Build.*$/', '', $matches["modell"]));
return true;
}

$device->setName($device::ANDROID_PHONE);
return true;
}
return false;
Expand Down
Loading