forked from ua-parser/uap-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractParser.php
150 lines (130 loc) · 3.61 KB
/
AbstractParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* ua-parser
*
* Copyright (c) 2011-2013 Dave Olsen, http://dmolsen.com
* Copyright (c) 2013-2014 Lars Strojny, http://usrportage.de
*
* Released under the MIT license
*/
namespace UAParser;
use UAParser\Exception\FileNotFoundException;
abstract class AbstractParser
{
/** @var string */
public static $defaultFile;
/** @var array */
protected $regexes = array();
public function __construct(array $regexes)
{
$this->regexes = $regexes;
}
/**
* Create parser instance
*
* Either pass a custom regexes.php file or leave the argument empty and use the default file.
*
* @param string $file
* @throws FileNotFoundException
* @return static
*/
public static function create($file = null)
{
return $file ? static::createCustom($file) : static::createDefault();
}
/**
* @return static
* @throws FileNotFoundException
*/
protected static function createDefault()
{
return static::createInstance(
static::getDefaultFile(),
array('UAParser\Exception\FileNotFoundException', 'defaultFileNotFound')
);
}
/**
* @return static
* @throws FileNotFoundException
*/
protected static function createCustom($file)
{
return static::createInstance(
$file,
array('UAParser\Exception\FileNotFoundException', 'customRegexFileNotFound')
);
}
private static function createInstance($file, $exceptionFactory)
{
if (!file_exists($file)) {
throw call_user_func($exceptionFactory, $file);
}
return new static(include $file);
}
/**
* @param array $regexes
* @param string $userAgent
* @return array
*/
protected function tryMatch(array $regexes, $userAgent)
{
foreach ($regexes as $regex) {
$flag = isset($regex['regex_flag']) ? $regex['regex_flag'] : '';
if (preg_match('@' . $regex['regex'] . '@' . $flag, $userAgent, $matches)) {
$defaults = array(
1 => 'Other',
2 => null,
3 => null,
4 => null,
5 => null,
);
return array($regex, $matches + $defaults);
}
}
return array(null, null);
}
/**
* @param array $regex
* @param string $key
* @param string $string
* @return string
*/
protected function replaceString(array $regex, $key, $string)
{
if (!isset($regex[$key])) {
return $string;
}
return str_replace('$1', $string, $regex[$key]);
}
/**
* @param array $regex
* @param string $key
* @param string $default
* @param array $matches
* @return string
*/
protected function multiReplace(array $regex, $key, $default, array $matches)
{
if (!isset($regex[$key])) {
return $default;
}
$replacement = preg_replace_callback(
"|\\$(?<key>\d)|",
function ($m) use ($matches){
return isset($matches[$m['key']]) ? $matches[$m['key']] : "";
},
$regex[$key]
);
$replacement = trim($replacement);
return $replacement == '' ? null : $replacement;
}
/**
* @return string
*/
protected static function getDefaultFile()
{
return static::$defaultFile
? static::$defaultFile
: realpath(__DIR__ . '/../resources') . DIRECTORY_SEPARATOR . 'regexes.php';
}
}