Configuration   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 59
ccs 15
cts 15
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrder() 0 12 2
B setFilter() 0 19 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Atlance\HttpDbalFilter\Query;
6
7
use Atlance\HttpDbalFilter\Dto\AbstractCommand;
8
use Atlance\HttpDbalFilter\Utils\JsonNormalizer;
9
use Webmozart\Assert\Assert;
10
11
final class Configuration extends AbstractCommand
12
{
13
    public int $page;
14
15
    public int $limit;
16
17
    /**
18
     * @var array<string,array<string,array<int,string|int|float|\Stringable>>>
19
     */
20
    public array $filter = [];
21
22
    /**
23
     * @var array<string,string>
24
     */
25
    public array $order = [];
26
27
    /**
28
     * @psalm-suppress MixedArrayAssignment
29
     * @psalm-suppress MixedPropertyTypeCoercion
30
     *
31
     * @param non-empty-array<string,array<string,string|int|float>> $conditions
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-array<string,a...ring,string|int|float>> at position 0 could not be parsed: Unknown type name 'non-empty-array' at position 0 in non-empty-array<string,array<string,string|int|float>>.
Loading history...
32
     *
33
     * @return Configuration
34
     *
35
     * @throws \JsonException
36
     */
37 73
    public function setFilter(array $conditions): self
38
    {
39 73
        foreach ($conditions as $expr => $aliases) {
40 73
            Assert::oneOf($expr, Builder::SUPPORTED_EXPRESSIONS);
41
42 72
            foreach ($aliases as $alias => $values) {
43 72
                if (!\array_key_exists($expr, $this->filter)) {
44 72
                    $this->filter[$expr] = [];
45
                }
46
47 72
                if (\is_string($values) && (bool) preg_match('#\|#', $values)) {
48 29
                    $values = JsonNormalizer::normalize(explode('|', $values));
49
                }
50
51 72
                $this->filter[$expr][$alias] = \is_array($values) ? $values : [$values];
52
            }
53
        }
54
55 72
        return $this;
56
    }
57
58 2
    public function setOrder(array $conditions): self
59
    {
60
        /**
61
         * @var string $alias
62
         * @var string $direction
63
         */
64 2
        foreach ($conditions as $alias => $direction) {
65 2
            Assert::oneOf($direction, ['asc', 'desc']);
66 1
            $this->order[$alias] = $direction;
67
        }
68
69 1
        return $this;
70
    }
71
}
72