Passed
Pull Request — master (#5)
by Alex
02:16
created

IsEntityNoMatchValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 35
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Validator;
6
7
use Laminas\Validator\Exception\RuntimeException;
8
9
final class IsEntityNoMatchValidator extends AbstractEntityValidator
10
{
11
    public const IS_MATCH = 'is_match';
12
13
    /**
14
     * @var array<string, string>
15
     */
16
    protected array $messageTemplates = [
17
        self::CRITERIA_EMPTY => 'No criteria was provided',
18
        self::IS_MATCH => 'Value matched an existing entity',
19
    ];
20
21
    /**
22
     * @param mixed $value
23
     * @param array<string, mixed> $context
24
     *
25
     * @throws RuntimeException
26
     */
27
    public function isValid(mixed $value, array $context = []): bool
28
    {
29
        $criteria = $this->getMatchCriteria($value, $context);
30
        if (empty($criteria)) {
31
            $this->error(self::CRITERIA_EMPTY);
32
            return false;
33
        }
34
35
        // We would like to ensure that the criteria will NOT result in an entity being matched
36
        $match = $this->getMatchedEntity($criteria);
37
        if (null === $match) {
38
            return true;
39
        }
40
41
        // We matched at least one entity with the provided criteria
42
        $this->error(self::IS_MATCH, $criteria);
43
        return false;
44
    }
45
}
46