|
1
|
|
|
<?php namespace Gears\String\Methods; |
|
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// __________ __ ________ __________ |
|
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
|
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
|
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
|
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
|
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
|
9
|
|
|
// ----------------------------------------------------------------------------- |
|
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
|
11
|
|
|
// ----------------------------------------------------------------------------- |
|
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
13
|
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
|
15
|
|
|
|
|
16
|
|
|
trait Is |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Poor mans WildCard regular expression. |
|
20
|
|
|
* |
|
21
|
|
|
* Asterisks are translated into zero-or-more regular expression wildcards |
|
22
|
|
|
* to make it convenient to check if the strings starts with the given |
|
23
|
|
|
* pattern such as "library/*", making any string check convenient. |
|
24
|
|
|
* |
|
25
|
|
|
* @credit Originally from Laravel, thanks Taylor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $pattern The string or pattern to match against. |
|
28
|
|
|
* |
|
29
|
|
|
* @return bool Whether or not we match the provided pattern. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function is($pattern) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($this->toString() === $pattern) return true; |
|
34
|
|
|
$quotedPattern = preg_quote($pattern, $this->regexDelimiter); |
|
35
|
|
|
$replaceWildCards = str_replace('\*', '.*', $quotedPattern); |
|
36
|
|
|
return $this->regexMatch('^'.$replaceWildCards.'\z'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Is the entire string lower case? |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool Whether or not $str contains only lower case characters. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function isLowerCase() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->regexMatch('^[[:lower:]]*$'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Is the entire string upper case? |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool Whether or not $str contains only upper case characters. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function isUpperCase() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->regexMatch('^[[:upper:]]*$'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns true if the string contains only alphabetic chars, false |
|
61
|
|
|
* otherwise. |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool Whether or not $str contains only alphabetic chars |
|
64
|
|
|
*/ |
|
65
|
|
|
public function isAlpha() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->regexMatch('^[[:alpha:]]*$'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns true if the string contains only alphabetic and numeric chars, |
|
72
|
|
|
* false otherwise. |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool Whether or not $str contains only alphanumeric chars |
|
75
|
|
|
*/ |
|
76
|
|
|
public function isAlphanumeric() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->regexMatch('^[[:alnum:]]*$'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns true if the string contains only whitespace chars, false |
|
83
|
|
|
* otherwise. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool Whether or not $str contains only whitespace characters |
|
86
|
|
|
*/ |
|
87
|
|
|
public function isBlank() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->regexMatch('^[[:space:]]*$'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns true if the string contains only hexadecimal chars, false |
|
94
|
|
|
* otherwise. |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool Whether or not $str contains only hexadecimal chars |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isHexadecimal() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->regexMatch('^[[:xdigit:]]*$'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns true if the string is JSON, false otherwise. Unlike json_decode |
|
105
|
|
|
* in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
|
106
|
|
|
* in that an empty string is not considered valid JSON. |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool Whether or not $str is JSON |
|
109
|
|
|
*/ |
|
110
|
|
|
public function isJson() |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->getLength() === 0) return false; |
|
113
|
|
|
|
|
114
|
|
|
json_decode($this->scalarString); |
|
115
|
|
|
|
|
116
|
|
|
return (json_last_error() === JSON_ERROR_NONE); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns true if the string is serialized, false otherwise. |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool Whether or not $str is serialized |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isSerialized() |
|
125
|
|
|
{ |
|
126
|
|
|
if ($this->getLength() === 0) return false; |
|
127
|
|
|
|
|
128
|
|
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
129
|
|
|
return |
|
130
|
|
|
( |
|
131
|
|
|
$this->scalarString === 'b:0;' || |
|
132
|
|
|
@unserialize($this->scalarString) !== false |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns true if the string is base64 encoded, false otherwise. |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function isBase64() |
|
142
|
|
|
{ |
|
143
|
|
|
// An empty string is by definition not encoded. |
|
144
|
|
|
if ($this->getLength() === 0) return false; |
|
145
|
|
|
|
|
146
|
|
|
// Grab the current string value. |
|
147
|
|
|
$possiblyEncoded = $this->scalarString; |
|
148
|
|
|
|
|
149
|
|
|
// Attempt to decode it. |
|
150
|
|
|
$decoded = base64_decode($possiblyEncoded, true); |
|
151
|
|
|
|
|
152
|
|
|
// If we get false it can't be base64 |
|
153
|
|
|
if ($decoded === false) return false; |
|
154
|
|
|
|
|
155
|
|
|
// Lets double check |
|
156
|
|
|
return (base64_encode($decoded) === $this->scalarString); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|