class MobileDeviceDetectionObject in Mobile Device Detection 8
Hierarchy
- class \Drupal\mobile_device_detection\Object\MobileDeviceDetectionObject
Expanded class hierarchy of MobileDeviceDetectionObject
1 file declares its use of MobileDeviceDetectionObject
- MobileDeviceDetection.php in src/
Object/ MobileDeviceDetection.php
File
- src/
Object/ MobileDeviceDetectionObject.php, line 6
Namespace
Drupal\mobile_device_detection\ObjectView source
class MobileDeviceDetectionObject {
/**
* MobileDeviceDetectionAttributes
*
* @var array
*/
private $attributes;
/**
* The MobileDeviceDetectionObject mobile_headers
*
* @var array
*/
private $mobile_headers;
/**
* The MobileDeviceDetectionObject cloud_headers
*
* @var array
*/
private $cloud_headers;
/**
* The MobileDeviceDetectionObject user_agent_headers
*
* @var array
*/
private $user_agent_headers;
/**
* The MobileDeviceDetectionObject object
*
* @var object
*/
private $object;
/**
* The constructoror
*/
public function __construct(MobileDeviceDetectionAttributes $attributes) {
$this
->setAttributes($attributes);
$this
->init();
}
/**
* {@inheritdoc}
*/
private function init() {
$this
->setMobileHeaders(\Drupal::request()->server
->all());
$this
->setCloudHeaders(\Drupal::request()->server
->all());
$this
->setUserAgentHeaders($this
->getAttributes()
->get('user_agent_headers'));
$this->object->type = null;
if ($this
->check('mobile')) {
$this->object->type = 'mobile';
}
if ($this
->check('tablet')) {
$this->object->type = 'tablet';
}
}
/**
* {@inheritdoc}
*/
public function getObject() {
if (isset($this->object->type)) {
$this
->getOperatingSystem();
$this
->getBrowser();
return $this->object;
}
}
/**
* {@inheritdoc}
*/
public function isMobile() {
return $this->object->type === 'mobile' ? true : false;
}
/**
* {@inheritdoc}
*/
public function isTablet() {
return $this->object->type === 'tablet' ? true : false;
}
/**
* {@inheritdoc}
*/
protected function setAttributes(MobileDeviceDetectionAttributes $attributes) {
$this->attributes = $attributes;
}
/**
* {@inheritdoc}
*/
protected function getAttributes() {
return $this->attributes;
}
/**
* {@inheritdoc}
*/
protected function setMobileHeaders($headers) {
array_walk($headers, function (&$v, $k) {
if (substr($k, 0, 5) === 'HTTP_') {
$this->mobile_headers[$k] = $v;
}
});
}
/**
* {@inheritdoc}
*/
protected function getMobileHeaders() {
return $this->mobile_headers;
}
/**
* {@inheritdoc}
*/
protected function setCloudHeaders($headers) {
array_walk($headers, function (&$v, $k) {
if (substr(strtolower($k), 0, 16) === 'http_cloudfront_') {
$this->cloud_headers[strtoupper($k)] = $v;
}
});
}
/**
* {@inheritdoc}
*/
protected function getCloudHeaders() {
return $this->cloud_headers;
}
/**
* {@inheritdoc}
*/
protected function setUserAgentHeaders($headers) {
$this->user_agent_headers = join(' ', array_intersect_key($this
->getMobileHeaders(), array_flip($headers)));
if (!$this->user_agent_headers && !empty($this
->getCloudHeaders())) {
$this->user_agent_headers = 'Amazon CloudFront';
}
}
/**
* {@inheritdoc}
*/
protected function getUserAgentHeaders() {
return $this->user_agent_headers;
}
/**
* {@inheritdoc}
*/
private function check($type) {
if ($this
->getUserAgentHeaders() === 'Amazon CloudFront') {
$headers = $this
->setCloudHeaders();
if (array_key_exists('HTTP_CLOUDFRONT_IS_MOBILE_VIEWER', $headers) && $headers['HTTP_CLOUDFRONT_IS_MOBILE_VIEWER'] === 'true') {
return true;
}
}
switch ($type) {
case 'mobile':
$options = array_merge($this
->getAttributes()
->get('phone_devices'));
$headers = array_intersect_key($this
->getAttributes()
->get('mobile_headers'), $this
->getMobileHeaders());
foreach ($headers as $key => $value) {
foreach ($value as $v) {
if (strpos($this
->getMobileHeaders()[$key], $v) !== false) {
return true;
}
}
}
goto device_detect;
break;
case 'tablet':
$options = array_merge($this
->getAttributes()
->get('tablet_devices'));
case 'deviceDetect':
device_detect:
foreach ($options as $value) {
if (!empty($value)) {
if ($this
->_match($value)) {
return true;
}
}
}
break;
}
return false;
}
/**
* {@inheritdoc}
*/
private function getOperatingSystem() {
$this->object->OS = $this
->_get($this
->getAttributes()
->get('operating_systems'));
$this
->_version($this->object->OS);
}
/**
* {@inheritdoc}
*/
private function getBrowser() {
$this->object->browser = $this
->_get($this
->getAttributes()
->get('browsers'));
$this
->_version($this->object->browser);
}
/**
* {@inheritdoc}
*/
private function _get($options) {
foreach ($options as $key => $value) {
if (!empty($value)) {
if ($this
->_match($value)) {
return $key;
}
}
}
}
/**
* {@inheritdoc}
*/
private function _match($value) {
return (bool) preg_match(sprintf('#%s#is', $value), $this
->getUserAgentHeaders(), $matches);
}
/**
* {@inheritdoc}
*/
private function _version($name) {
$properties = (array) $this
->getAttributes()
->get('properties')[$name];
foreach ($properties as $value) {
$pattern = str_replace('[VER]', $this
->getAttributes()
->get('VER'), $value);
preg_match(sprintf('#%s#is', $pattern), $this
->getUserAgentHeaders(), $matches);
if (!empty($matches)) {
$this->object->{$name}[] = $matches;
}
}
}
}