Completed
Push — master ( 3f285b...f51ae3 )
by Alessandro
08:34
created

Builder::equal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\QueryBuilder;
4
5
use MongoDB\Collection;
6
7
/**
8
 * Class Builder.
9
 */
10
class Builder
11
{
12
    /** @var Collection */
13
    private $collection;
14
    /** @var Expression  */
15
    private $expression;
16
    /** @var array */
17
    private $options;
18
    /** @var int */
19
    private $queryType;
20
21
    /**
22
     * Builder constructor.
23
     *
24
     * @param Collection $collection
25
     */
26 13
    public function __construct(Collection $collection)
27
    {
28 13
        $this->collection = $collection;
29 13
        $this->queryType = Query::TYPE_FIND;
30 13
        $this->expression = new Expression();
31 13
        $this->options = [];
32 13
    }
33
34
    /**
35
     * @return $this
36
     */
37 3
    public function find()
38
    {
39 3
        return $this->setType(Query::TYPE_FIND);
40
    }
41
42
    /**
43
     * @return $this
44
     */
45 1
    public function count()
46
    {
47 1
        return $this->setType(Query::TYPE_COUNT);
48
    }
49
50
    /**
51
     * @param int $type
52
     *
53
     * @return $this
54
     */
55 4
    protected function setType(int $type)
56
    {
57 4
        $this->queryType = $type;
58
59 4
        return $this;
60
    }
61
62
    /**
63
     * @param array|Expression $expression
64
     *
65
     * @return $this
66
     */
67
    public function and($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...
68
    {
69 5
        $this->expression->and(...func_get_args());
70
71 5
        return $this;
72
    }
73
74
    /**
75
     * @param array|Expression $expression
76
     *
77
     * @return $this
78
     */
79 2
    public function or($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...
80
    {
81 2
        $this->expression->or(...func_get_args());
82
83 2
        return $this;
84
    }
85
86 1
    public function equal(string $field, $expression)
87
    {
88 1
        $this->expression->equal($field, $expression);
89
90 1
        return $this;
91
    }
92
93
    public function notEqual(string $field, $expression)
94
    {
95
        $this->expression->notEqual($field, $expression);
96 13
97
        return $this;
98 13
    }
99 13
100 13
    /**
101 13
     * @return Query
102 13
     */
103
    public function getQuery(): Query
104
    {
105
        return new Query(
106
            $this->collection,
107
            $this->queryType,
108
            $this->expression->getExpressionFilters(),
109 2
            $this->options
110
        );
111 2
    }
112
113
    /**
114
     * @return int
115
     */
116
    public function getQueryType(): int
117
    {
118
        return $this->queryType;
119 1
    }
120
121 1
    /**
122
     * @param array $fields
123
     *
124
     * @return $this
125
     */
126
    public function sort(array $fields)
127
    {
128
        return $this->setQueryOption('sort', $fields);
129 1
    }
130
131 1
    /**
132
     * @param int $limit
133
     *
134
     * @return $this
135
     */
136
    public function limit(int $limit)
137
    {
138
        return $this->setQueryOption('limit', $limit);
139 1
    }
140
141 1
    /**
142
     * @param int $skip
143
     *
144
     * @return $this
145
     */
146
    public function skip(int $skip)
147
    {
148
        return $this->setQueryOption('skip', $skip);
149 1
    }
150
151 1
    /**
152
     * @param array $projection
153 1
     *
154 1
     * @return $this
155
     */
156
    public function select(...$projection)
157 1
    {
158
        $this->setQueryOption('projection', array_fill_keys($projection, 1));
159
160
        if (!in_array('_id', $projection)) {
161
            $this->options['projection']['_id'] = -1;
162
        }
163
164
        return $this;
165
    }
166 5
167
    /**
168 5
     * @param string $option
169
     * @param mixed  $value
170 5
     *
171
     * @return $this
172
     */
173
    public function setQueryOption(string $option, $value)
174
    {
175
        $this->options[$option] = $value;
176 2
177
        return $this;
178 2
    }
179
180
    /**
181
     * @return Expression
182
     */
183
    public function expr(): Expression
184
    {
185
        return new Expression();
186
    }
187
}