Expression::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\MongoDB\QueryBuilder;
4
5
/**
6
 * Class Expression.
7
 */
8
class Expression
9
{
10
    /** @var array */
11
    private $filters;
12
13
    /**
14
     * Expression constructor.
15
     */
16 24
    public function __construct()
17
    {
18 24
        $this->filters = [];
19 24
    }
20
21
    /**
22
     * Adds and filter
23
     *
24
     * @param array|Expression $expression
25
     *
26
     * @return $this|Expression
27
     */
28 View Code Duplication
    public function and($expression): Expression
0 ignored issues
show
Unused Code introduced by
The parameter $expression 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...
Duplication introduced by
This method seems to be duplicated in 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...
29
    {
30 9
        $this->prepareFilterIndex('$and');
31
32 9
        $this->filters['$and'] = array_merge(
33 9
            $this->filters['$and'],
34 9
            $this->mapExpressions(...func_get_args())
35
        );
36
37 9
        return $this;
38
    }
39
40
    /**
41
     * @param string $operator
42
     */
43 19
    private function prepareFilterIndex(string $operator)
44
    {
45 19
        if (!isset($this->filters[$operator])) {
46 19
            $this->filters[$operator] = [];
47
        }
48 19
    }
49
50
    /**
51
     * @param  $expressions
52
     *
53
     * @return array
54
     */
55 10
    private function mapExpressions($expressions): array
0 ignored issues
show
Unused Code introduced by
The parameter $expressions 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...
56
    {
57 10
        return array_map(
58
            function ($expression) {
59 10
                return $expression instanceof Expression ? $expression->getExpressionFilters() : $expression;
60 10
            },
61
            func_get_args()
62
        );
63
    }
64
65
    /**
66
     * Returns the filters generated by the expression
67
     * 
68
     * @return array
69
     */
70 24
    public function getExpressionFilters(): array
71
    {
72 24
        return $this->filters;
73
    }
74
75
    /**
76
     * Adds or filter
77
     * 
78
     * @param array|Expression $expression
79
     *
80
     * @return $this|Expression
81
     */
82 3 View Code Duplication
    public function or ($expression): Expression
0 ignored issues
show
Unused Code introduced by
The parameter $expression 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...
Duplication introduced by
This method seems to be duplicated in 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...
83
    {
84 3
        $this->prepareFilterIndex('$or');
85
86 3
        $this->filters['$or'] = array_merge(
87 3
            $this->filters['$or'],
88 3
            $this->mapExpressions(...func_get_args())
89
        );
90
91 3
        return $this;
92
    }
93
94
    /**
95
     * Adds eq filter
96
     *
97
     * @param string $field
98
     * @param string $value
99
     *
100
     * @return $this|Expression
101
     */
102 2
    public function equal(string $field, $value): Expression
103
    {
104 2
        $this->prepareFilterIndex($field);
105
106 2
        $this->filters[$field] = $this->operationExpression('$eq', $value);
107
108 2
        return $this;
109
    }
110
111
    /**
112
     * Adds ne filter
113
     *
114
     * @param string $field
115
     * @param string $value
116
     *
117
     * @return $this|Expression
118
     */
119 4
    public function notEqual(string $field, $value): Expression
120
    {
121 4
        $this->prepareFilterIndex($field);
122
123 4
        $this->filters[$field] = $this->operationExpression('$ne', $value);
124
125 4
        return $this;
126
    }
127
128
    /**
129
     * Adds in filter
130
     *
131
     * @param string $field
132
     * @param array  $values
133
     *
134
     * @return $this|Expression
135
     */
136 2
    public function in(string $field, array $values): Expression
137
    {
138 2
        $this->prepareFilterIndex($field);
139
140 2
        $this->filters[$field] = $this->operationExpression('$in', $values);
141
142 2
        return $this;
143
    }
144
145
    /**
146
     * Adds notIn filter
147
     *
148
     * @param string $field
149
     * @param array  $values
150
     *
151
     * @return $this|Expression
152
     */
153 2
    public function notIn(string $field, array $values): Expression
154
    {
155 2
        $this->prepareFilterIndex($field);
156
157 2
        $this->filters[$field] = $this->operationExpression('$nin', $values);
158
159 2
        return $this;
160
    }
161
162
163
    /**
164
     * @param string $operation
165
     * @param mixed  $value
166
     *
167
     * @return array
168
     */
169 10
    private function operationExpression(string $operation, $value): array
170
    {
171 10
        return [$operation => $value];
172
    }
173
}
174