Passed
Push — laravel-5 ( 66f120...6eb64b )
by Robert
02:51
created

src/TaggingUtility.php (1 issue)

1
<?php
2
3
namespace Conner\Tagging;
4
5
/**
6
 * Utility functions to help with various tagging functionality.
7
 *
8
 * @author Rob Conner <[email protected]>
9
 * @copyright Copyright (C) 2014 Robert Conner
10
 */
11
class TaggingUtility
12
{
13
    /**
14
     * Converts input into array
15
     *
16
     * @param string|array $tagNames
17
     * @return array
18
     */
19
    public static function makeTagArray($tagNames)
20
    {
21
        if(is_array($tagNames) && count($tagNames) == 1) {
22
            $tagNames = reset($tagNames);
23
        }
24
25
        if(is_string($tagNames)) {
26
            $tagNames = explode(',', $tagNames);
27
        } elseif(!is_array($tagNames)) {
28
            $tagNames = array(null);
29
        }
30
31
        $tagNames = array_map('trim', $tagNames);
32
33
        return array_values($tagNames);
34
    }
35
36
    public static function displayize($string)
37
    {
38
        $displayer = config('tagging.displayer');
39
        $displayer = empty($displayer) ? '\Illuminate\Support\Str::title' : $displayer;
40
41
        return call_user_func($displayer, $string);
42
    }
43
44
    public static function normalize($string)
45
    {
46
        $normalizer = config('tagging.normalizer');
47
        $normalizer = $normalizer ?: self::class.'::slug';
48
49
        return call_user_func($normalizer, $string);
50
    }
51
52
    /**
53
     * Create normalize string slug.
54
     *
55
     * Although supported, transliteration is discouraged because
56
     * 1) most web browsers support UTF-8 characters in URLs
57
     * 2) transliteration causes a loss of information
58
     *
59
     * @param string $str
60
     * @return string
61
     */
62
    public static function slug($str)
63
    {
64
        // Make sure string is in UTF-8 and strip invalid UTF-8 characters
65
        $str = mb_convert_encoding((string)$str, 'UTF-8');
66
67
        $options = [
68
            'delimiter' => config('taggable.delimiter', '-'),
69
            'limit' => '255',
70
            'lowercase' => true,
71
            'replacements' => [],
72
            'transliterate' => true,
73
        ];
74
75
        $char_map = [
76
                // Latin
77
                'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
78
                'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
79
                'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
80
                'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
81
                'ß' => 'ss',
82
                'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
83
                'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
84
                'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
85
                'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
86
                'ÿ' => 'y',
87
88
                // Latin symbols
89
                '©' => '(c)',
90
91
                // Greek
92
                'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
93
                'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
94
                'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
95
                'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
96
                'Ϋ' => 'Y',
97
                'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
98
                'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
99
                'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
100
                'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
101
                'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
102
103
                // Turkish
104
                'Ş' => 'S', 'İ' => 'I', 'Ğ' => 'G',
105
                'ş' => 's', 'ı' => 'i', 'ğ' => 'g',
106
107
                // Russian
108
                'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
109
                'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
110
                'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
111
                'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
112
                'Я' => 'Ya',
113
                'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
114
                'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
115
                'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
116
                'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
117
                'я' => 'ya',
118
119
                // Ukrainian
120
                'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G',
121
                'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
122
123
                // Czech
124
                'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U',
125
                'Ž' => 'Z',
126
                'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
127
                'ž' => 'z',
128
129
                // Polish
130
                'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ś' => 'S', 'Ź' => 'Z',
131
                'Ż' => 'Z',
132
                'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ś' => 's', 'ź' => 'z',
133
                'ż' => 'z',
134
135
                // Latvian
136
                'Ā' => 'A', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Ū' => 'u',
137
                'ā' => 'a', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n', 'ū' => 'u',
138
139
                //Romanian
140
                'Ă' => 'A', 'ă' => 'a', 'Ș' => 'S', 'ș' => 's', 'Ț' => 'T', 'ț' => 't',
141
142
                //Vietnamese
143
                'ả' => 'a', 'Ả' => 'A','ạ' => 'a', 'Ạ' => 'A', 'ắ' => 'a', 'Ắ' => 'A', 'ằ' => 'a', 'Ằ' => 'A',
144
                'ẳ' => 'a', 'Ẳ' => 'A', 'ẵ' => 'a', 'Ẵ' => 'A', 'ặ' => 'a', 'Ặ' => 'A', 'ẩ' => 'a', 'Ẩ' => 'A',
145
                'Ấ' => 'A', 'ấ' => 'a', 'Ầ' => 'A', 'ầ' => 'a', 'Ơ' => 'O', 'ơ' => 'o', 'Đ' => 'D', 'đ' => 'd',
146
                'ẫ' => 'a', 'Ẫ' => 'A', 'ậ' => 'a', 'Ậ' => 'A', 'ẻ' => 'e', 'Ẻ' => 'E', 'ẽ' => 'e', 'Ẽ' => 'E',
147
                'ẹ' => 'e', 'Ẹ' => 'E', 'ế' => 'e', 'Ế' => 'E', 'ề' => 'e', 'Ề' => 'E',  'ể' => 'e', 'Ể' => 'E',
148
                'ễ' => 'e', 'Ễ' => 'E', 'ệ' => 'e', 'Ệ' => 'E', 'ỉ' => 'i', 'Ỉ' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I',
149
                'ị' => 'i', 'Ị' => 'I', 'ỏ' => 'o', 'Ỏ' => 'O', 'ọ' => 'o', 'Ọ' => 'O', 'ố' => 'o', 'Ố' => 'O',
150
                'ồ' => 'o', 'Ồ' => 'O', 'ổ' => 'o', 'Ổ' => 'O', 'ỗ' => 'o', 'Ỗ' => 'O', 'ộ' => 'o', 'Ộ' => 'O',
151
                'ớ' => 'o', 'Ớ' => 'O', 'ờ' => 'o', 'Ờ' => 'O', 'ở' => 'o', 'Ở' => 'O', 'ỡ' => 'o', 'Ỡ' => 'O',
152
                'ợ' => 'o', 'Ợ' => 'O', 'ủ' => 'u', 'Ủ' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ụ' => 'u', 'Ụ' => 'U',
153
                'ư' => 'u', 'Ư' => 'U', 'ứ' => 'u', 'Ứ' => 'U', 'ừ' => 'u', 'Ừ' => 'U', 'ử' => 'u', 'Ử' => 'U',
154
                'ữ' => 'u', 'Ữ' => 'U', 'ự' => 'u', 'Ự' => 'U', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ỷ' => 'y', 'Ỷ' => 'Y',
155
                'ỹ' => 'y', 'Ỹ' => 'Y', 'ỵ' => 'y', 'Ỵ' => 'Y',
156
            
157
                //Kurdish
158
				'ا' => 'a', 'ب' => 'b', 'ج' => 'c', 'د' => 'd', 'ێ' => 'ee', 'ف' => 'f', 'گ' => 'g',
159
				'ژ' => 'j', 'ک' => 'k', 'ل' => 'l', 'م' => 'm', 'ن' => 'n', 'ۆ' => 'o',
160
				'پ' => 'p', 'ق' => 'q', 'ر' => 'r', 'س' => 's', 'ت' => 't', 'ڤ' => 'v', 'و' => 'w',
161
				'خ' => 'x', 'ی' => 'y', 'ز' => 'z', 'ڕ' => 'rr', 'ە' => 'e', 'ح' => 'hh', 'ع' => 'a', 'ش' => 'sh', 'غ' => 'gh', 'ك' => 'k',
162
				'ڵ' => 'll', 'چ' => 'ch', 'ھ' => 'h', "ئ" => ''
163
        ];
164
165
        // Make custom replacements
166
        $str = preg_replace(array_keys($options['replacements']), $options['replacements'], $str);
167
168
        // Transliterate characters to ASCII
169
        if ($options['transliterate']) {
170
            $str = str_replace(array_keys($char_map), $char_map, $str);
171
        }
172
        // Replace non-alphanumeric characters with our delimiter
173
        $str = preg_replace('/[^\p{L}\p{Nd}]+/u', $options['delimiter'], $str);
174
175
        // Remove duplicate delimiters
176
        $str = preg_replace('/(' . preg_quote($options['delimiter'], '/') . '){2,}/', '$1', $str);
177
178
        // Truncate slug to max. characters
179
        $str = mb_substr($str, 0, ($options['limit'] ? $options['limit'] : mb_strlen($str, 'UTF-8')), 'UTF-8');
180
181
        // Remove delimiter from ends
182
        $str = trim($str, $options['delimiter']);
183
184
        return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str;
185
    }
186
187
    /**
188
     * Private! Please do not call this function directly, just let the Tag library use it.
189
     * Increment count of tag by one. This function will create tag record if it does not exist.
190
     *
191
     * @param string $tagString
192
     * @param string $tagSlug
193
     * @param integer $count
194
     */
195
    public static function incrementCount($tagString, $tagSlug, $count)
196
    {
197
        if($count <= 0) { return; }
198
        $model = static::tagModelString();
199
200
        $tag = $model::where('slug', '=', $tagSlug)->first();
201
202
        if(!$tag) {
203
            $tag = new $model;
204
            $tag->name = $tagString;
205
            $tag->slug = $tagSlug;
206
            $tag->suggest = false;
207
            $tag->save();
208
        }
209
210
        $tag->count = $tag->count + $count;
211
        $tag->save();
212
    }
213
214
    /**
215
     * Private! Please do not call this function directly, let the Tag library use it.
216
     * Decrement count of tag by one. This function will create tag record if it does not exist.
217
     *
218
     * @param string $tagString
219
     */
220
    public static function decrementCount($tagString, $tagSlug, $count)
0 ignored issues
show
The parameter $tagString is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

220
    public static function decrementCount(/** @scrutinizer ignore-unused */ $tagString, $tagSlug, $count)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
221
    {
222
        if($count <= 0) { return; }
223
        $model = static::tagModelString();
224
225
        $tag = $model::where('slug', '=', $tagSlug)->first();
226
227
        if($tag) {
228
            $tag->count = $tag->count - $count;
229
            if($tag->count < 0) {
230
                $tag->count = 0;
231
                \Log::warning("The '.$model.' count for `$tag->name` was a negative number. This probably means your data got corrupted. Please assess your code and report an issue if you find one.");
232
            }
233
            $tag->save();
234
        }
235
    }
236
237
    /**
238
     * Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
239
     * Does not delete tags where 'suggest' is true
240
     *
241
     * @return int
242
     */
243
    public static function deleteUnusedTags()
244
    {
245
        $model = static::tagModelString();
246
        return $model::deleteUnused();
247
    }
248
249
    /**
250
     * @return string
251
     */
252
    public static function tagModelString()
253
    {
254
        return config('tagging.tag_model', '\Conner\Tagging\Model\Tag');
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    public static function taggedModelString()
261
    {
262
        return config('tagging.tagged_model', 'Conner\Tagging\Model\Tagged');
263
    }
264
}
265