Passed
Push — master ( b380a0...d69ac1 )
by Dāvis
05:52
created

Helper   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 172
rs 9
c 0
b 0
f 0
wmc 35

14 Methods

Rating   Name   Duplication   Size   Complexity  
A removeDuplicates() 0 3 1
B initialize() 0 22 4
A swap() 0 5 1
A translit4() 0 3 1
B isEmpty() 0 13 5
A toCamelCase() 0 7 2
A fromCamelCase() 0 3 1
B useHttps() 0 14 9
A cleanText() 0 3 1
A getSchema() 0 7 2
A multiple() 0 9 3
A oneSpace() 0 3 1
A multiset() 0 9 3
A translit2() 0 3 1
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class Helper
11
{
12
    // @formatter:off
13
    /**
14
     * Cyrillic mapping.
15
     *
16
     * @var array
17
     */
18
    protected static $cyrMap = array(
19
        'е', 'ё', 'ж', 'х', 'ц', 'ч', 'ш', 'щ', 'ю', 'я',
20
        'Е', 'Ё', 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ю', 'Я',
21
        'а', 'б', 'в', 'г', 'д', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'ъ', 'ы', 'ь', 'э',
22
        'А', 'Б', 'В', 'Г', 'Д', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Ъ', 'Ы', 'Ь', 'Э'
23
    );
24
25
    /**
26
     * Latin mapping.
27
     *
28
     * @var array
29
     */
30
    protected static $latMap = array(
31
        'ye', 'ye', 'zh', 'kh', 'ts', 'ch', 'sh', 'shch', 'yu', 'ya',
32
        'Ye', 'Ye', 'Zh', 'Kh', 'Ts', 'Ch', 'Sh', 'Shch', 'Yu', 'Ya',
33
        'a', 'b', 'v', 'g', 'd', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ʺ', 'y', '–', 'e',
34
        'A', 'B', 'V', 'G', 'D', 'Z', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'ʺ', 'Y', '–', 'E'
35
    );
36
    // @formatter:on
37
38
    public static function toCamelCase($string, $upFirst = true)
39
    {
40
        if ($upFirst) {
41
            return preg_replace('~\s+~', '', lcfirst(ucwords(str_replace('_', ' ', $string))));
42
        }
43
44
        return preg_replace('~\s+~', '', ucwords(str_replace('_', ' ', $string)));
45
    }
46
47
    public static function fromCamelCase($string, $separator = '_')
48
    {
49
        return strtolower(preg_replace('/(?!^)[[:upper:]]+/', $separator.'$0', $string));
50
    }
51
52
    public static function isEmpty($variable)
53
    {
54
        $result = true;
55
56
        if (\is_array($variable) && \count($variable) > 0) {
57
            foreach ($variable as $value) {
58
                $result = $result && self::isEmpty($value);
59
            }
60
        } else {
61
            $result = empty($variable);
62
        }
63
64
        return $result;
65
    }
66
67
    public static function swap(&$foo, &$bar)
68
    {
69
        $tmp = $foo;
70
        $foo = $bar;
71
        $bar = $tmp;
72
    }
73
74
    public static function removeDuplicates(&$array)
75
    {
76
        $array = array_map('unserialize', array_unique(array_map('serialize', $array)));
77
    }
78
79
    public static function cleanText($text)
80
    {
81
        return html_entity_decode(self::oneSpace(str_replace(' ?', '', mb_convert_encoding(strip_tags($text), 'UTF-8', 'UTF-8'))));
82
    }
83
84
    public static function oneSpace($text)
85
    {
86
        return preg_replace('/\s+/S', ' ', $text);
87
    }
88
89
    /**
90
     * Transliterates cyrillic text to latin.
91
     *
92
     * @param  string $text cyrillic text
93
     *
94
     * @return string latin text
95
     */
96
    public static function translit2($text)
97
    {
98
        return str_replace(self::$cyrMap, self::$latMap, $text);
99
    }
100
101
    /**
102
     * Transliterates latin text to cyrillic.
103
     *
104
     * @param  string $text latin text
105
     *
106
     * @return string cyrillic text
107
     */
108
    public static function translit4($text)
109
    {
110
        return str_replace(self::$latMap, self::$cyrMap, $text);
111
    }
112
113
    public static function multiple(array $keys)
114
    {
115
        foreach ($keys as $key) {
116
            if (!\is_array($key)) {
117
                return false;
118
            }
119
        }
120
121
        return true;
122
    }
123
124
    public static function multiset(array $keys)
125
    {
126
        foreach ($keys as $key) {
127
            if ($key === null) {
128
                return false;
129
            }
130
        }
131
132
        return true;
133
    }
134
135
    public static function useHttps(Request $request)
136
    {
137
        $https = false;
138
        if ($request->server->has('HTTPS') && 'on' === $request->server->get('HTTPS')) {
139
            $https = true;
140
        } elseif ($request->server->has('SERVER_PORT') && 443 === (int)$request->server->get('SERVER_PORT')) {
141
            $https = true;
142
        } elseif ($request->server->has('HTTP_X_FORWARDED_SSL') && 'on' === $request->server->get('HTTP_X_FORWARDED_SSL')) {
143
            $https = true;
144
        } elseif ($request->server->has('HTTP_X_FORWARDED_PROTO') && 'https' === $request->server->get('HTTP_X_FORWARDED_PROTO')) {
145
            $https = true;
146
        }
147
148
        return $https;
149
    }
150
151
    public static function getSchema(Request $request)
152
    {
153
        if (self::useHttps($request)) {
154
            return 'https://';
155
        }
156
157
        return 'http://';
158
    }
159
160
    public static function initialize(array $arguments, ContainerAwareCommand $command)
161
    {
162
        list($params, $input, $output) = $arguments;
163
        /** @var InputInterface $input */
164
        /** @var OutputInterface $output */
165
166
        foreach ($params as $param => $check) {
167
            $command->{$param} = $input->getOption($param);
168
            if (!$command->{$param}) {
169
                $output->writeln("Please provide --$param parameter!");
170
171
                return false;
172
            }
173
174
            if (!$check($command->{$param})) {
175
                $output->writeln("Incorrect input in --$param parameter!");
176
177
                return false;
178
            }
179
        }
180
181
        return true;
182
    }
183
}
184