props   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 91
ccs 32
cts 32
cp 1
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A set() 0 9 6
B match() 0 35 10
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
class props {
6
7
	/**
8
	 * @var string $type The type of search to use when matching - start|any|end|exact
9
	 */
10
	protected string $type;
11
12
	/**
13
	 * @var array<string,string|int|bool>|\Closure $props An array of properties to set when matched, or a closure that generates the properties
14
	 */
15
	protected array|\Closure $props;
16
17
	/**
18
	 * Creates a props object
19
	 * 
20
	 * @param string $type The type of search to use when matching - start|any|end|exact
21
	 * @param array<string,string|int|bool>|\Closure $props An array of properties to set when matched, or a closure that generates the properties
22
	 */
23 1
	public function __construct(string $type, array|\Closure $props) {
24 1
		$this->type = $type;
25 1
		$this->props = $props;
26
	}
27
28
	/**
29
	 * Match tokens against this property
30
	 * 
31
	 * @param \stdClass $obj A stdClass object to push the matched properties into
32
	 * @param string $search The string to match
33
	 * @param array<string> $tokens An array of tokens
34
	 * @param array<string> $tokenslower The same array of tokens, but with te values lowercased
35
	 * @param array<mixed> $config A configuration array
36
	 * @return void
37
	 */
38 100
	public function match(\stdClass $obj, string $search, array $tokens, array $tokenslower, array $config = []) : void {
39 100
		$type = $this->type;
40 100
		$searchlower = \mb_strtolower($search);
41 100
		foreach ($tokenslower AS $i => $item) {
42
			switch ($type) {
43
44
				// match exact string
45 100
				case 'exact':
46 100
					if ($item === $searchlower) {
47 84
						$this->set($obj, $tokens[$i], $i, $tokens, $search, $config);
48 84
						break 2;
49
					} else {
50 100
						break;
51
					}
52
53
				// match from start of string
54 100
				case 'start':
55 100
					if (\str_starts_with($item, $searchlower)) {
56 100
						$this->set($obj, $tokens[$i], $i, $tokens, $search, $config);
57
					}
58 100
					break;
59
60
				// match anywhere in the string
61 100
				case 'any':
62 100
					if (\str_contains($item, $searchlower)) {
63 100
						$this->set($obj, $tokens[$i], $i, $tokens, $search, $config);
64
					}
65 100
					break;
66
67
				// match end of token
68 100
				case 'end':
69 100
					if (\str_ends_with($item, $searchlower)) {
70 13
						$this->set($obj, $tokens[$i], $i, $tokens, $search, $config);
71
					}
72 100
					break;
73
			}
74
		}
75
	}
76
77
	/**
78
	 * Sets parsed UA properties, and calls callbacks to generate properties and sets them to the output object
79
	 * 
80
	 * @param \stdClass $obj A stdClass object to which the properties will be set
81
	 * @param string $value The current token value
82
	 * @param int $i The ID of the current token
83
	 * @param array<string> $tokens The tokens array
84
	 * @param string $key The string that was matched in the token
85
	 * @return void
86
	 */
87 100
	protected function set(\stdClass $obj, string $value, int $i, array $tokens, string $key, array $config = []) : void {
88 100
		$props = $this->props;
89 100
		if ($props instanceof \Closure) {
90 100
			$props = $props($value, $i, $tokens, $key, $config);
91
		}
92 100
		if (\is_array($props)) {
93 99
			foreach ($props AS $key => $item) {
94 99
				if ($item !== null && !isset($obj->{$key})) {
95 99
					$obj->{$key} = $item;
96
				}
97
			}
98
		}
99
	}
100
}