TaggingUtility::incrementCount()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

227
    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...
228
    {
229
        if($count <= 0) { return; }
230
        $model = static::tagModelString();
231
232
        $tag = $model::where('slug', '=', $tagSlug)->first();
233
234
        if($tag) {
235
            $tag->count = $tag->count - $count;
236
            if($tag->count < 0) {
237
                $tag->count = 0;
238
                \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.");
239
            }
240
            $tag->save();
241
        }
242
    }
243
244
    /**
245
     * Look at the tags table and delete any tags that are no longer in use by any taggable database rows.
246
     * Does not delete tags where 'suggest' is true
247
     *
248
     * @return int
249
     */
250
    public static function deleteUnusedTags()
251
    {
252
        $model = static::tagModelString();
253
        return $model::deleteUnused();
254
    }
255
256
    /**
257
     * @return string
258
     */
259
    public static function tagModelString()
260
    {
261
        return config('tagging.tag_model', '\Conner\Tagging\Model\Tag');
262
    }
263
264
    /**
265
     * @return string
266
     */
267
    public static function taggedModelString()
268
    {
269
        return config('tagging.tagged_model', 'Conner\Tagging\Model\Tagged');
270
    }
271
}
272