Completed
Pull Request — master (#20)
by Daniel
01:54
created

Query::getSelects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Psi\Component\ObjectAgent\Query;
4
5
use Psi\Component\ObjectAgent\Query\Join;
6
7
final class Query
8
{
9
    private $classFqn;
10
    private $expression;
11
    private $orderings;
12
    private $firstResult;
13
    private $maxResults;
14
    private $joins;
15
    private $selects;
16
17
    private function __construct(
18
        string $classFqn,
19
        array $selects = [],
20
        array $joins = [],
21
        Expression $expression = null,
22
        array $orderings = [],
23
        int $firstResult = null,
24
        int $maxResults = null
25
    ) {
26
        $this->classFqn = $classFqn;
27
        $this->expression = $expression;
28
        $this->orderings = $orderings;
29
        $this->firstResult = $firstResult;
30
        $this->maxResults = $maxResults;
31
        $this->joins = $joins;
32
        $this->selects = $selects;
33
34
        array_walk($joins, function (Join $join) {
0 ignored issues
show
Unused Code introduced by
The parameter $join is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
        });
36
    }
37
38
    public static function create(
39
        string $classFqn,
40
        array $query = []
41
    ) {
42
        $defaults = [
43
            'selects' => [],
44
            'criteria' => null,
45
            'orderings' => [],
46
            'joins' => [],
47
            'firstResult' => null,
48
            'maxResults' => null,
49
        ];
50
51 View Code Duplication
        if ($diff = array_diff(array_keys($query), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            throw new \InvalidArgumentException(sprintf(
53
                'Invalid query keys "%s", valid keys: "%s"',
54
                implode('", "', $diff), implode('", "', array_keys($defaults))
55
            ));
56
        }
57
58
        $query = array_merge($defaults, $query);
59
60
        return new self($classFqn, $query['selects'], $query['joins'], $query['criteria'], $query['orderings'], $query['firstResult'], $query['maxResults']);
61
    }
62
63
    public static function comparison(string $comparator, $value1, $value2): Comparison
64
    {
65
        return new Comparison($comparator, $value1, $value2);
66
    }
67
68
    public static function composite(string $type, ...$expressions): Composite
69
    {
70
        return new Composite($type, $expressions);
71
    }
72
73
    public static function join(string $type, string $alias)
74
    {
75
        return new Join($type, $alias);
76
    }
77
78
    public function getClassFqn(): string
79
    {
80
        return $this->classFqn;
81
    }
82
83
    public function hasExpression()
84
    {
85
        return null !== $this->expression;
86
    }
87
88
    public function getExpression(): Expression
89
    {
90
        return $this->expression;
91
    }
92
93
    public function getOrderings(): array
94
    {
95
        return $this->orderings;
96
    }
97
98
    public function getMaxResults()
99
    {
100
        return $this->maxResults;
101
    }
102
103
    public function getFirstResult()
104
    {
105
        return $this->firstResult;
106
    }
107
108
    public function getJoins(): array
109
    {
110
        return $this->joins;
111
    }
112
113
    public function getSelects(): array
114
    {
115
        return $this->selects;
116
    }
117
}
118