forked from phpgearbox/string
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIs.php
More file actions
158 lines (140 loc) · 4.84 KB
/
Copy pathIs.php
File metadata and controls
158 lines (140 loc) · 4.84 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
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
151
152
153
154
155
156
157
158
<?php namespace Tualo\StringGear\Methods;
////////////////////////////////////////////////////////////////////////////////
// __________ __ ________ __________
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ /
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > <
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \
// \/|__| \/ \/ \/ \/ \/
// -----------------------------------------------------------------------------
// Designed and Developed by Brad Jones <brad @="bjc.id.au" />
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
use voku\helper\UTF8;
trait Is
{
/**
* Poor mans WildCard regular expression.
*
* Asterisks are translated into zero-or-more regular expression wildcards
* to make it convenient to check if the strings starts with the given
* pattern such as "library/*", making any string check convenient.
*
* @credit Originally from Laravel, thanks Taylor.
*
* @param string $pattern The string or pattern to match against.
*
* @return bool Whether or not we match the provided pattern.
*/
public function is($pattern)
{
if ($this->toString() === $pattern) return true;
$quotedPattern = preg_quote($pattern, $this->regexDelimiter);
$replaceWildCards = str_replace('\*', '.*', $quotedPattern);
return $this->regexMatch('^'.$replaceWildCards.'\z');
}
/**
* Is the entire string lower case?
*
* @return bool Whether or not $str contains only lower case characters.
*/
public function isLowerCase()
{
return $this->regexMatch('^[[:lower:]]*$');
}
/**
* Is the entire string upper case?
*
* @return bool Whether or not $str contains only upper case characters.
*/
public function isUpperCase()
{
return $this->regexMatch('^[[:upper:]]*$');
}
/**
* Returns true if the string contains only alphabetic chars, false
* otherwise.
*
* @return bool Whether or not $str contains only alphabetic chars
*/
public function isAlpha()
{
return $this->regexMatch('^[[:alpha:]]*$');
}
/**
* Returns true if the string contains only alphabetic and numeric chars,
* false otherwise.
*
* @return bool Whether or not $str contains only alphanumeric chars
*/
public function isAlphanumeric()
{
return $this->regexMatch('^[[:alnum:]]*$');
}
/**
* Returns true if the string contains only whitespace chars, false
* otherwise.
*
* @return bool Whether or not $str contains only whitespace characters
*/
public function isBlank()
{
return $this->regexMatch('^[[:space:]]*$');
}
/**
* Returns true if the string contains only hexadecimal chars, false
* otherwise.
*
* @return bool Whether or not $str contains only hexadecimal chars
*/
public function isHexadecimal()
{
return $this->regexMatch('^[[:xdigit:]]*$');
}
/**
* Returns true if the string is JSON, false otherwise. Unlike json_decode
* in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers,
* in that an empty string is not considered valid JSON.
*
* @return bool Whether or not $str is JSON
*/
public function isJson()
{
if ($this->getLength() === 0) return false;
json_decode($this->scalarString);
return (json_last_error() === JSON_ERROR_NONE);
}
/**
* Returns true if the string is serialized, false otherwise.
*
* @return bool Whether or not $str is serialized
*/
public function isSerialized()
{
if ($this->getLength() === 0) return false;
/** @noinspection PhpUsageOfSilenceOperatorInspection */
return
(
$this->scalarString === 'b:0;' ||
@unserialize($this->scalarString) !== false
);
}
/**
* Returns true if the string is base64 encoded, false otherwise.
*
* @return bool
*/
public function isBase64()
{
// An empty string is by definition not encoded.
if ($this->getLength() === 0) return false;
// Grab the current string value.
$possiblyEncoded = $this->scalarString;
// Attempt to decode it.
$decoded = base64_decode($possiblyEncoded, true);
// If we get false it can't be base64
if ($decoded === false) return false;
// Lets double check
return (base64_encode($decoded) === $this->scalarString);
}
}