|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Derafu: Biblioteca PHP (Núcleo). |
|
7
|
|
|
* Copyright (C) Derafu <https://www.derafu.org> |
|
8
|
|
|
* |
|
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
|
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
|
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
|
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
|
13
|
|
|
* |
|
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
|
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
|
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
|
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
|
18
|
|
|
* |
|
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de GNU |
|
20
|
|
|
* junto a este programa. |
|
21
|
|
|
* |
|
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Derafu\Lib\Core\Helper; |
|
26
|
|
|
|
|
27
|
|
|
use Illuminate\Support\Str as IlluminateStr; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Clase para trabajar con strings. |
|
31
|
|
|
*/ |
|
32
|
|
|
class Str extends IlluminateStr |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Ancho por defecto al aplicar la función wordWrap(). |
|
36
|
|
|
*/ |
|
37
|
|
|
public const WORDWRAP = 64; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Corta el string a un largo fijo por línea. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $string String a recortar. |
|
43
|
|
|
* @param integer $characters Ancho, o largo, máximo de cada línea. |
|
44
|
|
|
* @param string $break Caracter para el "corte" o salto de línea. |
|
45
|
|
|
* @param boolean $cutLongWords Si se deben cortar igual palabras largas. |
|
46
|
|
|
* @return string String en varias líneas ajustado al largo solicitado. |
|
47
|
|
|
*/ |
|
48
|
10 |
|
public static function wordWrap( |
|
49
|
|
|
$string, |
|
50
|
|
|
$characters = self::WORDWRAP, |
|
51
|
|
|
$break = "\n", |
|
52
|
|
|
$cutLongWords = true |
|
53
|
|
|
) { |
|
54
|
10 |
|
return parent::wordWrap($string, $characters, $break, $cutLongWords); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Convierte un string desde UTF-8 a ISO-8859-1. |
|
59
|
|
|
* |
|
60
|
|
|
* Si el string pasado no está codificado en UTF-8 se retornará el |
|
61
|
|
|
* string origial. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $string String a convertir en UTF-8. |
|
64
|
|
|
* @return string String en ISO-8859-1 si se logró convertir. |
|
65
|
|
|
*/ |
|
66
|
22 |
|
public static function utf8decode(string $string): string |
|
67
|
|
|
{ |
|
68
|
22 |
|
if (empty($string)) { |
|
69
|
1 |
|
return $string; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
21 |
|
if (!mb_detect_encoding($string, 'UTF-8', true)) { |
|
73
|
1 |
|
return $string; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
20 |
|
$result = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8'); |
|
77
|
|
|
|
|
78
|
20 |
|
return $result ?: $string; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Convierte un string desde ISO-8859-1 a UTF-8. |
|
83
|
|
|
* |
|
84
|
|
|
* Si el string pasado no está codificado en ISO-8859-1 se retornará el |
|
85
|
|
|
* string origial. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $string String a convertir en ISO-8859-1. |
|
88
|
|
|
* @return string String en UTF-8 si se logró convertir. |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public static function utf8encode(string $string): string |
|
91
|
|
|
{ |
|
92
|
2 |
|
if (empty($string)) { |
|
93
|
|
|
return $string; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
if (!mb_detect_encoding($string, 'ISO-8859-1', true)) { |
|
97
|
|
|
return $string; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
$result = mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1'); |
|
101
|
|
|
|
|
102
|
2 |
|
return $result ?: $string; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Reemplaza un listado de placeholders que tiene $template con los valores |
|
107
|
|
|
* que están en $data. |
|
108
|
|
|
* |
|
109
|
|
|
* Si $data es un arreglo con otros arreglos el método aplanará el arreglo y |
|
110
|
|
|
* se reemplazarán los placeholders con la sintaxis de punto ".". |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $template |
|
113
|
|
|
* @param array $data |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function replacePlaceholders(string $template, array $data): string |
|
117
|
|
|
{ |
|
118
|
|
|
$flatData = Arr::dot($data); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($flatData as $key => $value) { |
|
121
|
|
|
$template = str_replace("{{{$key}}}", (string) $value, $template); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $template; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Genera un UUID versión 4 con la variante RFC 4122. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function uuid4(): string |
|
133
|
|
|
{ |
|
134
|
|
|
$data = random_bytes(16); |
|
135
|
|
|
|
|
136
|
|
|
// Ajusta las versiones y variantes del UUID. |
|
137
|
|
|
$data[6] = chr((ord($data[6]) & 0x0f) | 0x40); // Versión 4. |
|
138
|
|
|
$data[8] = chr((ord($data[8]) & 0x3f) | 0x80); // Variante RFC 4122. |
|
139
|
|
|
|
|
140
|
|
|
return sprintf( |
|
141
|
|
|
'%08s-%04s-%04s-%04s-%12s', |
|
142
|
|
|
bin2hex(substr($data, 0, 4)), |
|
143
|
|
|
bin2hex(substr($data, 4, 2)), |
|
144
|
|
|
bin2hex(substr($data, 6, 2)), |
|
145
|
|
|
bin2hex(substr($data, 8, 2)), |
|
146
|
|
|
bin2hex(substr($data, 10, 6)) |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|