Passed
Branch tests1.5 (b5e513)
by Wanderson
01:14
created

Str::strip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Format;
4
5
/**
6
 * Manipulador de Strings
7
 */
8
class Str {
9
10
	/**
11
	 * @param string $string
12
	 * @return string
13
	 */
14
	public static function toUrl($string) {
15
		$url = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
16
		$url = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $url);
17
		$url = preg_replace("/[\/_| -]+/", '-', $url);
18
		$url = strtolower(trim($url, '-'));
19
		return $url;
20
	}
21
22
	/**
23
	 * @param string $string
24
	 * @return string
25
	 */
26
	public static function toFileName($string) {
27
		return static::toUrl($string);
28
	}
29
30
	/**
31
	 * @param string $string
32
	 * @return int
33
	 */
34
	public static function length($string) {
35
		return mb_strlen($string);
36
	}
37
38
	/**
39
	 * @param string $string
40
	 * @return string
41
	 */
42
	public static function lower($string) {
43
		return mb_strtolower($string);
44
	}
45
46
	/**
47
	 * @param string $string
48
	 * @return string
49
	 */
50
	public static function upper($string) {
51
		return mb_strtoupper($string);
52
	}
53
54
	/**
55
	 * @param string $string
56
	 * @return string
57
	 */
58
	public static function camel($string) {
59
		$ucwords = ucwords(strtolower(str_replace(['_', '-'], ' ', $string)));
60
		$camel = lcfirst(preg_replace("/[^a-zA-Z0-9]/", '', $ucwords));
61
		return $camel;
62
	}
63
64
	/**
65
	 * Retorna a string resumida, sem cortar a última palavra
66
	 * @param string $string
67
	 * @param int $limit tamanho máximo
68
	 * @param bool $after define se corta depois do limit
69
	 * @return string
70
	 */
71
	public static function truncate($string, $limit, $after = false) {
72
		if (mb_strlen($string) > $limit) {
73
			if ($after === false) {
74
				$oc = mb_strrpos(mb_substr($string, 0, $limit + 1), ' ');
75
			} else {
76
				$oc = mb_strpos(mb_substr($string, $limit), ' ') + $limit;
77
			}
78
			$string = rtrim(rtrim(rtrim(mb_substr($string, 0, $oc), ','), '.')) . '...';
79
		}
80
		return $string;
81
	}
82
83
	/**
84
	 * Limpa a string, retirando espaços e tags html
85
	 * @param string $string
86
	 * @return string
87
	 */
88
	public static function strip($string) {
89
		return trim(strip_tags($string));
90
	}
91
92
	/**
93
	 * Formata o número com zeros à esquerda
94
	 * @param int $int
95
	 * @param int $length
96
	 * @return string
97
	 */
98
	public static function zeroOnLeft($int = 0, $length = 2) {
99
		return str_pad($int, $length, "0", STR_PAD_LEFT);
100
	}
101
102
}
103