Completed
Push — master ( b5cf4d...16b067 )
by Mr
02:05
created

Cleaner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileRegexp() 0 3 1
B run() 0 23 4
A fixQuotes() 0 3 1
1
<?php
2
3
namespace DrMVC\Helpers;
4
5
/**
6
 * Class Cleaner
7
 * @package DrMVC\Helpers
8
 */
9
class Cleaner
10
{
11
12
    const INT = '0-9';
13
    const FLOAT = self::INT . '\,\.';
14
    const CHARS_RUS = 'а-яё';
15
    const CHARS_ENG = 'a-z';
16
    const SUPER = '\*\#\ \n\r';
17
18
    private static function fixQuotes($value): string
19
    {
20
        return htmlspecialchars(addslashes($value), ENT_QUOTES);
21
    }
22
23
    private static function compileRegexp($line): string
24
    {
25
        return '#[^' . $line . ']#iu';
26
    }
27
28
    /**
29
     * Cleanup the value
30
     *
31
     * @param   string $value
32
     * @param   string $type
33
     * @return  mixed
34
     */
35
    public static function run(string $value, string $type = null)
36
    {
37
        switch ($type) {
38
            case 'int':
39
                $regexp = self::compileRegexp(self::INT);
40
                break;
41
            case 'float':
42
                $regexp = self::compileRegexp(self::FLOAT);
43
                break;
44
            case 'text':
45
                $value = self::fixQuotes($value);
46
                $regexp = self::compileRegexp(self::CHARS_RUS . self::CHARS_ENG);
47
                break;
48
            default:
49
                $value = self::fixQuotes($value);
50
                $regexp = self::compileRegexp(
51
                    self::CHARS_RUS . self::CHARS_ENG . self::FLOAT .
52
                    '—~`@%[]/:<>;?&()_!$^-+=' . self::SUPER
53
                );
54
                break;
55
        }
56
57
        return preg_replace($regexp, '', $value);
58
    }
59
60
}
61