1
|
|
|
<?php |
2
|
|
|
namespace Padosoft\Presenty; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Presenty |
8
|
|
|
* @package Padosoft\Presenty |
9
|
|
|
*/ |
10
|
|
|
class Presenty |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* An instance's string. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $str; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The string's encoding, which should be one of the mbstring module's |
21
|
|
|
* supported encodings. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $encoding; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializes a Stringy object and assigns both str and encoding properties |
29
|
|
|
* the supplied values. $str is cast to a string prior to assignment, and if |
30
|
|
|
* $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
31
|
|
|
* an InvalidArgumentException if the first argument is an array or object |
32
|
|
|
* without a __toString method. |
33
|
|
|
* |
34
|
|
|
* @param mixed $str Value to modify, after being cast to string |
35
|
|
|
* @param string $encoding The character encoding |
36
|
|
|
* @throws \InvalidArgumentException if an array or object without a |
37
|
|
|
* __toString method is passed as the first argument |
38
|
|
|
*/ |
39
|
|
|
public function __construct($str = '', $encoding = null) |
40
|
|
|
{ |
41
|
|
|
$this->validateArgument($str); |
42
|
|
|
|
43
|
|
|
$this->str = (string) $str; |
44
|
|
|
|
45
|
|
|
$this->encoding = $encoding ?: \mb_internal_encoding(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $str |
50
|
|
|
* |
51
|
|
|
* @throws \InvalidArgumentException if an array or object without a |
52
|
|
|
* __toString method is passed as the first argument |
53
|
|
|
*/ |
54
|
|
|
public function validateArgument($str): void |
55
|
|
|
{ |
56
|
|
|
if (\is_array($str)) { |
57
|
|
|
throw new InvalidArgumentException( |
58
|
|
|
'Passed value cannot be an array' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (\is_object($str) && !method_exists($str, '__toString')) { |
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
'Passed object must have a __toString method' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a Stringy object and assigns both str and encoding properties |
71
|
|
|
* the supplied values. $str is cast to a string prior to assignment, and if |
72
|
|
|
* $encoding is not specified, it defaults to mb_internal_encoding(). It |
73
|
|
|
* then returns the initialized object. Throws an InvalidArgumentException |
74
|
|
|
* if the first argument is an array or object without a __toString method. |
75
|
|
|
* |
76
|
|
|
* @param mixed $str Value to modify, after being cast to string |
77
|
|
|
* @param string $encoding The character encoding |
78
|
|
|
* @return static A Stringy object |
79
|
|
|
* @throws \InvalidArgumentException if an array or object without a __toString method is passed as the first argument |
80
|
|
|
*/ |
81
|
|
|
public static function create($str = '', $encoding = null) |
82
|
|
|
{ |
83
|
|
|
return new self($str, $encoding); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the value in $str. |
88
|
|
|
* |
89
|
|
|
* @return string The current value of the $str property |
90
|
|
|
*/ |
91
|
|
|
public function __toString() |
92
|
|
|
{ |
93
|
|
|
return $this->str; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Format money |
98
|
|
|
* |
99
|
|
|
* @param int $decimal |
100
|
|
|
* @param $currency |
101
|
|
|
* @return Presenty |
102
|
|
|
*/ |
103
|
|
|
public function money(int $decimal = 2, $currency = '€'): self |
104
|
|
|
{ |
105
|
|
|
if (isNullOrEmpty($this->str)) { |
106
|
|
|
$this->str = 0; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->number($decimal); |
110
|
|
|
|
111
|
|
|
if (!$this->str) { |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->str = $currency . ' ' . $this->str; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Format number |
122
|
|
|
* |
123
|
|
|
* @param int $decimal |
124
|
|
|
* @param $dec_point |
125
|
|
|
* @param $thousands_sep |
126
|
|
|
* @return Presenty |
127
|
|
|
*/ |
128
|
|
|
public function number(int $decimal = 0, $dec_point = ',', $thousands_sep = '.'): self |
129
|
|
|
{ |
130
|
|
|
if (isNullOrEmpty($this->str)) { |
131
|
|
|
$this->str = 0; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($decimal < 0) { |
135
|
|
|
$decimal = 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->str = number_format($this->str, $decimal, $dec_point, $thousands_sep); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Format boolean representation of string to translated yes/no string |
145
|
|
|
* |
146
|
|
|
* @param string $keyYes trans for yes |
147
|
|
|
* @param string $keyNo trans for no |
148
|
|
|
* @return Presenty |
149
|
|
|
*/ |
150
|
|
|
public function boolean($keyYes = 'si', $keyNo = 'no'): self |
151
|
|
|
{ |
152
|
|
|
if (isNullOrEmpty($this->str)) { |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($this->str == 1) { |
157
|
|
|
$this->str = $keyYes; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($this->isBooleanYes()) { |
162
|
|
|
$this->str = $keyYes; |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->str = $keyNo; |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function isBooleanYes(): bool |
174
|
|
|
{ |
175
|
|
|
$tmp = strtolower($this->str); |
176
|
|
|
return ($tmp === 'yes' || $tmp === 'si' || $tmp === 'y' || $tmp === 's'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Format url |
181
|
|
|
* |
182
|
|
|
* @param integer $len |
183
|
|
|
* @return Presenty |
184
|
|
|
*/ |
185
|
|
|
public function url(int $len = 50): self |
186
|
|
|
{ |
187
|
|
|
if (isNullOrEmpty($this->str)) { |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
$this->str = str_limit($this->str, $len); |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Format text |
196
|
|
|
* |
197
|
|
|
* @param integer $len |
198
|
|
|
* @return Presenty |
199
|
|
|
*/ |
200
|
|
|
public function description(int $len = 50): self |
201
|
|
|
{ |
202
|
|
|
if (isNullOrEmpty($this->str)) { |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
$this->str = str_limit($this->str, $len); |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Format html anchor |
211
|
|
|
* |
212
|
|
|
* @param array $arrAnchorAttributes |
213
|
|
|
* @return Presenty |
214
|
|
|
*/ |
215
|
|
|
public function anchor(array $arrAnchorAttributes = []): self |
216
|
|
|
{ |
217
|
|
|
if (isNullOrEmpty($this->str)) { |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$attrib = ''; |
222
|
|
|
foreach ($arrAnchorAttributes as $key => $val) { |
223
|
|
|
$attrib .= attre($key) . '="' . attre($val) . '" '; |
224
|
|
|
} |
225
|
|
|
$attrib .= ' '; |
226
|
|
|
|
227
|
|
|
$this->str = '<a ' . $attrib . 'href="' . $this->str . '">' . $this->str . '</a>'; |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Format mailto |
234
|
|
|
* @param array $arrAnchorAttributes |
235
|
|
|
* @param string $label |
236
|
|
|
* @return Presenty |
237
|
|
|
*/ |
238
|
|
|
public function mailto(?array $arrAnchorAttributes = [], string $label = ''): self |
239
|
|
|
{ |
240
|
|
|
if (!\is_array($arrAnchorAttributes)) { |
241
|
|
|
$arrAnchorAttributes = []; |
242
|
|
|
} |
243
|
|
|
$attrib = ''; |
244
|
|
|
foreach ($arrAnchorAttributes as $key => $value) { |
245
|
|
|
$attrib .= attre($key) . '="' . attre($value) . '" '; |
246
|
|
|
} |
247
|
|
|
if (isNullOrEmpty($label)) { |
248
|
|
|
$label = $this->str; |
249
|
|
|
} |
250
|
|
|
$this->str = '<a ' . $attrib . ' href="mailto:' . attre($this->str) . '">' . $label . '</a>'; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $classPositiveNumber |
257
|
|
|
* @param string $classNegativeNumber |
258
|
|
|
* @return Presenty |
259
|
|
|
*/ |
260
|
|
|
public function bkgPositiveOrNegative(string $classPositiveNumber = 'label label-success', string $classNegativeNumber = 'label label-danger'): self |
261
|
|
|
{ |
262
|
|
|
if (isNullOrEmpty($this->str)) { |
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$class = $classNegativeNumber; |
267
|
|
|
if ($this->str >= 0 && isDouble($this->str, '', true)) { |
268
|
|
|
$class = $classPositiveNumber; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$this->str = '<span class="' . $class . '">' . $this->str . '</span>'; |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Format date ita |
278
|
|
|
* |
279
|
|
|
* @return Presenty |
280
|
|
|
*/ |
281
|
|
|
public function dateIta(): self |
282
|
|
|
{ |
283
|
|
|
$tmp = new \DateTime($this->str); |
284
|
|
|
$this->str = $tmp->format('d/m/Y'); |
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Formats an array of strings or numbers to a single chained string. |
290
|
|
|
* Automatically trims spaces from letters. |
291
|
|
|
* If $excludeZeroNumber is set to TRUE it recognizes 0 as a falsy value and it will be ignored. |
292
|
|
|
* @param array $array |
293
|
|
|
* @param string $implodeString |
294
|
|
|
* @param boolean $excludeZeroNumber |
295
|
|
|
* @return Presenty |
296
|
|
|
*/ |
297
|
|
|
public function implode( |
298
|
|
|
array $array, |
299
|
|
|
$implodeString = " ", |
300
|
|
|
$excludeZeroNumber = true |
301
|
|
|
) { |
302
|
|
|
$trimmedArray = array_map('trim', $array); |
303
|
|
|
$cleanedArray = []; |
|
|
|
|
304
|
|
|
|
305
|
|
|
if ($excludeZeroNumber) { |
306
|
|
|
$cleanedArray = array_values(array_filter($trimmedArray)); |
307
|
|
|
} else { |
308
|
|
|
$cleanedArray = array_values( |
309
|
|
|
array_filter( |
310
|
|
|
$trimmedArray, |
311
|
|
|
function ($item) { |
312
|
|
|
return ($item || is_numeric($item)); |
313
|
|
|
}) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$this->str = implode($implodeString, $cleanedArray); |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.