Passed
Push — master ( 4a0974...9e10e2 )
by Esteban De La Fuente
04:23
created

Str   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 93
ccs 14
cts 21
cp 0.6667
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A utf8decode() 0 13 4
A utf8encode() 0 13 4
A wordWrap() 0 7 1
A replacePlaceholders() 0 9 2
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result ?: $string could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result ?: $string could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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
    function replacePlaceholders(string $template, array $data): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
    {
118
        $flatData = Arr::dot($data);
119
120
        foreach ($flatData as $key => $value) {
121
            $template = str_replace("{{{$key}}}", $value, $template);
122
        }
123
124
        return $template;
125
    }
126
}
127