tgn_singularity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 1
eloc 28
c 3
b 1
f 2
nc 1
nop 1
dl 0
loc 32
rs 9.472
1
<?php
2
3
use CSlant\TelegramGitNotifier\Helpers\ConfigHelper;
4
5
if (!function_exists('tgn_singularity')) {
6
    /**
7
     * The reverse of pluralizing, returns the singular form of a word in a string.
8
     *
9
     * @param string $word
10
     *
11
     * @return string|null
12
     */
13
    function tgn_singularity(string $word): string|null
14
    {
15
        static $singular_rules = [
16
            '/(quiz)zes$/i' => '$1',
17
            '/(matr)ices$/i' => '$1ix',
18
            '/(vert|ind)ices$/i' => '$1ex',
19
            '/^(ox)en$/i' => '$1',
20
            '/(alias|status)es$/i' => '$1',
21
            '/([octop|vir])i$/i' => '$1us',
22
            '/(cris|ax|test)es$/i' => '$1is',
23
            '/(shoe)s$/i' => '$1',
24
            '/(o)es$/i' => '$1',
25
            '/(bus)es$/i' => '$1',
26
            '/([m|l])ice$/i' => '$1ouse',
27
            '/(x|ch|ss|sh)es$/i' => '$1',
28
            '/(m)ovies$/i' => '$1ovie',
29
            '/(s)eries$/i' => '$1eries',
30
            '/([^aeiouy]|qu)ies$/i' => '$1y',
31
            '/([lr])ves$/i' => '$1f',
32
            '/(tive)s$/i' => '$1',
33
            '/(hive)s$/i' => '$1',
34
            '/([^f])ves$/i' => '$1fe',
35
            '/(^analy)ses$/i' => '$1sis',
36
            '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '$1$2sis',
37
            '/(n)ews$/i' => '$1ews',
38
            '/(.)s$/i' => '$1',
39
        ];
40
41
        return preg_replace(
42
            array_keys($singular_rules),
43
            array_values($singular_rules),
44
            $word
45
        );
46
    }
47
}
48
49
if (!function_exists('tgn_snake_case')) {
50
    /**
51
     * Convert a string to a snack case
52
     *
53
     * @param string $string
54
     *
55
     * @return string
56
     */
57
    function tgn_snake_case(string $string): string
58
    {
59
        $string = preg_replace('/\s+/', '_', $string);
60
61
        return strtolower($string);
62
    }
63
}
64
65
if (!function_exists('tgn_event_name')) {
66
    /**
67
     * Get event name
68
     *
69
     * @param string $event
70
     *
71
     * @return string
72
     */
73
    function tgn_event_name(string $event): string
74
    {
75
        return tgn_snake_case(str_replace(' Hook', '', $event));
76
    }
77
}
78
79
if (!function_exists('tgn_convert_event_name')) {
80
    /**
81
     * Convert event name
82
     *
83
     * @param string $event
84
     *
85
     * @return string
86
     */
87
    function tgn_convert_event_name(string $event): string
88
    {
89
        return tgn_singularity(tgn_event_name($event));
90
    }
91
}
92
93
if (!function_exists('tgn_convert_action_name')) {
94
    /**
95
     * Convert action name
96
     *
97
     * @param string $action
98
     *
99
     * @return string
100
     */
101
    function tgn_convert_action_name(string $action): string
102
    {
103
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $action));
104
    }
105
}
106
107
if (!class_exists('Illuminate\Foundation\Application')) {
108
    if (!function_exists('config')) {
109
        /**
110
         * Return config value by string
111
         *
112
         * @param string $string
113
         *
114
         * @return mixed
115
         */
116
        function config(string $string): mixed
117
        {
118
            return (new ConfigHelper())->execConfig($string);
119
        }
120
    }
121
122
    if (!function_exists('view')) {
123
        /**
124
         * Get view template
125
         *
126
         * @param string $partialPath
127
         * @param array $data
128
         *
129
         * @return null|string
130
         */
131
        function view(string $partialPath, array $data = []): null|string
132
        {
133
            $content = (new ConfigHelper())->getTemplateData(
134
                $partialPath,
135
                $data
136
            );
137
138
            return $content ?: null;
139
        }
140
    }
141
}
142
143
if (!function_exists('tgn_view')) {
144
    /**
145
     * Get view template
146
     *
147
     * @param string $partialPath
148
     * @param array $data
149
     *
150
     * @noinspection PhpMissingReturnTypeInspection
151
     */
152
    function tgn_view(string $partialPath, array $data = [])
153
    {
154
        if (class_exists('Illuminate\Foundation\Application')) {
155
            $partialPath = config('telegram-git-notifier.view.namespace') . '::' . $partialPath;
156
        }
157
158
        return view($partialPath, $data);
159
    }
160
}
161