Passed
Push — main ( 6044d0...c28500 )
by Paul
09:22
created

Sanitizer::buildSanitizers()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 9
c 2
b 0
f 2
dl 0
loc 14
ccs 8
cts 10
cp 0.8
rs 9.9666
cc 4
nc 3
nop 1
crap 4.128
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Modules\Sanitizers\SanitizeCompat;
9
use GeminiLabs\SiteReviews\Modules\Sanitizers\SanitizeText;
10
11
class Sanitizer
12
{
13
    /**
14
     * @var array
15
     */
16
    public $sanitizers;
17
18
    /**
19
     * @var array
20
     */
21
    public $values;
22
23 68
    public function __construct(array $values = [], array $sanitizers = [])
24
    {
25 68
        $this->sanitizers = $this->buildSanitizers($sanitizers);
26 68
        $this->values = $values;
27
    }
28
29 27
    public function __call(string $method, array $args)
30
    {
31 27
        $value = array_shift($args);
32
        // @todo remove in v7.0.0
33 27
        $name = lcfirst(Str::removePrefix($method, 'sanitize'));
34 27
        if (in_array($name, ['array', 'bool', 'int'])) {
35
            return (new SanitizeCompat($value, $name, $this->values))->run();
36
        }
37 27
        $classname = Helper::buildClassName($method, 'Modules\Sanitizers');
38 27
        if (class_exists($classname)) {
39 27
            return (new $classname($value, $args, $this->values))->run();
40
        }
41
        glsr_log()->error("Sanitizer method [$method] not found.");
42
        return array_shift($args);
43
    }
44
45 68
    public function run(): array
46
    {
47 68
        $results = $this->values;
48 68
        foreach ($this->values as $key => $value) {
49 68
            if (!array_key_exists($key, $this->sanitizers)) {
50 35
                continue;
51
            }
52 68
            foreach ($this->sanitizers[$key] as $sanitizer) {
53 68
                $args = $sanitizer['args'];
54 68
                $classname = $sanitizer['sanitizer'];
55 68
                $value = (new $classname($value, $args, $this->values))->run();
56
            }
57 68
            $results[$key] = $value;
58
        }
59 68
        return $results;
60
    }
61
62 68
    protected function buildSanitizer(string $sanitizer): array
63
    {
64 68
        $parts = preg_split('/:/', $sanitizer, 2, PREG_SPLIT_NO_EMPTY);
65 68
        $name = trim(Arr::get($parts, 0));
66 68
        $args = trim(Arr::get($parts, 1));
67 68
        $args = 'regex' === $name ? [$args] : explode(',', $args);
68 68
        $classname = Helper::buildClassName('sanitize-'.$name, 'Modules\Sanitizers');
69 68
        if (!class_exists($classname)) {
70
            $classname = SanitizeText::class;
71
        }
72 68
        if (in_array($name, ['array', 'bool', 'int'])) { // @todo remove in v7.0.0
73
            $args = $name;
74
            $classname = SanitizeCompat::class;
75
        }
76 68
        return [
77 68
            'args' => $args,
78 68
            'sanitizer' => $classname,
79 68
        ];
80
    }
81
82 68
    protected function buildSanitizers(array $sanitizers): array
83
    {
84 68
        foreach ($sanitizers as $key => $value) {
85 68
            $methods = Arr::consolidate(preg_split('/\|/', $value, -1, PREG_SPLIT_NO_EMPTY));
86 68
            $sanitizers[$key] = [];
87 68
            if (empty($methods)) {
88
                $sanitizers[$key][] = $this->buildSanitizer('text');
89
                continue;
90
            }
91 68
            foreach ($methods as $sanitizer) {
92 68
                $sanitizers[$key][] = $this->buildSanitizer($sanitizer);
93
            }
94
        }
95 68
        return $sanitizers;
96
    }
97
}
98