Completed
Push — master ( cf38d9...2ecc50 )
by Rob
01:50
created

Utf7::callback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 12
cp 0
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace devtoolboxuk\soteria\voku\Resources;
4
5
class Utf7 extends Resources
6
{
7
8 6
    public function repack($str)
9
    {
10 6
        return (string)preg_replace_callback(
11 6
            '#\+([\\p{L}0-9]+)\-#ui',
12
            function ($matches) {
13
                return $this->callback($matches);
14 6
            },
15 6
            $str
16
        );
17
    }
18
19
20
    private function callback(array $strings)
21
    {
22
        $strTmp = base64_decode($strings[1], true);
23
24
        if ($strTmp === false) {
25
            return $strings[0];
26
        }
27
28
        if (rtrim(base64_encode($strTmp), '=') !== rtrim($strings[1], '=')) {
29
            return $strings[0];
30
        }
31
32
        $string = (string)preg_replace_callback(
33
            '/^((?:\x00.)*?)((?:[^\x00].)+)/us',
34
            function ($matches) {
35
                return $matches[1] . '+' . rtrim(base64_encode($matches[2]), '=') . '-';
36
            },
37
            $strTmp
38
        );
39
40
        return (string)preg_replace('/\x00(.)/us', '$1', $string);
41
    }
42
43
}