Passed
Push — main ( 909801...83bea6 )
by Thierry
06:09
created

UtilTrait::repeatPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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 Input
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(): Input
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
    /**
77
     * Create repeat pattern for preg
78
     *
79
     * @param string $pattern
80
     * @param int $length
81
     *
82
     * @return string
83
     */
84
    public function repeatPattern(string $pattern, int $length): string
85
    {
86
        // fix for Compilation failed: number too big in {} quantifier
87
        // can create {0,0} which is OK
88
        return str_repeat("$pattern{0,65535}", $length / 65535) . "$pattern{0," . ($length % 65535) . '}';
89
    }
90
91
    /**
92
     * Shorten UTF-8 string
93
     *
94
     * @param string $string
95
     * @param int $length
96
     * @param string $suffix
97
     *
98
     * @return string
99
     */
100
    public function shortenUtf8(string $string, int $length = 80, string $suffix = ''): string
101
    {
102
        if (!preg_match('(^(' . $this->repeatPattern("[\t\r\n -\x{10FFFF}]", $length) . ')($)?)u', $string, $match)) {
103
            // ~s causes trash in $match[2] under some PHP versions, (.|\n) is slow
104
            preg_match('(^(' . $this->repeatPattern("[\t\r\n -~]", $length) . ')($)?)', $string, $match);
105
        }
106
        return $this->html($match[1]) . $suffix . (isset($match[2]) ? '' : '<i>…</i>');
107
    }
108
}
109