ConditionGroup::conditions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace mav3rick177\RapidPagination\Base\Query;
4
5
use mav3rick177\RapidPagination\Base\Contracts\Cursor;
6
use mav3rick177\RapidPagination\Base\ArrayCursor;
7
use mav3rick177\RapidPagination\Base\Exceptions\Query\CursorParameterException;
8
9
/**
10
 * Class ConditionGroup
11
 *
12
 * Conditions are concatenated with "AND".
13
 */
14
class ConditionGroup implements \IteratorAggregate
15
{
16
    /**
17
     * @var Condition[]
18
     */
19
    protected $conditions = [];
20
21
    /**
22
     * @param  Order[]               $orders
23
     * @param  Cursor|int[]|string[] $cursor
24
     * @param  Direction             $direction
25
     * @param  bool                  $exclusive
26
     * @param  bool                  $isSupportQuery
27
     * @return static[]
28
     */
29 10
    public static function createMany(array $orders, $cursor, Direction $direction, $exclusive, $isSupportQuery = false)
30
    {
31 10
        $groups = [];
32 10
        $count = count($orders);
33 10
        for ($i = 0; $i < $count; ++$i) {
34
            /*
35
             * Slice orders for building conditions.
36
             *
37
             * e.g.
38
             *
39
             *    1st:  updated_at = ? AND created_at = ? AND id > ?
40
             *    2nd:  updated_at = ? AND created_at > ?
41
             *    3rd:  updated_at > ?
42
             */
43 10
            $groups[] = static::create(
44 10
                array_slice($orders, 0, $count - $i),
45
                $cursor,
46
                $direction,
47
                $exclusive,
48 10
                $i === 0, // First row has a primary key
49
                $isSupportQuery
50
            );
51
        }
52 10
        return $groups;
53
    }
54
55
    /**
56
     * @param  Order[]               $orders
57
     * @param  Cursor|int[]|string[] $cursor
58
     * @param  Direction             $direction
59
     * @param  bool                  $exclusive
60
     * @param  bool                  $hasPrimaryKey
61
     * @param  bool                  $isSupportQuery
62
     * @return static
63
     */
64 10
    public static function create(array $orders, $cursor, Direction $direction, $exclusive, $hasPrimaryKey, $isSupportQuery = false)
65
    {
66 10
        $conditions = [];
67 10
        $i = 0;
68 10
        $count = count($orders);
69 10
        $cursor = $cursor instanceof Cursor ? $cursor : new ArrayCursor($cursor);
70 10
        foreach ($orders as $order) {
71 10
            if (!$cursor->has($order->column())) {
72
                // All parameters must be specified.
73
                throw new CursorParameterException("Missing cursor parameter: {$order->column()}", $order->column());
74
            }
75 10
            $isLastKey = ++$i === $count;
76 10
            $conditions[] = Condition::create(
77 10
                $order,
78 10
                $cursor->get($order->column()),
79
                $direction,
80
                $exclusive,
81 10
                $isLastKey && $hasPrimaryKey, // When it is the last key and we have a primary key, it is also the primary key.
82
                $isLastKey,
83
                $isSupportQuery
84
            );
85
        }
86 10
        return new static($conditions);
87
    }
88
89
    /**
90
     * Group constructor.
91
     *
92
     * @param Condition[] $conditions
93
     */
94 10
    public function __construct(array $conditions)
95
    {
96 10
        $this->conditions = static::validate(...$conditions);
0 ignored issues
show
Documentation Bug introduced by
It seems like static::validate(...$conditions) of type array<integer,array<inte...Base\Query\Condition>>> is incompatible with the declared type array<integer,object<mav...\Base\Query\Condition>> of property $conditions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
    }
98
99
    /**
100
     * @param  Condition[] $conditions
101
     * @return Condition[]
102
     */
103 10
    protected static function validate(Condition ...$conditions)
104
    {
105 10
        return $conditions;
106
    }
107
108
    /**
109
     * @return Condition[]
110
     */
111
    public function conditions()
112
    {
113
        return $this->conditions;
114
    }
115
116
    /**
117
     * @return static
118
     */
119 10
    public function inverse()
120
    {
121 10
        return new static(array_map(static function (Condition $condition) {
122 10
            return $condition->inverse();
123 10
        }, $this->conditions));
124
    }
125
126
    /**
127
     * Clone Group.
128
     */
129
    public function __clone()
130
    {
131
        $this->conditions = array_map(static function (Condition $condition) {
132
            return clone $condition;
133
        }, $this->conditions);
134
    }
135
136
    /**
137
     * Retrieve an external iterator.
138
     *
139
     * @return \ArrayIterator|Condition[]
140
     */
141 10
    public function getIterator()
142
    {
143 10
        return new \ArrayIterator($this->conditions);
144
    }
145
}
146