Completed
Push — master ( ff3d5b...77ffdb )
by hook
06:35
created

Builder::invalidOperator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
 
3
namespace Hoooklife\DynamodbPodm\Query;
4
use Hoooklife\DynamodbPodm\DB;
5
use Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar;
6
class Builder{
7
8
    /**
9
     * 查询排序
10
     */
11
    public $limit;
12
    public $where;
13
14
15
    /**
16
     * All of the available clause operators.
17
     *
18
     * @var array
19
     */
20
    public $operators = [
21
        '=', '<', '>', '<=', '>=', '<>', '!='
22
    ];
23
24
    public function __construct($connection = 'default')
25
    {
26
        switch (DB::$config[$connection]['driver']) {
27
            case "dynamedb":
28
               $this->grammar = new DynamoDBGrammar();
0 ignored issues
show
Bug Best Practice introduced by
The property grammar does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
               break;
30
            default:
31
                throw new Exception("bad driver");
0 ignored issues
show
Bug introduced by
The type Hoooklife\DynamodbPodm\Query\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
32
        }        
33
    }
34
35
36
    /**
37
     * 设置要查出的列
38
     *
39
     * @param  array|mixed  $columns
40
     * @return $this
41
     */
42
    public function select($columns = ['*'])
43
    {
44
        $this->columns = is_array($columns) ? $columns : func_get_args();
0 ignored issues
show
Bug Best Practice introduced by
The property columns does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
        return $this;
46
    }
47
48
49
    /**
50
     * Add a new select column to the query.
51
     *
52
     * @param  array|mixed  $column
53
     * @return $this
54
     */
55
    public function addSelect($columns)
56
    {
57
        $columns = is_array($columns) ? $columns : func_get_args();
58
        $this->columns = array_merge((array) $this->columns, $columns);
0 ignored issues
show
Bug Best Practice introduced by
The property columns does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
        return $this;
60
    }
61
62
63
    /**
64
     * Set the table which the query is targeting.
65
     *
66
     * @param  string  $table
67
     * @return $this
68
     */
69
    public function from($table)
70
    {
71
        $this->from = $table;
0 ignored issues
show
Bug Best Practice introduced by
The property from does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
        return $this;
73
    }
74
    
75
76
77
    /**
78
     * Add a basic where clause to the query.
79
     *
80
     * @param  string|array|\Closure  $column
81
     * @param  mixed   $operator
82
     * @param  mixed   $value
83
     * @param  string  $boolean
84
     * @return $this
85
     */
86
87
    public function where($column, $operator = null, $value = null, $boolean = 'and')
88
    {
89
        if (is_array($column)) {
90
            // 递归
91
            foreach ($column as $key => $value) {
92
                 if (is_numeric($key) && is_array($value)) {
93
                    $this->where(...array_values($value));
0 ignored issues
show
Bug introduced by
array_values($value) is expanded, but the parameter $column of Hoooklife\DynamodbPodm\Query\Builder::where() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
                    $this->where(/** @scrutinizer ignore-type */ ...array_values($value));
Loading history...
94
                } else {
95
                    $this->where($key, '=', $value, $boolean);
96
                }
97
            }
98
            return $this;
99
            // return $this->addArrayOfWheres($column, $boolean);
100
        }
101
102
        if(func_num_args() === 2 || $this->invalidOperator($operator)) {
103
            list($value, $operator) = [$operator, '='];
104
        }
105
106
        // where in
107
        if (is_array($value)){
108
            return $this->whereIn($column,$boolean);
0 ignored issues
show
Bug introduced by
The method whereIn() does not exist on Hoooklife\DynamodbPodm\Query\Builder. Did you maybe mean where()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            return $this->/** @scrutinizer ignore-call */ whereIn($column,$boolean);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        }
110
111
        // is null
112
        if (is_null($value)) {
113
            return $this->whereNull($column, $boolean, $operator !== '=');
0 ignored issues
show
Bug introduced by
The method whereNull() does not exist on Hoooklife\DynamodbPodm\Query\Builder. Did you maybe mean where()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            return $this->/** @scrutinizer ignore-call */ whereNull($column, $boolean, $operator !== '=');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        }
115
116
117
        $type = 'Basic';
118
        $this->wheres[] = compact(
0 ignored issues
show
Bug Best Practice introduced by
The property wheres does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
            'type', 'column', 'operator', 'value', 'boolean'
120
        );
121
122
123
        // TODO 参数绑定
124
        // $this->addBinding($value, 'where');
125
126
        return $this;
127
    }
128
129
130
     /**
131
     * Determine if the given operator is supported.
132
     *
133
     * @param  string  $operator
134
     * @return bool
135
     */
136
    protected function invalidOperator($operator)
137
    {
138
        return ! in_array(strtolower($operator), $this->operators, true) &&
139
               ! in_array(strtolower($operator), $this->grammar->getOperators(), true);
140
    }
141
142
143
    // /**
144
    //  * 向查询添加排序语句。
145
    //  *
146
    //  * @param  string  $column
147
    //  * @param  string  $direction
148
    //  * @return $this
149
    //  */
150
    // public function orderBy($columns, $direction = 'asc')
151
    // {
152
    //     $orders = is_array($columns) ? $columns : [$columns => $direction];
153
    //     $this->orders = array_merge($this->orders, $orders);
154
    //     return $this;
155
    // }
156
157
    // public function offset($value)
158
    // {
159
    //     $this->offset =  max(0, $value);
160
    //     return $this;
161
    // }
162
163
    //  /**
164
    //  * Alias to set the "offset" value of the query.
165
    //  *
166
    //  * @param  int  $value
167
    //  * @return \Illuminate\Database\Query\Builder|static
168
    //  */
169
    // public function skip($value)
170
    // {
171
    //     return $this->offset($value);
172
    // }
173
174
175
    /**
176
     * Alias to set the "limit" value of the query.
177
     *
178
     * @param  int  $value
179
     * @return \Illuminate\Database\Query\Builder|static
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Query\Builder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
180
     */
181
    public function take($value)
182
    {
183
        return $this->limit($value);
184
    }
185
186
187
    /**
188
     * Set the "limit" value of the query.
189
     *
190
     * @param  int  $value
191
     * @return $this
192
     */
193
    public function limit($value)
194
    {
195
        //  FIXME ??? 为啥等于0
196
        if ($value > 0) {
197
            $this->limit = $value;
198
        }
199
        return $this;
200
    }
201
202
     /**
203
     * Set the limit and offset for a given page.
204
     *
205
     * @param  int  $page
206
     * @param  int  $perPage
207
     * @return \Illuminate\Database\Query\Builder|static
208
     */
209
    public function forPage($page, $perPage = 15)
210
    {
211
        return $this->skip(($page - 1) * $perPage)->take($perPage);
0 ignored issues
show
Bug introduced by
The method skip() does not exist on Hoooklife\DynamodbPodm\Query\Builder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
        return $this->/** @scrutinizer ignore-call */ skip(($page - 1) * $perPage)->take($perPage);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
212
    }
213
214
215
     /**
216
     * Get the SQL representation of the query.
217
     *
218
     * @return string
219
     */
220
    public function toSql()
221
    {
222
        return $this->grammar->compileSelect($this);
223
    }
224
225
226
    /**
227
     * Get a single column's value from the first result of a query.
228
     *
229
     * @param  string  $column
230
     * @return mixed
231
     */
232
    public function value($column)
233
    {
234
        $result = (array) $this->first([$column]);
235
        return count($result) > 0 ? reset($result) : null;
236
    }
237
238
239
240
    /**
241
     * Execute the query and get the first result.
242
     *
243
     * @param  array  $columns
244
     * @return \Illuminate\Database\Eloquent\Model|object|static|null
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
245
     */
246
    public function first($columns = ['*'])
247
    {
248
        return reset($this->take(1)->all($columns));
0 ignored issues
show
Bug introduced by
$this->take(1)->all($columns) of type Illuminate\Support\Collection is incompatible with the type array expected by parameter $array of reset(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

248
        return reset(/** @scrutinizer ignore-type */ $this->take(1)->all($columns));
Loading history...
249
    }
250
251
252
    /**
253
     * Execute the query as a "select" statement.
254
     *
255
     * @param  array  $columns
256
     * @return \Illuminate\Support\Collection
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
257
     */
258
    public function all($columns = ['*'])
0 ignored issues
show
Unused Code introduced by
The parameter $columns is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

258
    public function all(/** @scrutinizer ignore-unused */ $columns = ['*'])

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

Loading history...
259
    {
260
        return $this->grammar->all();
261
    }
262
263
}
264