RegexChecker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 89
ccs 27
cts 27
cp 1
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getArrayValue() 0 7 2
A __construct() 0 4 1
A getEntityValue() 0 9 3
A getObjectValue() 0 11 4
A check() 0 11 4
1
<?php
2
3
namespace BlockListCheck\Checkers;
4
5
use BlockListCheck\Contracts\BlocklistChecker;
6
use BlockListCheck\Exceptions\BlockListCheckException;
7
8
class RegexChecker implements BlocklistChecker
9
{
10
    use HasPatterns, HasFields;
11
12
    /**
13
     * @param array $patterns
14
     * @param array $fields
15
     */
16 14
    public function __construct(array $patterns = [], array $fields = [])
17
    {
18 14
        $this->patterns = $patterns;
19 14
        $this->fields   = $fields;
20
    }
21
22
23
    /**
24
     * @inheritDoc
25
     */
26 10
    public function check(mixed $entity): bool
27
    {
28 10
        foreach ($this->fields as $field) {
29 10
            foreach ($this->patterns as $pattern) {
30 10
                if (preg_match($pattern, (string) $this->getEntityValue($entity, $field))) {
31 5
                    return false;
32
                }
33
            }
34
        }
35
36 5
        return true;
37
    }
38
39
    /**
40
     * Get field value from entity.
41
     *
42
     * @param mixed $entity
43
     * @param string $field
44
     *
45
     * @return mixed
46
     * @throws BlockListCheckException
47
     */
48 10
    protected function getEntityValue(mixed $entity, string $field): mixed
49
    {
50 10
        if (is_array($entity)) {
51 3
            return $this->getArrayValue($entity, $field);
52 7
        } elseif (is_object($entity)) {
53 6
            return $this->getObjectValue($entity, $field);
54
        }
55
56 1
        throw new BlockListCheckException("Field [$field] value not found.");
57
    }
58
59
    /**
60
     * Get field value from array.
61
     *
62
     * @param mixed $entity
63
     * @param string $field
64
     *
65
     * @return mixed
66
     * @throws BlockListCheckException
67
     */
68 3
    protected function getArrayValue(array $entity, string $field): mixed
69
    {
70 3
        if (array_key_exists($field, $entity)) {
71 3
            return $entity[ $field ];
72
        }
73
74 1
        throw new BlockListCheckException("Field [$field] value not found.");
75
    }
76
77
    /**
78
     * Get field value from object.
79
     *
80
     * @param object $entity
81
     * @param string $field
82
     *
83
     * @return mixed
84
     * @throws BlockListCheckException
85
     */
86 6
    protected function getObjectValue(object $entity, string $field): mixed
87
    {
88 6
        if (property_exists($entity, $field)) {
89 2
            return $entity->{$field};
90 5
        } elseif (method_exists($entity, 'blocklistCheckValue')) {
91 2
            return $entity->blocklistCheckValue($field);
92 3
        } elseif (method_exists($entity, 'getAttribute')) {
93 2
            return $entity->getAttribute($field);
94
        }
95
96 1
        throw new BlockListCheckException("Field [$field] value not found.");
97
    }
98
}
99