Test Failed
Push — master ( 4516e7...6ee9d0 )
by Esteban De La Fuente
04:23
created

Str::utf8decode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
c 0
b 0
f 0
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 7
    public static function utf8decode(string $string): string
67
    {
68 7
        if (empty($string)) {
69
            return $string;
70
        }
71
72 7
        if (!mb_detect_encoding($string, 'UTF-8', true)) {
73 1
            return $string;
74
        }
75
76 6
        $result = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');
77
78 6
        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