Skip to content

Commit

Permalink
Merge pull request #5 from NielsLeenheer/improve-whichbrowser
Browse files Browse the repository at this point in the history
Improve WhichBrowser adapter
  • Loading branch information
ThaDafinser committed Nov 8, 2015
2 parents dc3c472 + ceb918a commit 19b235e
Showing 1 changed file with 70 additions and 22 deletions.
92 changes: 70 additions & 22 deletions src/Provider/WhichBrowser.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace UserAgentParser\Provider;

// include 'vendor/whichbrowser/whichbrowser/libraries/whichbrowser.php';
include 'vendor/whichbrowser/whichbrowser/libraries/whichbrowser.php';

class WhichBrowser extends AbstractProvider
Expand All @@ -17,24 +16,9 @@ public function getName()
*
* @return \WhichBrowser
*/
private function getParser()
{
if ($this->parser !== null) {
return $this->parser;
}

$parser = new \WhichBrowser([]);

$this->parser = $parser;

return $this->parser;
}

public function parse($userAgent)
{
$parser = $this->getParser();
$parser->analyseUserAgent($userAgent);

$parser = new \WhichBrowser(array('headers' => array('User-Agent' => $userAgent)));
$raw = $parser->toArray();

/*
Expand All @@ -50,21 +34,70 @@ public function parse($userAgent)
if (isset($raw['browser']['name'])) {
$browserFamily = $raw['browser']['name'];
}

if (isset($raw['browser']['alias'])) {
$browserFamily = $raw['browser']['alias'];
}

$browserVersion = null;
if (isset($raw['browser']['version'])) {
$browserVersion = $raw['browser']['version'];
if(is_array($raw['browser']['version'])) {
if(isset($raw['browser']['version']['alias'])){
$browserVersion = $raw['browser']['version']['alias'];
} else {
$browserVersion = $raw['browser']['version']['value'];
}

if(isset($raw['browser']['version']['nickname'])){
$osVersion .= ' ' . $raw['browser']['version']['nickname'];
}
}
else {
$browserVersion = $raw['browser']['version'];
}
}

$osFamily = null;
if (isset($raw['os']['name'])) {
$osFamily = $raw['os']['name'];
}
if (isset($raw['os']['alias'])) {
$osFamily = $raw['os']['alias'];
}

$osVersion = null;
// if(isset($raw['os']['version'])){
// $osVersion = $raw['os']['version'];
// }
if(isset($raw['os']['version'])){
if(is_array($raw['os']['version'])) {
if(isset($raw['os']['version']['alias'])){
$osVersion = $raw['os']['version']['alias'];
} else {
$osVersion = $raw['os']['version']['value'];
}

if(isset($raw['os']['version']['nickname'])){
$osVersion .= ' ' . $raw['os']['version']['nickname'];
}
}
else {
$osVersion = $raw['os']['version'];
}
}

$deviceManufacturer = null;
if (isset($raw['device']['manufacturer'])) {
$deviceManufacturer = $raw['device']['manufacturer'];
}

$deviceModel = null;
if (isset($raw['device']['model'])) {
$deviceModel = $raw['device']['model'];

if (isset($raw['device']['series'])) {
$deviceModel .= ' ' . $raw['device']['series'];
}
}
elseif (isset($raw['device']['series'])) {
$deviceModel = $raw['device']['series'];
}

/*
* Bot found
Expand Down Expand Up @@ -98,6 +131,21 @@ public function parse($userAgent)
'version' => $osVersion,
'platform' => null
],

'device' => [
'brand' => $deviceManufacturer,
'model' => $deviceModel,
'type' => $raw['device']['type'],

'isMobile' => $raw['device']['type'] == 'mobile',
],

'bot' => [
'isBot' => false,

'name' => null,
'type' => null
],

'raw' => $raw
]);
Expand Down

0 comments on commit 19b235e

Please sign in to comment.