Skip to content

Commit

Permalink
Merge pull request #44 from ThaDafinser/feature/http
Browse files Browse the repository at this point in the history
adding some new methods
  • Loading branch information
ThaDafinser committed Dec 14, 2015
2 parents d31fa56 + dfdefe6 commit 89d544d
Show file tree
Hide file tree
Showing 32 changed files with 785 additions and 280 deletions.
100 changes: 93 additions & 7 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
<?php
namespace UserAgentParser\Provider;

use DateTime;
use DateTimeZone;
use UserAgentParser\Exception;
use UserAgentParser\Model;

abstract class AbstractProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name;

/**
* Homepage of the provider
*
* @var string
*/
protected $homepage;

/**
* Composer package name
*
* @var string
*/
protected $packageName;

/**
* Version string for caching
*
* @var string
*/
private $version;

protected $defaultValues = [];
/**
* Last update date
*
* @var DateTime
*/
private $updateDate;

/**
* Per default the provider cannot detect anything
Expand Down Expand Up @@ -58,14 +87,37 @@ abstract class AbstractProvider
*/
protected $detectionCapabilities = [];

protected $defaultValues = [];

/**
* Return the name of the provider
*
* @return string
*/
abstract public function getName();
public function getName()
{
return $this->name;
}

abstract public function getComposerPackageName();
/**
* Get the homepage
*
* @return string
*/
public function getHomepage()
{
return $this->homepage;
}

/**
* Get the package name
*
* @return string null
*/
public function getPackageName()
{
return $this->packageName;
}

/**
* Return the version of the provider
Expand All @@ -78,18 +130,18 @@ public function getVersion()
return $this->version;
}

if ($this->getComposerPackageName() === null) {
if ($this->getPackageName() === null) {
return;
}

$packages = $this->getComposerPackages();
$packages = $this->getPackages();

if ($packages === null) {
return;
}

foreach ($packages as $package) {
if ($package->name === $this->getComposerPackageName()) {
if ($package->name === $this->getPackageName()) {
$this->version = $package->version;

break;
Expand All @@ -99,11 +151,45 @@ public function getVersion()
return $this->version;
}

/**
* Get the last change date of the provider
*
* @return DateTime null
*/
public function getUpdateDate()
{
if ($this->updateDate !== null) {
return $this->updateDate;
}

if ($this->getPackageName() === null) {
return;
}

$packages = $this->getPackages();

if ($packages === null) {
return;
}

foreach ($packages as $package) {
if ($package->name === $this->getPackageName()) {
$updateDate = new DateTime($package->time, new DateTimeZone('UTC'));

$this->updateDate = $updateDate;

break;
}
}

return $this->updateDate;
}

/**
*
* @return array null
*/
private function getComposerPackages()
private function getPackages()
{
if (! file_exists('composer.lock')) {
return;
Expand Down
36 changes: 26 additions & 10 deletions src/Provider/BrowscapPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@

class BrowscapPhp extends AbstractProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name = 'BrowscapPhp';

/**
* Homepage of the provider
*
* @var string
*/
protected $homepage = 'https://github.com/browscap/browscap-php';

/**
* Composer package name
*
* @var string
*/
protected $packageName = 'browscap/browscap-php';

protected $detectionCapabilities = [

'browser' => [
Expand Down Expand Up @@ -58,23 +79,18 @@ public function __construct(Browscap $parser)
$this->parser = $parser;
}

public function getName()
{
return 'BrowscapPhp';
}

public function getComposerPackageName()
{
return 'browscap/browscap-php';
}

public function getVersion()
{
return $this->getParser()
->getCache()
->getVersion();
}

public function getUpdateDate()
{
return;
}

/**
*
* @return Browscap
Expand Down
17 changes: 7 additions & 10 deletions src/Provider/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

class Chain extends AbstractProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name = 'Chain';

/**
*
* @var AbstractProvider[]
Expand All @@ -20,16 +27,6 @@ public function __construct(array $providers = [])
$this->providers = $providers;
}

public function getName()
{
return 'Chain';
}

public function getComposerPackageName()
{
return;
}

/**
*
* @return AbstractProvider[]
Expand Down
33 changes: 22 additions & 11 deletions src/Provider/DonatjUAParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@

class DonatjUAParser extends AbstractProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name = 'DonatjUAParser';

/**
* Homepage of the provider
*
* @var string
*/
protected $homepage = 'https://github.com/donatj/PhpUserAgent';

/**
* Composer package name
*
* @var string
*/
protected $packageName = 'donatj/phpuseragentparser';

protected $detectionCapabilities = [

'browser' => [
Expand Down Expand Up @@ -41,20 +62,10 @@ class DonatjUAParser extends AbstractProvider
public function __construct()
{
if (! function_exists('parse_user_agent')) {
throw new Exception\PackageNotLoadedException('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
throw new Exception\PackageNotLoadedException('You need to install ' . $this->getHomepage() . ' to use this provider');
}
}

public function getName()
{
return 'DonatjUAParser';
}

public function getComposerPackageName()
{
return 'donatj/phpuseragentparser';
}

/**
*
* @param array $resultRaw
Expand Down
30 changes: 15 additions & 15 deletions src/Provider/Http/NeutrinoApiCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
use UserAgentParser\Exception;
use UserAgentParser\Model;

/**
*
* @see https://www.neutrinoapi.com/api/user-agent-info/
*/
class NeutrinoApiCom extends AbstractHttpProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name = 'NeutrinoApiCom';

/**
* Homepage of the provider
*
* @var string
*/
protected $homepage = 'https://www.neutrinoapi.com/';

protected $detectionCapabilities = [

'browser' => [
Expand Down Expand Up @@ -63,16 +73,6 @@ public function __construct(Client $client, $apiUserId, $apiKey)
$this->apiKey = $apiKey;
}

public function getName()
{
return 'NeutrinoApiCom';
}

public function getComposerPackageName()
{
return;
}

public function getVersion()
{
return;
Expand Down Expand Up @@ -252,7 +252,7 @@ private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
$device->setType($resultRaw->type);
}

if (isset($resultRaw->is_mobile) && $this->isRealResult($resultRaw->is_mobile) === true) {
if (isset($resultRaw->is_mobile) && $resultRaw->is_mobile === true) {
$device->setIsMobile(true);
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/Provider/Http/UdgerCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
*/
class UdgerCom extends AbstractHttpProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name = 'UdgerCom';

/**
* Homepage of the provider
*
* @var string
*/
protected $homepage = 'https://udger.com/';

protected $detectionCapabilities = [

'browser' => [
Expand Down Expand Up @@ -61,16 +75,6 @@ public function __construct(Client $client, $apiKey)
$this->apiKey = $apiKey;
}

public function getName()
{
return 'UdgerCom';
}

public function getComposerPackageName()
{
return;
}

public function getVersion()
{
return;
Expand Down
Loading

0 comments on commit 89d544d

Please sign in to comment.