Skip to content

Commit c5a6718

Browse files
committed
Added unit testing for PHP 7.2
Updated README.md Separated preg_replace code into an method
1 parent ae5660b commit c5a6718

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ env:
44
sudo: false
55
language: php
66
php:
7+
- 7.2
78
- 7.1
89
- 7.0
910
- 5.6

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
[![Test Coverage](https://api.codeclimate.com/v1/badges/319c474eb3a681c50ba3/test_coverage)](https://codeclimate.com/github/VIPnytt/UserAgentParser/test_coverage)
55
[![License](https://poser.pugx.org/VIPnytt/UserAgentParser/license)](https://github.com/VIPnytt/UserAgentParser/blob/master/LICENSE)
66
[![Packagist](https://img.shields.io/packagist/v/vipnytt/useragentparser.svg)](https://packagist.org/packages/vipnytt/useragentparser)
7-
[![Chat](https://badges.gitter.im/VIPnytt/UserAgentParser.svg)](https://gitter.im/VIPnytt/UserAgentParser)
87

98
# User-Agent parser for robot rule sets
109
Parser and group determiner optimized for ``robots.txt``, ``X-Robots-tag`` and ``Robots-meta-tag`` usage cases.
1110

1211
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/1386c14c-546c-4c42-ac55-91ea3a3a1ae1/big.png)](https://insight.sensiolabs.com/projects/1386c14c-546c-4c42-ac55-91ea3a3a1ae1)
1312

1413
#### Requirements:
15-
- PHP [5.5+](http://php.net/supported-versions.php)
14+
- PHP 5.5+ or 7.0+
1615

1716
## Installation
1817
The library is available for install via [Composer](https://getcomposer.org). Just add this to your `composer.json` file:

src/UserAgentParser.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ class UserAgentParser
2525
*/
2626
const RFC_README = 'https://tools.ietf.org/html/rfc7231#section-5.5.3';
2727

28-
/**
29-
* PREG pattern for valid characters
30-
*/
31-
const PREG_PATTERN = '/[^\x21-\x7E]/';
32-
3328
/**
3429
* Origin product
3530
* @var string
@@ -91,7 +86,7 @@ private function validateProduct()
9186
{
9287
$this->blacklistCheck($this->product);
9388
$this->originProduct = $this->product;
94-
if ($this->originProduct !== ($this->product = preg_replace(self::PREG_PATTERN, '', $this->product))) {
89+
if ($this->originProduct !== ($this->product = $this->strip($this->product))) {
9590
trigger_error("Product name contains invalid characters. Truncated to `$this->product`.", E_USER_NOTICE);
9691
}
9792
if (empty($this->product)) {
@@ -122,6 +117,17 @@ private function blacklistCheck($input)
122117
return true;
123118
}
124119

120+
/**
121+
* Strip invalid characters
122+
*
123+
* @param string|string[] $string
124+
* @return string|string[]|null
125+
*/
126+
private function strip($string)
127+
{
128+
return preg_replace('/[^\x21-\x7E]/', '', $string);
129+
}
130+
125131
/**
126132
* Validate the Version and it's format
127133
* @link https://tools.ietf.org/html/rfc7231#section-5.5.3
@@ -187,7 +193,7 @@ public function getMostSpecific(array $userAgents)
187193
$array = [];
188194
foreach ($userAgents as $string) {
189195
// Strip non-US-ASCII characters
190-
$array[$string] = strtolower(preg_replace(self::PREG_PATTERN, '', $string));
196+
$array[$string] = strtolower($this->strip($string));
191197
}
192198
foreach (array_map('strtolower', $this->getUserAgents()) as $generated) {
193199
if (($result = array_search($generated, $array)) !== false) {

0 commit comments

Comments
 (0)