Passed
Push — main ( ecdc1f...dbd268 )
by Thierry
01:34
created

UtilTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isUtf8() 0 4 2
A number() 0 3 1
A html() 0 7 2
A setDriver() 0 3 1
A input() 0 3 1
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver;
4
5
use function str_replace;
6
use function preg_replace;
7
use function preg_match;
8
use function htmlspecialchars;
9
10
trait UtilTrait
11
{
12
    /**
13
     * @var DriverInterface
14
     */
15
    public $driver;
16
17
    /**
18
     * @var TranslatorInterface
19
     */
20
    protected $trans;
21
22
    /**
23
     * @var InputInterface
24
     */
25
    public $input;
26
27
    /**
28
     * Set the driver
29
     *
30
     * @param DriverInterface $driver
31
     *
32
     * @return void
33
     */
34
    public function setDriver(DriverInterface $driver)
35
    {
36
        $this->driver = $driver;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function input(): InputInterface
43
    {
44
        return $this->input;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function html($string): string
51
    {
52
        if(!$string) {
53
            return '';
54
        }
55
        $string =  str_replace("\n", '<br>', $string);
56
        return str_replace("\0", '&#0;', htmlspecialchars($string, ENT_QUOTES, 'utf-8'));
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function number(string $value): string
63
    {
64
        return preg_replace('~[^0-9]+~', '', $value);
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function isUtf8(string $value): bool
71
    {
72
        // don't print control chars except \t\r\n
73
        return (preg_match('~~u', $value) && !preg_match('~[\0-\x8\xB\xC\xE-\x1F]~', $value));
74
    }
75
}
76