1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Class definition: [NelsonMartell\Extensions] String |
7
|
|
|
* |
8
|
|
|
* Copyright © 2015-2016 Nelson Martell (http://nelson6e65.github.io) |
9
|
|
|
* |
10
|
|
|
* Licensed under The MIT License (MIT) |
11
|
|
|
* For full copyright and license information, please see the LICENSE |
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
13
|
|
|
* |
14
|
|
|
* @copyright 2015-2017 Nelson Martell |
15
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
16
|
|
|
* @since 0.7.0 |
17
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
18
|
|
|
* */ |
19
|
|
|
namespace NelsonMartell\Extensions; |
20
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
use Cake\Utility\Text as TextBase; |
23
|
|
|
|
24
|
|
|
use function NelsonMartell\msg; |
25
|
|
|
use function NelsonMartell\typeof; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Provides extension methods to handle strings. |
29
|
|
|
* This class is based on \Cake\Utility\Text of CakePHP(tm) class. |
30
|
|
|
* |
31
|
|
|
* @since 0.7.0 |
32
|
|
|
* @author Nelson Martell <[email protected]> |
33
|
|
|
* @see \Cake\Utility\Text::insert() |
34
|
|
|
* @link http://book.cakephp.org/3.0/en/core-libraries/text.html |
35
|
|
|
* */ |
36
|
|
|
class Text extends TextBase |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Replaces format elements in a string with the string representation of an |
41
|
|
|
* object matching the list of arguments specified. You can give as many |
42
|
|
|
* params as you need, or an array with values. |
43
|
|
|
* |
44
|
|
|
* ##Usage |
45
|
|
|
* Using numbers as placeholders (encloses between `{` and `}`), you can get |
46
|
|
|
* the matching string representation of each object given. Use `{0}` for |
47
|
|
|
* the fist object, `{1}` for the second, and so on: |
48
|
|
|
* |
49
|
|
|
* ```php |
50
|
|
|
* $format = '{0} is {1} years old, and have {2} cats.'; |
51
|
|
|
* echo Text::format($format, 'Bob', 65, 101); // 'Bob is 65 years old, and have 101 cats.' |
52
|
|
|
* ``` |
53
|
|
|
* |
54
|
|
|
* You can also use an array to give objects values: |
55
|
|
|
* |
56
|
|
|
* ```php |
57
|
|
|
* $format = '{0} is {1} years old, and have {2} cats.'; |
58
|
|
|
* $data = ['Bob', 65, 101]; |
59
|
|
|
* echo Text::format($format, $data); // 'Bob is 65 years old, and have 101 cats.' |
60
|
|
|
* ``` |
61
|
|
|
* |
62
|
|
|
* This is specially useful to be able to use non-numeric placeholders (named placeholders): |
63
|
|
|
* |
64
|
|
|
* ```php |
65
|
|
|
* $format = '{name} is {age} years old, and have {n} cats.'; |
66
|
|
|
* $data = ['name' => 'Bob', 'n' => 101, 'age' => 65]; |
67
|
|
|
* echo Text::format($format, $data); // 'Bob is 65 years old, and have 101 cats.' |
68
|
|
|
* ``` |
69
|
|
|
* |
70
|
|
|
* For numeric placeholders, yo can convert the array into a list of arguments. |
71
|
|
|
* |
72
|
|
|
* ```php |
73
|
|
|
* $format = '{0} is {1} years old, and have {2} cats.'; |
74
|
|
|
* $data = ['Bob', 65, 101]; |
75
|
|
|
* echo Text::format($format, ...$data); // 'Bob is 65 years old, and have 101 cats.' |
76
|
|
|
* ``` |
77
|
|
|
* |
78
|
|
|
* > Note: If objects are not convertible to string, it will throws and catchable exception |
79
|
|
|
* (`InvalidArgumentException`). |
80
|
|
|
* |
81
|
|
|
* @param string $format An string containing variable placeholders to be replaced. If you provide name |
|
|
|
|
82
|
|
|
* placeholders, you must pass the target array as |
83
|
|
|
* @param array|mixed $args Object(s) to be replaced into $format placeholders. |
84
|
|
|
* You can provide one item only of type array for named placeholders replacement. For numeric placeholders, you |
|
|
|
|
85
|
|
|
* can still pass the array or convert it into arguments by using the '...' syntax instead. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
* @throws InvalidArgumentException if $format is not an string or placeholder values are not string-convertibles. |
|
|
|
|
89
|
|
|
* @todo Implement formatting, like IFormatProvider or something like that. |
90
|
|
|
* @author Nelson Martell <[email protected]> |
91
|
|
|
*/ |
92
|
259 |
|
public static function format($format, ...$args) |
93
|
|
|
{ |
94
|
259 |
|
static $options = [ |
95
|
|
|
'before' => '{', |
96
|
|
|
'after' => '}', |
97
|
|
|
]; |
98
|
|
|
|
99
|
259 |
|
$originalData = $args; |
100
|
|
|
|
101
|
|
|
// Make it compatible with named placeholders along numeric ones if passed only 1 array as argument |
|
|
|
|
102
|
259 |
|
if (count($args) === 1 && is_array($args[0])) { |
103
|
251 |
|
$originalData = $args[0]; |
104
|
|
|
} |
105
|
|
|
|
106
|
259 |
|
$data = []; |
107
|
|
|
// Sanitize values to be convertibles into strings |
108
|
259 |
|
foreach ($originalData as $placeholder => $value) { |
109
|
259 |
|
$valueType = typeof($value); |
110
|
|
|
|
111
|
259 |
|
if ($valueType->canBeString() === false) { |
112
|
3 |
|
$msg = 'Value for "{{0}}" placeholder is not convertible to string; "{1}" type given.'; |
|
|
|
|
113
|
3 |
|
throw new InvalidArgumentException(msg($msg, $placeholder, $valueType)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// This is to work-arround a bug in use of ``asort()`` function in ``Text::insert`` (at v3.2.5) |
|
|
|
|
117
|
|
|
// without SORT_STRING flag... by forcing value to be string. |
118
|
259 |
|
settype($value, 'string'); |
119
|
259 |
|
$data[$placeholder] = $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
259 |
|
return static::insert($format, $data, $options); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Ensures that object given is not null. If is `null`, throws and exception. |
127
|
|
|
* |
128
|
|
|
* @param mixed $obj Object to validate |
129
|
|
|
* |
130
|
|
|
* @return mixed Same object |
131
|
|
|
* @throws InvalidArgumentException if object is `null`. |
132
|
|
|
*/ |
133
|
328 |
|
public static function ensureIsNotNull($obj) |
134
|
|
|
{ |
135
|
328 |
|
if (is_null($obj)) { |
136
|
|
|
$msg = msg('Provided object must not be NULL.'); |
137
|
|
|
throw new InvalidArgumentException($msg); |
138
|
|
|
} |
139
|
|
|
|
140
|
328 |
|
return $obj; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Ensures that object given is an string. Else, thows an exception |
145
|
|
|
* |
146
|
|
|
* @param mixed $obj Object to validate. |
147
|
|
|
* |
148
|
|
|
* @return string Same object given, but ensured that is an string. |
149
|
|
|
* @throws InvalidArgumentException if object is not an `string`. |
150
|
|
|
*/ |
151
|
328 |
|
public static function ensureIsString($obj) |
152
|
|
|
{ |
153
|
328 |
|
if (!is_string(static::ensureIsNotNull($obj))) { |
154
|
1 |
|
$msg = msg('Provided object must to be an string; "{0}" given.', typeof($obj)); |
155
|
1 |
|
throw new InvalidArgumentException($msg); |
156
|
|
|
} |
157
|
|
|
|
158
|
328 |
|
return $obj; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Ensures that given string is not empty. |
163
|
|
|
* |
164
|
|
|
* @param string $string String to validate. |
165
|
|
|
* |
166
|
|
|
* @return string Same string given, but ensured that is not empty. |
167
|
|
|
* @throws InvalidArgumentException if string is null or empty. |
168
|
|
|
*/ |
169
|
|
|
public static function ensureIsNotEmpty($string) |
170
|
|
|
{ |
171
|
|
|
if (static::ensureIsString($string) === '') { |
172
|
|
|
$msg = msg('Provided string must not be empty.'); |
173
|
|
|
throw new InvalidArgumentException($msg); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $string; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Ensures that given string is not empty or whitespaces. |
181
|
|
|
* |
182
|
|
|
* @param string $string String to validate. |
183
|
|
|
* |
184
|
|
|
* @return string Same string given, but ensured that is not whitespaces. |
185
|
|
|
* @throws InvalidArgumentException if object is not an `string`. |
186
|
|
|
* @see \trim() |
187
|
|
|
*/ |
188
|
|
|
public static function ensureIsNotWhiteSpaces($string) |
189
|
|
|
{ |
190
|
|
|
if (trim(static::ensureIsNotEmpty($string)) === '') { |
191
|
|
|
$msg = msg('Provided string must not be white spaces.'); |
192
|
|
|
throw new InvalidArgumentException($msg); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $string; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Ensures that an string follows the PHP variables naming convention. |
200
|
|
|
* |
201
|
|
|
* @param string $string String to be ensured. |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
* @throws InvalidArgumentException if object is not an `string` or do not |
205
|
|
|
* follows the PHP variables naming convention. |
206
|
|
|
*/ |
207
|
328 |
|
public static function ensureIsValidVarName($string) |
208
|
|
|
{ |
209
|
328 |
|
$pattern = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/'; |
210
|
|
|
|
211
|
328 |
|
if (!preg_match($pattern, static::ensureIsString($string))) { |
212
|
|
|
$msg = msg('Provided string do not follows PHP variables naming convention: "{0}".', $string); |
|
|
|
|
213
|
|
|
throw new InvalidArgumentException($msg); |
214
|
|
|
} |
215
|
|
|
|
216
|
328 |
|
return $string; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.