Passed
Push — main ( c3cb08...0cda90 )
by Martin
11:26
created

BooleanValidationTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 24
ccs 8
cts 9
cp 0.8889
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidBoolean() 0 3 1
A validateBoolean() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits;
6
7
use Doctrine\ORM\Query\AST\Literal;
8
use Doctrine\ORM\Query\AST\Node;
9
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidBooleanException;
10
11
/**
12
 * Provides boolean validation functionality for functions that accept boolean parameters.
13
 *
14
 * @since 3.1
15
 *
16
 * @author Martin Georgiev <[email protected]>
17
 */
18
trait BooleanValidationTrait
19
{
20
    /**
21
     * Validates that the given node represents a valid boolean value.
22
     *
23
     * @throws InvalidBooleanException If the value is not a valid boolean
24
     */
25 10
    protected function validateBoolean(Node $node, string $functionName): void
26
    {
27 10
        if (!$node instanceof Literal || !\is_string($node->value)) {
28
            throw InvalidBooleanException::forNonLiteralNode($node::class, $functionName);
29
        }
30
31 10
        $value = \strtolower(\trim((string) $node->value, "'\""));
32 10
        $lowercaseValue = \strtolower($value);
33
34 10
        if (!$this->isValidBoolean($lowercaseValue)) {
35 5
            throw InvalidBooleanException::forInvalidBoolean($value, $functionName);
36
        }
37
    }
38
39 10
    private function isValidBoolean(string $boolean): bool
40
    {
41 10
        return \in_array($boolean, ['true', 'false'], true);
42
    }
43
}
44