1 | <?php |
||
13 | class TableQuery implements \IteratorAggregate, DatasetInterface { |
||
14 | use Utils\FetchModeTrait, DatasetTrait { |
||
15 | fetchAll as protected fetchAllTrait; |
||
16 | setFetchMode as protected; |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * @var Db |
||
21 | */ |
||
22 | private $db; |
||
23 | |||
24 | /** |
||
25 | * @var array|null |
||
26 | */ |
||
27 | private $data; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $table; |
||
33 | |||
34 | /** |
||
35 | * @var |
||
36 | */ |
||
37 | private $where; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $options; |
||
43 | |||
44 | /** |
||
45 | * @var callable A callback used to unserialize rows from the database. |
||
46 | */ |
||
47 | private $rowCallback; |
||
48 | |||
49 | /** |
||
50 | * Construct a new {@link TableQuery} object. |
||
51 | * |
||
52 | * Note that this class is not meant to be modified after being constructed so it's important to pass everything you |
||
53 | * need early. |
||
54 | * |
||
55 | * @param string $table The name of the table to query. |
||
56 | * @param array $where The filter information for the query. |
||
57 | * @param Db $db The database to fetch the data from. This can be changed later. |
||
58 | * @param array $options Additional options for the object: |
||
59 | * |
||
60 | * fetchMode |
||
61 | * : The PDO fetch mode. This can be one of the **PDO::FETCH_** constants, a class name or an array of arguments for |
||
62 | * {@link PDOStatement::fetchAll()} |
||
63 | * rowCallback |
||
64 | * : A callable that will be applied to each row of the result set. |
||
65 | */ |
||
66 | 17 | public function __construct($table, array $where, Db $db, array $options = []) { |
|
67 | 17 | $this->table = $table; |
|
68 | 17 | $this->where = $where; |
|
69 | 17 | $this->db = $db; |
|
|
|||
70 | |||
71 | $options += [ |
||
72 | 17 | Db::OPTION_FETCH_MODE => $db->getFetchArgs(), |
|
73 | 'rowCallback' => null |
||
74 | 17 | ]; |
|
75 | |||
76 | 17 | $fetchMode = (array)$options[Db::OPTION_FETCH_MODE]; |
|
77 | 17 | if (in_array($fetchMode[0], [PDO::FETCH_OBJ | PDO::FETCH_NUM | PDO::FETCH_FUNC])) { |
|
78 | throw new \InvalidArgumentException("Fetch mode not supported.", 500); |
||
79 | 17 | } elseif ($fetchMode[0] == PDO::FETCH_CLASS && !is_a($fetchMode[1], \ArrayAccess::class, true)) { |
|
80 | throw new \InvalidArgumentException("The {$fetchMode[1]} class must implement ArrayAccess.", 500); |
||
81 | } |
||
82 | |||
83 | 17 | $this->setFetchMode(...$fetchMode); |
|
84 | 17 | $this->rowCallback = $options['rowCallback']; |
|
85 | 17 | } |
|
86 | |||
87 | /** |
||
88 | * Get the order. |
||
89 | * |
||
90 | * @return array Returns the order. |
||
91 | */ |
||
92 | 1 | public function getOrder() { |
|
95 | |||
96 | /** |
||
97 | * Set the order. |
||
98 | * |
||
99 | * @param string[] $columns The column names to order by. |
||
100 | * @return $this |
||
101 | */ |
||
102 | 16 | public function setOrder(...$columns) { |
|
106 | |||
107 | /** |
||
108 | * Get the offset. |
||
109 | * |
||
110 | * @return int Returns the offset. |
||
111 | */ |
||
112 | 1 | public function getOffset() { |
|
115 | |||
116 | /** |
||
117 | * Set the offset. |
||
118 | * |
||
119 | * @param int $offset |
||
120 | * @return $this |
||
121 | */ |
||
122 | 2 | public function setOffset($offset) { |
|
130 | |||
131 | /** |
||
132 | * Get the limit. |
||
133 | * |
||
134 | * @return int Returns the limit. |
||
135 | */ |
||
136 | 3 | public function getLimit() { |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 17 | public function setLimit($limit) { |
|
144 | 17 | if (!is_numeric($limit) || $limit < 0) { |
|
145 | throw new \InvalidArgumentException("Invalid limit '$limit.'", 500); |
||
146 | } |
||
147 | |||
148 | |||
149 | 17 | $reset = true; |
|
150 | |||
151 | 17 | if (is_array($this->data) && $limit < $this->getLimit()) { |
|
152 | 2 | $this->data = array_slice($this->data, 0, $limit); |
|
153 | 2 | $reset = false; |
|
154 | 2 | } |
|
155 | |||
156 | 17 | $this->setOption('limit', (int)$limit, $reset); |
|
157 | |||
158 | 17 | return $this; |
|
159 | } |
||
160 | |||
161 | 3 | protected function getOption($name, $default = null) { |
|
164 | |||
165 | /** |
||
166 | * Set a query option. |
||
167 | * |
||
168 | * @param string $name The name of the option to set. |
||
169 | * @param mixed $value The new value of the option. |
||
170 | * @param bool $reset Pass **true** and the data will be queried again if the option value has changed. |
||
171 | * @return $this |
||
172 | */ |
||
173 | 17 | protected function setOption($name, $value, $reset = false) { |
|
183 | |||
184 | /** |
||
185 | * Get the data. |
||
186 | * |
||
187 | * @return array Returns the data. |
||
188 | */ |
||
189 | 16 | protected function getData() { |
|
210 | |||
211 | 12 | public function fetchAll($mode = 0, ...$args) { |
|
225 | } |
||
226 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.