Completed
Push — master ( b23853...6c4ac7 )
by hook
05:18 queued 02:51
created

Builder   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 266
rs 10
c 0
b 0
f 0
wmc 28

14 Methods

Rating   Name   Duplication   Size   Complexity  
A limit() 0 7 2
A forPage() 0 3 1
A take() 0 3 1
A from() 0 4 1
A toSql() 0 3 1
B where() 0 39 9
A first() 0 3 1
A select() 0 4 2
A addSelect() 0 5 2
A invalidOperator() 0 4 2
A __construct() 0 8 2
A insert() 0 3 1
A value() 0 4 2
A all() 0 3 1
1
<?php
2
3
namespace Hoooklife\DynamodbPodm\Query;
4
5
use Hoooklife\DynamodbPodm\Collection;
6
use Hoooklife\DynamodbPodm\DB;
7
use Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar;
8
9
class Builder
10
{
11
12
    /**
13
     * 查询排序
14
     */
15
    public $limit;
16
    public $wheres = [];
17
    public $bindings = [];
18
    public $columns;
19
    public $table;
20
21
22
    /**
23
     * All of the available clause operators.
24
     *
25
     * @var array
26
     */
27
    public $operators = [
28
        '=', '<', '>', '<=', '>=', '<>', '!='
29
    ];
30
31
    public $reservedKey = [
32
        'key'
33
    ];
34
35
    /** @var DynamoDBGrammar */
36
    private $grammar;
37
38
    public function __construct($connection = 'default')
39
    {
40
        switch (DB::$config[$connection]['driver']) {
41
            case "dynamedb":
42
                $this->grammar = new DynamoDBGrammar($this, DB::$config[$connection]);
43
                break;
44
            default:
45
                throw new \Exception("bad driver");
46
        }
47
    }
48
49
50
    /**
51
     * 设置要查出的列
52
     *
53
     * @param  array|mixed $columns
54
     * @return $this
55
     */
56
    public function select($columns = ['*'])
57
    {
58
        $this->columns = is_array($columns) ? $columns : func_get_args();
59
        return $this;
60
    }
61
62
63
    public function addSelect($columns)
64
    {
65
        $columns = is_array($columns) ? $columns : func_get_args();
66
        $this->columns = array_merge((array)$this->columns, $columns);
67
        return $this;
68
    }
69
70
71
    /**
72
     * Set the table which the query is targeting.
73
     *
74
     * @param  string $table
75
     * @return $this
76
     */
77
    public function from($table)
78
    {
79
        $this->table = $table;
80
        return $this;
81
    }
82
83
84
    /**
85
     * Add a basic where clause to the query.
86
     *
87
     * @param  string|array|\Closure $column
88
     * @param  mixed $operator
89
     * @param  mixed $value
90
     * @param  string $boolean
91
     * @return $this
92
     */
93
94
    public function where($column, $operator = null, $value = null, $boolean = 'and')
95
    {
96
        if (is_array($column)) {
97
            // 递归
98
            foreach ($column as $key => $value) {
99
                if (is_numeric($key) && is_array($value)) {
100
                    $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

100
                    $this->where(/** @scrutinizer ignore-type */ ...array_values($value));
Loading history...
101
                } else {
102
                    $this->where($key, '=', $value, $boolean);
103
                }
104
            }
105
            return $this;
106
            // return $this->addArrayOfWheres($column, $boolean);
107
        }
108
109
        if (func_num_args() === 2 || $this->invalidOperator($operator)) {
110
            list($value, $operator) = [$operator, '='];
111
        }
112
113
        // where in
114
        if (is_array($value)) {
115
            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

115
            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...
116
        }
117
118
        // is null
119
        if (is_null($value)) {
120
            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

120
            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...
121
        }
122
123
        $type = 'Basic';
124
        $this->wheres[] = compact(
125
            'type', 'column', 'operator', 'value', 'boolean'
126
        );
127
128
129
        // TODO 参数绑定
130
//         $this->addBinding($value, 'where');
131
132
        return $this;
133
    }
134
135
136
    /**
137
     * Determine if the given operator is supported.
138
     *
139
     * @param  string $operator
140
     * @return bool
141
     */
142
    protected function invalidOperator($operator)
143
    {
144
        return !in_array(strtolower($operator), $this->operators, true) &&
145
            !in_array(strtolower($operator), $this->grammar->getOperators(), true);
146
    }
147
148
149
    // /**
150
    //  * 向查询添加排序语句。
151
    //  *
152
    //  * @param  string  $column
153
    //  * @param  string  $direction
154
    //  * @return $this
155
    //  */
156
    // public function orderBy($columns, $direction = 'asc')
157
    // {
158
    //     $orders = is_array($columns) ? $columns : [$columns => $direction];
159
    //     $this->orders = array_merge($this->orders, $orders);
160
    //     return $this;
161
    // }
162
163
    // public function offset($value)
164
    // {
165
    //     $this->offset =  max(0, $value);
166
    //     return $this;
167
    // }
168
169
    //  /**
170
    //  * Alias to set the "offset" value of the query.
171
    //  *
172
    //  * @param  int  $value
173
    //  * @return \Illuminate\Database\Query\Builder|static
174
    //  */
175
    // public function skip($value)
176
    // {
177
    //     return $this->offset($value);
178
    // }
179
180
181
    /**
182
     * Alias to set the "limit" value of the query.
183
     *
184
     * @param  int $value
185
     * @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...
186
     */
187
    public function take($value)
188
    {
189
        return $this->limit($value);
190
    }
191
192
193
    /**
194
     * Set the "limit" value of the query.
195
     *
196
     * @param  int $value
197
     * @return $this
198
     */
199
    public function limit($value)
200
    {
201
        //  FIXME ??? 为啥等于0
202
        if ($value > 0) {
203
            $this->limit = $value;
204
        }
205
        return $this;
206
    }
207
208
    /**
209
     * Set the limit and offset for a given page.
210
     *
211
     * @param  int $page
212
     * @param  int $perPage
213
     * @return \Illuminate\Database\Query\Builder|static
214
     */
215
    public function forPage($page, $perPage = 15)
216
    {
217
        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

217
        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...
218
    }
219
220
221
    /**
222
     * Get the SQL representation of the query.
223
     *
224
     * @return string
225
     */
226
    public function toSql()
227
    {
228
        return $this->grammar->compileSelect($this);
0 ignored issues
show
Bug introduced by
The method compileSelect() does not exist on Hoooklife\DynamodbPodm\Grammars\DynamoDBGrammar. ( Ignorable by Annotation )

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

228
        return $this->grammar->/** @scrutinizer ignore-call */ compileSelect($this);

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...
229
    }
230
231
232
    /**
233
     * Get a single column's value from the first result of a query.
234
     *
235
     * @param  string $column
236
     * @return mixed
237
     */
238
    public function value($column)
239
    {
240
        $result = (array)$this->first([$column]);
241
        return count($result) > 0 ? reset($result) : null;
242
    }
243
244
245
    /**
246
     * Execute the query and get the first result.
247
     *
248
     * @param  array $columns
249
     * @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...
250
     */
251
    public function first($columns = ['*'])
252
    {
253
        return reset($this->take(1)->all($columns));
0 ignored issues
show
Bug introduced by
$this->take(1)->all($columns) of type Hoooklife\DynamodbPodm\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

253
        return reset(/** @scrutinizer ignore-type */ $this->take(1)->all($columns));
Loading history...
254
    }
255
256
257
    /**
258
     * Execute the query as a "select" statement.
259
     *
260
     * @param  array $columns
261
     * @return Collection
262
     */
263
    public function all($columns = ['*']): Collection
264
    {
265
        return $this->grammar->all($columns);
266
    }
267
268
    /**
269
     * @param array $data
270
     * @return \Aws\Result
271
     */
272
    public function insert(array $data)
273
    {
274
        return $this->grammar->insert($data);
275
    }
276
277
278
}
279