-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserAgentString.php
More file actions
73 lines (61 loc) · 1.87 KB
/
Copy pathUserAgentString.php
File metadata and controls
73 lines (61 loc) · 1.87 KB
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
<?php
/*
* Gatekeeper
* Copyright (C) 2022 Christian Neff
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all copies.
*/
namespace Secondtruth\Gatekeeper;
/**
* The UserAgentString class.
*
* @author Christian Neff <christian.neff@gmail.com>
*/
class UserAgentString
{
private $string;
public function __construct($string)
{
$this->string = $string;
}
public function __toString()
{
return $this->string;
}
public function contains($needle, $caseSensitive = false)
{
return ($caseSensitive ? strpos($this->string, $needle) : stripos($this->string, $needle)) !== false;
}
public function containsAny(array $needles, $caseSensitive = false)
{
foreach ($needles as $needle) {
if ($this->contains($needle, $caseSensitive)) {
return true;
}
}
return false;
}
public function startsWith($needle, $caseSensitive = false)
{
return ($caseSensitive ? strpos($this->string, $needle) : stripos($this->string, $needle)) === 0;
}
public function startsWithAny(array $needles, $caseSensitive = false)
{
foreach ($needles as $needle) {
if ($this->startsWith($needle, $caseSensitive)) {
return true;
}
}
return false;
}
public function compare($string, $caseSensitive = true)
{
return $caseSensitive ? strcmp($this->string, $string) : strcasecmp($this->string, $string);
}
public function compareStart($needle, $caseSensitive = true)
{
return $caseSensitive ? strncmp($this->string, $needle, mb_strlen($needle)) : strncasecmp($this->string, $needle, mb_strlen($needle));
}
}