Passed
Push — main ( e55150...6ce6d7 )
by Will
02:49
created

agentzero::setProps()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.2222
cc 6
nc 8
nop 6
crap 6
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
/**
6
 * @phpstan-import-type MatchValue from config
7
 */
8
class agentzero {
9
10
	// categories
11
	public readonly ?string $type;
12
	public readonly ?string $category;
13
14
	// device
15
	public readonly ?string $vendor;
16
	public readonly ?string $device;
17
	public readonly ?string $model;
18
	public readonly ?string $build;
19
	public readonly ?int $ram;
20
21
	// architecture
22
	public readonly ?string $processor;
23
	public readonly ?string $architecture;
24
	public readonly ?int $bits;
25
	public readonly ?string $cpu;
26
	public readonly ?int $cpuclock;
27
28
	// platform
29
	public readonly ?string $kernel;
30
	public readonly ?string $platform;
31
	public readonly ?string $platformversion;
32
33
	// browser
34
	public readonly ?string $engine;
35
	public readonly ?string $engineversion;
36
	public readonly ?string $browser;
37
	public readonly ?string $browserversion;
38
	public readonly ?string $language;
39
40
	// app
41
	public readonly ?string $app;
42
	public readonly ?string $appversion;
43
	public readonly ?string $url;
44
45
	// network
46
	public readonly ?string $nettype;
47
	public readonly ?string $proxy;
48
49
	// screen
50
	public readonly ?int $width;
51
	public readonly ?int $height;
52
	public readonly ?int $dpi;
53
	public readonly ?float $density;
54
	public readonly ?bool $darkmode;
55
56
	/**
57
	 * Constructs a new AgentZero object, private because it can only be created internally
58
	 * 
59
	 * @param \stdClass $data A stdClass object containing the UA details
60
	 */
61 87
	private function __construct(\stdClass $data) {
62
63
		// categories
64 87
		$this->type = $data->type ?? null;
0 ignored issues
show
Bug introduced by
The property type is declared read-only in hexydec\agentzero\agentzero.
Loading history...
65 87
		$this->category = $data->category ?? null;
0 ignored issues
show
Bug introduced by
The property category is declared read-only in hexydec\agentzero\agentzero.
Loading history...
66
67
		// device
68 87
		$this->vendor = $data->vendor ?? null;
0 ignored issues
show
Bug introduced by
The property vendor is declared read-only in hexydec\agentzero\agentzero.
Loading history...
69 87
		$this->device = $data->device ?? null;
0 ignored issues
show
Bug introduced by
The property device is declared read-only in hexydec\agentzero\agentzero.
Loading history...
70 87
		$this->model = $data->model ?? null;
0 ignored issues
show
Bug introduced by
The property model is declared read-only in hexydec\agentzero\agentzero.
Loading history...
71 87
		$this->build = $data->build ?? null;
0 ignored issues
show
Bug introduced by
The property build is declared read-only in hexydec\agentzero\agentzero.
Loading history...
72 87
		$this->ram = $data->ram ?? null;
0 ignored issues
show
Bug introduced by
The property ram is declared read-only in hexydec\agentzero\agentzero.
Loading history...
73
74
		// architecture
75 87
		$this->processor = $data->processor ?? null;
0 ignored issues
show
Bug introduced by
The property processor is declared read-only in hexydec\agentzero\agentzero.
Loading history...
76 87
		$this->architecture = $data->architecture ?? null;
0 ignored issues
show
Bug introduced by
The property architecture is declared read-only in hexydec\agentzero\agentzero.
Loading history...
77 87
		$this->bits = $data->bits ?? null;
0 ignored issues
show
Bug introduced by
The property bits is declared read-only in hexydec\agentzero\agentzero.
Loading history...
78 87
		$this->cpu = $data->cpu ?? null;
0 ignored issues
show
Bug introduced by
The property cpu is declared read-only in hexydec\agentzero\agentzero.
Loading history...
79 87
		$this->cpuclock = $data->cpuclock ?? null;
0 ignored issues
show
Bug introduced by
The property cpuclock is declared read-only in hexydec\agentzero\agentzero.
Loading history...
80
81
		// platform
82 87
		$this->kernel = $data->kernel ?? null;
0 ignored issues
show
Bug introduced by
The property kernel is declared read-only in hexydec\agentzero\agentzero.
Loading history...
83 87
		$this->platform = $data->platform ?? null;
0 ignored issues
show
Bug introduced by
The property platform is declared read-only in hexydec\agentzero\agentzero.
Loading history...
84 87
		$this->platformversion = $data->platformversion ?? null;
0 ignored issues
show
Bug introduced by
The property platformversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
85
86
		// browser
87 87
		$this->engine = $data->engine ?? null;
0 ignored issues
show
Bug introduced by
The property engine is declared read-only in hexydec\agentzero\agentzero.
Loading history...
88 87
		$this->engineversion = $data->engineversion ?? null;
0 ignored issues
show
Bug introduced by
The property engineversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
89 87
		$this->browser = $data->browser ?? null;
0 ignored issues
show
Bug introduced by
The property browser is declared read-only in hexydec\agentzero\agentzero.
Loading history...
90 87
		$this->browserversion = $data->browserversion ?? null;
0 ignored issues
show
Bug introduced by
The property browserversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
91 87
		$this->language = $data->language ?? null;
0 ignored issues
show
Bug introduced by
The property language is declared read-only in hexydec\agentzero\agentzero.
Loading history...
92
93
		// app
94 87
		$this->app = $data->app ?? null;
0 ignored issues
show
Bug introduced by
The property app is declared read-only in hexydec\agentzero\agentzero.
Loading history...
95 87
		$this->appversion = $data->appversion ?? null;
0 ignored issues
show
Bug introduced by
The property appversion is declared read-only in hexydec\agentzero\agentzero.
Loading history...
96 87
		$this->url = $data->url ?? null;
0 ignored issues
show
Bug introduced by
The property url is declared read-only in hexydec\agentzero\agentzero.
Loading history...
97
98
		// network
99 87
		$this->nettype = $data->nettype ?? null;
0 ignored issues
show
Bug introduced by
The property nettype is declared read-only in hexydec\agentzero\agentzero.
Loading history...
100 87
		$this->proxy = $data->proxy ?? null;
0 ignored issues
show
Bug introduced by
The property proxy is declared read-only in hexydec\agentzero\agentzero.
Loading history...
101
102
		// screen
103 87
		$this->width = $data->width ?? null;
0 ignored issues
show
Bug introduced by
The property width is declared read-only in hexydec\agentzero\agentzero.
Loading history...
104 87
		$this->height = $data->height ?? null;
0 ignored issues
show
Bug introduced by
The property height is declared read-only in hexydec\agentzero\agentzero.
Loading history...
105 87
		$this->dpi = $data->dpi ?? null;
0 ignored issues
show
Bug introduced by
The property dpi is declared read-only in hexydec\agentzero\agentzero.
Loading history...
106 87
		$this->density = $data->density ?? null;
0 ignored issues
show
Bug introduced by
The property density is declared read-only in hexydec\agentzero\agentzero.
Loading history...
107 87
		$this->darkmode = $data->darkmode ?? null;
0 ignored issues
show
Bug introduced by
The property darkmode is declared read-only in hexydec\agentzero\agentzero.
Loading history...
108
	}
109
110
	/**
111
	 * Retrieves calculated properties
112
	 * 
113
	 * @param string $key The name of the property to retrieve
114
	 * @return string|int|null The requested property or null if it doesn't exist
115
	 */
116
	public function __get(string $key) : string|int|null {
117
		switch ($key) {
118
			case 'host':
119
				if ($this->url !== null && ($host = \parse_url($this->url, PHP_URL_HOST)) !== false && $host !== null) {
120
					return \str_starts_with($host, 'www.') ? \substr($host, 4) : $host;
121
				}
122
				return null;
123
			case 'browsermajorversion':
124
			case 'enginemajorversion':
125
			case 'platformmajorversion':
126
			case 'appmajorversion':
127
				$item = \str_replace('major', '', $key);
128
				$value = $this->{$item} ?? null;
129
				return $value === null ? null : \intval(\substr($value, 0, \strspn($value, '0123456789')));
130
		}
131
		return $this->{$key} ?? null;
132
	}
133
134
	/**
135
	 * Extracts tokens from a UA string
136
	 * 
137
	 * @param string $ua The User Agent string to be tokenised
138
	 * @param array<string> $single An array of strings that can appear on their own, enables the tokens to be split correctly
139
	 * @param array<string> $ignore An array of tokens that can be ignored in the UA string
140
	 * @return false|array<int,string> An array of tokens, or false if no tokens could be extracted
141
	 */
142 87
	protected static function getTokens(string $ua, array $single, array $ignore) : array|false {
143
144
		// prepare regexp
145 87
		$single = \implode('|', \array_map('preg_quote', $single));
146 87
		$pattern = '/\{[^}]++\}|[^()\[\];,\/  _-](?:(?<!'.$single.') (?!https?:\/\/)|(?<=[a-z])\([^)]+\)|[^()\[\];,\/ ]*)*[^()\[\];,\/  _-](?:\/[^;,()\[\]  ]++)?|[0-9]/i';
147
148
		// split up ua string
149 87
		if (\preg_match_all($pattern, $ua, $match)) {
150
151
			// remove ignore values
152 87
			$tokens = \array_diff($match[0], $ignore);
153
154
			// special case for handling like
155 87
			foreach ($tokens AS $key => $item) {
156 87
				if (\str_starts_with($item, 'like ')) {
157
158
					// chop off words up to a useful token e.g. Platform/Version
159 6
					if (\str_contains($item, '/') && ($pos = \mb_strrpos($item, ' ')) !== false) {
160 5
						$tokens[$key] = \mb_substr($item, $pos + 1);
161
162
					// just remove the token
163
					} else {
164 1
						unset($tokens[$key]);
165
					}
166
				}
167
			}
168
169
			// rekey and return
170 87
			return \array_values($tokens);
171
		}
172
		return false;
173
	}
174
175
	/**
176
	 * Parses a User Agent string
177
	 * 
178
	 * @param string $ua The User Agent string to be parsed
179
	 * @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
180
	 */
181 87
	public static function parse(string $ua) : agentzero|false {
182 87
		if (($config = config::get()) === null) {
183
184 87
		} elseif (($tokens = self::getTokens($ua, $config['single'], $config['ignore'])) !== false) {
185
186
			// extract UA info
187 87
			$browser = new \stdClass();
188 87
			foreach ($config['match'] AS $key => $item) {
189 87
				$item->match($browser, $key, $tokens);
190
			}
191 87
			return new agentzero($browser);
192
		}
193
		return false;
194
	}
195
}