Skip to content

Commit

Permalink
Merge pull request #161 from hisorange/develop
Browse files Browse the repository at this point in the history
Release for 4.3.0
  • Loading branch information
hisorange authored Nov 7, 2020
2 parents e0f3c7e + a114a57 commit 49dcf0e
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### Changes in 4.3.0

---

- Replaced the archived piwik/device-detector with the matomo/device-detector (by @matthewnessworthy)
- Merged some readme changes
- New micro feature **isInApp** check on the browser #156

### Changes in 4.2.2

---
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Browser Detection v4.2 by _[hisorange](https://hisorange.me)_
## Browser Detection v4.3 by _[hisorange](https://hisorange.me)_

[![Latest Stable Version](https://poser.pugx.org/hisorange/browser-detect/v/stable)](https://packagist.org/packages/hisorange/browser-detect)
[![Build Status](https://travis-ci.org/hisorange/browser-detect.svg?branch=stable)](https://travis-ci.org/hisorange/browser-detect)
Expand Down Expand Up @@ -130,8 +130,8 @@ Every call on the **Browser** facade is proxied to a result object, so the follo
| Browser::browserVersionPatch() | Browser's [semantic](https://semver.org/) patch version. | _(integer)_ |
| Browser::browserEngine() | Browser's engine like: Blink, WebKit, Gecko. | _(string)_ |
| **Operating system related functions** | | |
| Browser::platformName() | Operating system's human friendly name like Windows XP, Mac 10. | _(string)_ |
| Browser::platformFamily() | Operating system's vendor like Linux, Windows, Mac. | _(string)_ |
| Browser::platformName() | Operating system's human friendly name like Windows XP, Mac 10. | _(string)_ |
| Browser::platformFamily() | Operating system's vendor like Linux, Windows, Mac. | _(string)_ |
| Browser::platformVersion() | Operating system's human friendly version like XP, Vista, 10. | _(integer)_ |
| Browser::platformVersionMajor() | Operating system's [semantic](https://semver.org/) major version. | _(integer)_ |
| Browser::platformVersionMinor() | Operating system's [semantic](https://semver.org/) minor version. | _(integer)_ |
Expand All @@ -153,6 +153,8 @@ Every call on the **Browser** facade is proxied to a result object, so the follo
| Browser::isIE() | Checks if the browser is an some kind of Internet Explorer (or Trident) | _(boolean)_ |
| Browser::isIEVersion() | Compares to a given IE version | _(boolean)_ |
| Browser::isEdge() | Is this a microsoft edge browser. | _(boolean)_ |
| **Miscellaneous** | | |
| Browser::isInApp() | Check for browsers rendered inside applications like android webview. | _(boolean)_ |

### Configuration, personalization

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
}
],
"require": {
"php": "^7.1",
"php": ">=7.2",
"ua-parser/uap-php": "~3.9",
"league/pipeline": "^1.0",
"mobiledetect/mobiledetectlib": "~2.8",
"jaybizzle/crawler-detect": "~1.2",
"piwik/device-detector": "~3.0"
"matomo/device-detector": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "~5.0 || ~6.0 || ~7.0 || ~8.0",
Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/ResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public function isEdge(): bool;
*/
public function isIE(): bool;

/**
* Is this browser an android in app browser?
*
* @return bool
*/
public function isInApp(): bool;

/**
* Is this an Internet Explorer X (or lower version).
*
Expand Down
13 changes: 13 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Result implements ResultInterface
*/
protected $isEdge = false;

/**
* @var boolean
*/
protected $isInApp = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -259,6 +264,14 @@ public function isEdge(): bool
return $this->isEdge;
}

/**
* @inheritdoc
*/
public function isInApp(): bool
{
return $this->isInApp;
}

/**
* @inheritdoc
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Stages/BrowserDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,22 @@ public function __invoke(PayloadInterface $payload): ResultInterface
$payload->setValue('isLinux', true);
}

# Request: https://github.com/hisorange/browser-detect/issues/156
$payload->setValue('isInApp', $this->detectIsInApp($payload));

return new Result($payload->toArray());
}

/**
* Code snippet based on https://github.com/f2etw/detect-inapp/blob/master/src/inapp.js#L38-L47
*
* @param PayloadInterface $payload
* @return bool
*/
protected function detectIsInApp(PayloadInterface $payload): bool {
return preg_match('%(WebView|(iPhone|iPod|iPad)(?!.*Safari\/)|Android.*(wv|\.0\.0\.0))%', $payload->getAgent());
}

/**
* Trim the trailing .0 versions from a semantic version string.
* It makes it more readable for an end user.
Expand Down
4 changes: 2 additions & 2 deletions src/Stages/DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace hisorange\BrowserDetect\Stages;

use hisorange\BrowserDetect\Contracts\StageInterface;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use hisorange\BrowserDetect\Contracts\PayloadInterface;

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ public function __invoke(PayloadInterface $payload): PayloadInterface
}

if (! empty($device['brand'])) {
$payload->setValue('deviceFamily', DeviceParserAbstract::getFullName($device['brand']));
$payload->setValue('deviceFamily', AbstractDeviceParser::getFullName($device['brand']));
}

if (! empty($device['model'])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testToArray()
'isOpera' => false,
'isSafari' => false,
'isEdge' => false,
'isInApp' => false,
'isIE' => false,
'browserName' => 'Unknown',
'browserFamily' => 'Unknown',
Expand Down Expand Up @@ -93,6 +94,7 @@ protected function getEmptyResult()
* @covers ::isOpera()
* @covers ::isSafari()
* @covers ::isIE()
* @covers ::isInApp()
* @covers ::isEdge()
* @covers ::browserName()
* @covers ::browserFamily()
Expand Down Expand Up @@ -131,6 +133,7 @@ public function testUserAgent()
$this->assertSame(!!$value, $result->isOpera());
$this->assertSame(!!$value, $result->isSafari());
$this->assertSame(!!$value, $result->isIE());
$this->assertSame(!!$value, $result->isInApp());
$this->assertSame($value, $result->browserName());
$this->assertSame($value, $result->browserFamily());
$this->assertSame($value, $result->browserVersion());
Expand Down Expand Up @@ -169,6 +172,7 @@ protected function getKeys()
'isOpera',
'isSafari',
'isEdge',
'isInApp',
'isIE',
'browserName',
'browserFamily',
Expand Down
24 changes: 24 additions & 0 deletions tests/Stages/BrowserDetectTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace hisorange\BrowserDetect\Test\Stages;

use hisorange\BrowserDetect\Payload;
use hisorange\BrowserDetect\Test\TestCase;
use hisorange\BrowserDetect\Stages\BrowserDetect;
use hisorange\BrowserDetect\Contracts\ResultInterface;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

/**
* Test the UAParser stage.
Expand Down Expand Up @@ -43,6 +46,27 @@ public function testInvoke($scenario, $expectations)
}
}

/**
* Check for inApp browsers.
*
* @return void
* @throws ExpectationFailedException
* @throws InvalidArgumentException
*/
public function testInApp()
{
$stage = new BrowserDetect;
$payload = new Payload('Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/_BuildID_) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36');
$result = $stage($payload);
$this->assertTrue($result->isInApp());


$stage = new BrowserDetect;
$payload = new Payload('Mozilla/5.0 AppleWebKit/537.36');
$result = $stage($payload);
$this->assertFalse($result->isInApp());
}

/**
* Possible device scenarios.
*
Expand Down

0 comments on commit 49dcf0e

Please sign in to comment.