Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

Str::truncate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 10
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 toCamel($string) {
27
		$ucwords = ucwords(strtolower(str_replace(['_', '-'], ' ', $string)));
28
		$camel = lcfirst(preg_replace("/[^a-zA-Z0-9]/", '', $ucwords));
29
		return $camel;
30
	}
31
32
	/**
33
	 * Retorna a string resumida, sem cortar a última palavra
34
	 * @param string $string
35
	 * @param int $limit tamanho máximo permitido
36
	 * @param bool $after define se corta depois do tamanho máximo
37
	 * @return string
38
	 */
39
	public static function truncate($string, $limit, $after = false) {
40
		if (strlen($string) > $limit) {
41
			if ($after === false) {
42
				$lenght = strrpos(substr($string, 0, $limit), ' ');
43
			} else {
44
				$lenght = strpos(substr($string, $limit), ' ') + $limit;
45
			}
46
			$string = rtrim(rtrim(substr($string, 0, $lenght), ','), '.') . '...';
47
		}
48
		return $string;
49
	}
50
51
}
52