Completed
Branch master (8e0976)
by Adam
04:13
created

Helpers.php ➔ plain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Removes all html tags
5
 *
6
 * @param string $value
7
 * @return string
8
 */
9
function plain($value)
10
{
11
    return html_entity_decode(strip_tags($value));
12
}
13
14
/**
15
 * @param $value
16
 * @param int $limit
17
 * @return string
18
 */
19
function excerpt($value, $limit = 84)
20
{
21
    $value = str_replace(["\n", "\t", "\r"], ' ', plain($value));
22
    $value = trim(preg_replace('/ {2,}/', ' ', $value));
23
24
    return str_limit($value, $limit);
25
}
26
27
/**
28
 * Zwraca tablice najczesciej wykorzystywanych slow kluczowych w tekscie
29
 *
30
 * @param string $text
31
 * @param int $limit Limit slow kluczowych
32
 * @return array
33
 */
34
function keywords($text, $limit = 10)
35
{
36
    $text = preg_replace('/[^a-zA-Z0-9 -]/', '', mb_strtolower(plain($text), 'UTF-8'));
37
    $keywords = [];
38
39
    foreach (explode(' ', $text) as $word) {
40
        if (mb_strlen($word, 'UTF-8') >= 3) {
41
            $keywords[] = $word;
42
        }
43
    }
44
45
    $keywords = array_count_values($keywords);
46
    arsort($keywords);
47
48
    $keywords = array_keys($keywords);
49
50
    if ($limit) {
51
        $keywords = array_slice($keywords, 0, $limit);
52
    }
53
54
    return $keywords;
55
}
56
57
/**
58
 * @param \Coyote\Services\Stream\Activities\Activity|string $activity
59
 * @param \Coyote\Services\Stream\Objects\ObjectInterface|null $object
60
 * @param \Coyote\Services\Stream\Objects\ObjectInterface|null $target
61
 */
62
function stream($activity, $object = null, $target = null)
63
{
64
    $manager = app(\Coyote\Services\Stream\Manager::class);
65
66
    return $manager->save($activity, $object, $target);
67
}
68
69
/**
70
 * Creates CDN assets url
71
 *
72
 * @param string $path
73
 * @param null|bool $secure
74
 * @return string
75
 * @throws \Exception
76
 */
77
function cdn($path, $secure = null)
78
{
79
    $path = trim($path, '/');
80
    $pathinfo = pathinfo($path);
81
82
    if (in_array($pathinfo['extension'], ['css', 'js'])) {
83
        $path = manifest(trim($pathinfo['basename'], '/'));
84
    }
85
86
    return asset($path, $secure);
87
}
88
89
/**
90
 * Get the path to a versioned Mix file.
91
 *
92
 * @param  string  $path
93
 * @return string
94
 *
95
 * @throws \Exception
96
 */
97
function manifest($path)
98
{
99
    static $manifest;
100
101
    if (!$manifest) {
102
        if (!file_exists($manifestPath = public_path('manifest.json'))) {
103
            throw new Exception('The webpack manifest does not exist.');
104
        }
105
106
        $manifest = json_decode(file_get_contents($manifestPath), true);
107
    }
108
109
    if (!array_key_exists($path, $manifest)) {
110
        throw new Exception(
111
            "Unable to locate webpack mix file: {$path}. Please check your ".
112
            'webpack.mix.js output paths and try again.'
113
        );
114
    }
115
116
    return $manifest[$path];
117
}
118
119
/**
120
 * Uppercase first character of each word
121
 *
122
 * @param $string
123
 * @return string
124
 */
125
function capitalize($string)
126
{
127
    return mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
128
}
129