1
0
Fork 0
mirror of https://github.com/Findus23/devicedetector.net.git synced 2024-09-19 15:43:46 +02:00
devicedetector.net/src/IconPath.php

126 lines
3.5 KiB
PHP
Raw Normal View History

2019-04-09 20:36:39 +02:00
<?php
namespace DeviceDetectorNet;
use DeviceDetector\DeviceDetector;
2019-04-10 12:12:22 +02:00
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\OperatingSystem;
2019-04-09 20:36:39 +02:00
class IconPath {
private $dd;
2019-04-10 12:12:22 +02:00
private $iconDir = __DIR__ . "/../public";
2019-04-09 20:36:39 +02:00
public function __construct(DeviceDetector $dd) {
$this->dd = $dd;
}
2019-04-10 12:12:22 +02:00
/**
* modified from plugins/DevicesDetection/functions.php:17
* @return string
*/
public function getBrandLogo() {
$label = $this->dd->getBrandName();
$path = '/icons/brand/%s.png';
$label = preg_replace("/[^a-z0-9_-]+/i", "_", $label);
if (!file_exists($this->iconDir . sprintf($path, $label))) {
return null;
}
return sprintf($path, $label);
}
/**
* from plugins/DevicesDetection/functions.php:27
* @param $label
* @return string
* @throws \Exception
*/
private function getBrowserFamilyFullName($label) {
foreach (Browser::getAvailableBrowserFamilies() as $name => $family) {
if (in_array($label, $family)) {
return $name;
}
}
2019-04-12 19:43:24 +02:00
return false;
2019-04-10 12:12:22 +02:00
}
/**
* modified from from plugins/DevicesDetection/functions.php:80
* @return string
* @throws \Exception
*/
public function getBrowserLogo() {
2019-04-10 20:24:12 +02:00
$client = $this->dd->getClient();
2019-04-12 19:43:24 +02:00
if (empty($client["short_name"])) {
2019-04-10 20:24:12 +02:00
return null;
}
2019-04-10 12:12:22 +02:00
$short = $this->dd->getClient()["short_name"];
$path = '/icons/browsers/%s.png';
$family = $this->getBrowserFamilyFullName($short);
$browserFamilies = Browser::getAvailableBrowserFamilies();
if (!empty($short) &&
array_key_exists($short, Browser::getAvailableBrowsers()) &&
file_exists($this->iconDir . sprintf($path, $short))) {
return sprintf($path, $short);
} elseif (!empty($short) &&
array_key_exists($family, $browserFamilies) &&
file_exists($this->iconDir . sprintf($path, $browserFamilies[$family][0]))) {
return sprintf($path, $browserFamilies[$family][0]);
}
return null;
}
/**
* modified from plugins/DevicesDetection/functions.php:153
* @return string
*/
public function getDeviceTypeLogo() {
$label = $this->dd->getDeviceName();
2019-04-10 20:24:12 +02:00
if (empty($label)) {
return null;
}
2019-04-10 12:12:22 +02:00
$label = strtolower($label);
$label = str_replace(' ', '_', $label);
$path = '/icons/devices/' . $label . ".png";
return $path;
}
/**
* modified from plugins/DevicesDetection/functions.php:282
* @return string
*/
function getOsLogo() {
$path = '/icons/os/%s.png';
2019-04-12 19:43:24 +02:00
$client = $this->dd->getOs();
if (empty($client["short_name"])) {
2019-04-10 20:24:12 +02:00
return null;
}
$short = $client["short_name"];
2019-04-10 12:12:22 +02:00
$family = OperatingSystem::getOsFamily($short);
$osFamilies = OperatingSystem::getAvailableOperatingSystemFamilies();
// print_r(sprintf($path, $short));
if (!empty($short) &&
array_key_exists($short, OperatingSystem::getAvailableOperatingSystems()) &&
file_exists($this->iconDir . sprintf($path, $short))) {
return sprintf($path, $short);
} elseif (!empty($family) &&
array_key_exists($family, $osFamilies) &&
file_exists($this->iconDir . sprintf($path, $osFamilies[$family][0]))) {
return sprintf($path, $osFamilies[$family][0]);
}
return null;
}
}