Passed
Push — main ( 8ff36e...0e00e7 )
by Tan
13:44 queued 11:32
created

tgn_view()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 4
c 1
b 1
f 1
dl 0
loc 9
rs 10
cc 3
nc 2
nop 2
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
            '/([ti])a$/i' => '$1um',
38
            '/(n)ews$/i' => '$1ews',
39
            '/(.)s$/i' => '$1',
40
        ];
41
42
        return preg_replace(
43
            array_keys($singular_rules),
44
            array_values($singular_rules),
45
            $word
46
        );
47
    }
48
}
49
50
if (!function_exists('tgn_snake_case')) {
51
    /**
52
     * Convert a string to a snack case
53
     *
54
     * @param string $string
55
     *
56
     * @return string
57
     */
58
    function tgn_snake_case(string $string): string
59
    {
60
        $string = preg_replace('/\s+/', '_', $string);
61
62
        return strtolower($string);
63
    }
64
}
65
66
if (!function_exists('tgn_event_name')) {
67
    /**
68
     * Get event name
69
     *
70
     * @param string $event
71
     *
72
     * @return string
73
     */
74
    function tgn_event_name(string $event): string
75
    {
76
        return tgn_snake_case(str_replace(' Hook', '', $event));
77
    }
78
}
79
80
if (!function_exists('tgn_convert_event_name')) {
81
    /**
82
     * Convert event name
83
     *
84
     * @param string $event
85
     *
86
     * @return string
87
     */
88
    function tgn_convert_event_name(string $event): string
89
    {
90
        return tgn_singularity(tgn_event_name($event));
91
    }
92
}
93
94
if (!function_exists('tgn_convert_action_name')) {
95
    /**
96
     * Convert action name
97
     *
98
     * @param string $action
99
     *
100
     * @return string
101
     */
102
    function tgn_convert_action_name(string $action): string
103
    {
104
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $action));
105
    }
106
}
107
108
if (!class_exists('Illuminate\Foundation\Application')) {
109
    if (!function_exists('config')) {
110
        /**
111
         * Return config value by string
112
         *
113
         * @param string $string
114
         *
115
         * @return mixed
116
         */
117
        function config(string $string): mixed
118
        {
119
            return (new ConfigHelper())->execConfig($string);
120
        }
121
    }
122
123
    if (!function_exists('view')) {
124
        /**
125
         * Get view template
126
         *
127
         * @param string $partialPath
128
         * @param array $data
129
         *
130
         * @return null|string
131
         */
132
        function view(string $partialPath, array $data = []): null|string
133
        {
134
            $content = (new ConfigHelper())->getTemplateData(
135
                $partialPath,
136
                $data
137
            );
138
139
            return $content ?: null;
140
        }
141
    }
142
}
143
144
if (!function_exists('tgn_view')) {
145
    /**
146
     * Get view template
147
     *
148
     * @param string $partialPath
149
     * @param array $data
150
     *
151
     * @return null|string
152
     */
153
    function tgn_view(string $partialPath, array $data = []): null|string
154
    {
155
        if (class_exists('Illuminate\Foundation\Application')) {
156
            $partialPath = config('telegram-git-notifier.view.namespace') . '::' . $partialPath;
157
        }
158
159
        $content = view($partialPath, $data);
160
161
        return $content ?: null;
162
    }
163
}
164