Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

Str::purify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Support;
2
3
use Misd\Linkify\Linkify;
4
5
/**
6
 * Class Str
7
 *
8
 * @link   http://pyrocms.com/
9
 * @author PyroCMS, Inc. <[email protected]>
10
 * @author Ryan Thompson <[email protected]>
11
 */
12
class Str extends \Illuminate\Support\Str
13
{
14
15
    /**
16
     * Return a humanized string.
17
     *
18
     * @param         $value
19
     * @param  string $separator
20
     * @return string
21
     */
22
    public function humanize($value, $separator = '_')
23
    {
24
        return preg_replace('/[' . $separator . ']+/', ' ', strtolower(trim($value)));
25
    }
26
27
    /**
28
     * Limit the number of characters in a string
29
     * while preserving words.
30
     *
31
     * https://github.com/laravel/framework/pull/3547/files
32
     *
33
     * @param  string $value
34
     * @param  int    $limit
35
     * @param  string $end
36
     * @return string
37
     */
38
    public function truncate($value, $limit = 100, $end = '...')
39
    {
40
        if (strlen($value) <= $limit) {
41
            return $value;
42
        }
43
44
        $parts = preg_split('/([\s\n\r]+)/', $value, null, PREG_SPLIT_DELIM_CAPTURE);
45
        $count = count($parts);
46
47
        $last   = 0;
48
        $length = 0;
49
50
        for (; $last < $count; ++$last) {
51
            $length += strlen($parts[$last]);
52
53
            if ($length > $limit) {
54
                break;
55
            }
56
        }
57
58
        return trim(implode(array_slice($parts, 0, $last))) . $end;
59
    }
60
61
    /**
62
     * Linkify the provided text.
63
     *
64
     * @param       $text
65
     * @param array $options
66
     * @return string
67
     */
68
    public function linkify($text, array $options = [])
69
    {
70
        return (new Linkify($options))->process($text);
71
    }
72
73
    /**
74
     * Return purified HTML.
75
     *
76
     * @param $html
77
     * @return string
78
     */
79
    public function purify($html)
80
    {
81
        return app(Purifier::class)->purify($html);
82
    }
83
}
84