Completed
Push — master ( eea790...427bc5 )
by Lars
05:00 queued 02:52
created

Result::getArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace voku\db;
4
5
use Arrayy\Arrayy;
6
7
/**
8
 * Result: this handles the result from "DB"-Class
9
 *
10
 * @package   voku\db
11
 */
12
final class Result
13
{
14
15
  /**
16
   * @var int
17
   */
18
  public $num_rows;
19
20
  /**
21
   * @var string
22
   */
23
  public $sql;
24
25
  /**
26
   * @var \mysqli_result
27
   */
28
  private $_result;
29
30
  /**
31
   * @var string
32
   */
33
  private $_default_result_type = 'object';
34
35
  /**
36
   * Result
37
   *
38
   * @param string         $sql
39
   * @param \mysqli_result $result
40
   */
41 20
  public function __construct($sql = '', \mysqli_result $result)
42
  {
43 20
    $this->sql = $sql;
44
45 20
    $this->_result = $result;
46 20
    $this->num_rows = $this->_result->num_rows;
47 20
  }
48
49
  /**
50
   * @return string
51
   */
52 1
  public function getDefaultResultType()
53
  {
54 1
    return $this->_default_result_type;
55
  }
56
57
  /**
58
   * you can set the default result-type to 'object' or 'array'
59
   *
60
   * used for "fetch()" and "fetchAll()"
61
   *
62
   * @param string $default_result_type
63
   */
64 2
  public function setDefaultResultType($default_result_type = 'object')
65
  {
66 2
    if ($default_result_type === 'object' || $default_result_type === 'array') {
67 2
      $this->_default_result_type = $default_result_type;
68 2
    }
69 2
  }
70
71
  /**
72
   * fetch array-pair
73
   *
74
   * both "key" and "value" must exists in the fetched data
75
   * the key will be the new key of the result-array
76
   *
77
   * e.g.:
78
   *    fetchArrayPair('some_id', 'some_value');
79
   *    // array(127 => 'some value', 128 => 'some other value')
80
   *
81
   * @param string $key
82
   * @param string $value
83
   *
84
   * @return array
85
   */
86 1
  public function fetchArrayPair($key, $value)
87
  {
88 1
    $arrayPair = array();
89 1
    $data = $this->fetchAllArray();
90
91 1
    foreach ($data as $_row) {
92 1
      if (isset($_row[$key], $_row[$value])) {
93 1
        $_key = $_row[$key];
94 1
        $_value = $_row[$value];
95 1
        $arrayPair[$_key] = $_value;
96 1
      }
97 1
    }
98
99 1
    return $arrayPair;
100
  }
101
102
  /**
103
   * fetchAllArray
104
   *
105
   * @return array
106
   */
107 11 View Code Duplication
  public function fetchAllArray()
0 ignored issues
show
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...
108
  {
109
    // init
110 11
    $data = array();
111
112
    if (
113 11
        $this->_result
114 11
        &&
115 11
        !$this->is_empty()
116 11
    ) {
117 11
      $this->reset();
118
119
      /** @noinspection PhpAssignmentInConditionInspection */
120 11
      while ($row = mysqli_fetch_assoc($this->_result)) {
121 11
        $data[] = $row;
122 11
      }
123 11
    }
124
125 11
    return $data;
126
  }
127
128
  /**
129
   * fetch all results, return via Arrayy
130
   *
131
   * @return Arrayy
132
   */
133 1 View Code Duplication
  public function fetchAllArrayy()
0 ignored issues
show
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...
134
  {
135
    // init
136 1
    $data = array();
137
138
    if (
139 1
        $this->_result
140 1
        &&
141 1
        !$this->is_empty()
142 1
    ) {
143 1
      $this->reset();
144
145
      /** @noinspection PhpAssignmentInConditionInspection */
146 1
      while ($row = mysqli_fetch_assoc($this->_result)) {
147 1
        $data[] = $row;
148 1
      }
149 1
    }
150
151 1
    return Arrayy::create($data);
152
  }
153
154
  /**
155
   * is_empty
156
   *
157
   * @return bool
158
   */
159 13
  public function is_empty()
160
  {
161 13
    if ($this->num_rows > 0) {
162 13
      return false;
163
    } else {
164 1
      return true;
165
    }
166
  }
167
168
  /**
169
   * reset
170
   *
171
   * @return Result
172
   */
173 12
  public function reset()
174
  {
175 12
    if (!$this->is_empty()) {
176 12
      mysqli_data_seek($this->_result, 0);
177 12
    }
178
179 12
    return $this;
180
  }
181
182
  /**
183
   * json
184
   *
185
   * @return string
186
   */
187 1
  public function json()
188
  {
189 1
    $data = $this->fetchAllArray();
190
191 1
    return json_encode($data);
192
  }
193
194
  /**
195
   * __destruct
196
   *
197
   */
198 20
  public function __destruct()
199
  {
200 20
    $this->free();
201 20
  }
202
203
  /**
204
   * free
205
   */
206 20
  public function free()
207
  {
208 20
    mysqli_free_result($this->_result);
209 20
  }
210
211
  /**
212
   * get
213
   *
214
   * @return array|object|false false on error
215
   */
216 1
  public function get()
217
  {
218 1
    return $this->fetch();
219
  }
220
221
  /**
222
   * fetch (object -> not a array by default)
223
   *
224
   * @param $reset
225
   *
226
   * @return array|object|false false on error
227
   */
228 2
  public function fetch($reset = false)
229
  {
230 2
    $return = false;
231
232 2
    if ($this->_default_result_type === 'object') {
233 2
      $return = $this->fetchObject('', '', $reset);
234 2
    } elseif ($this->_default_result_type === 'array') {
235 2
      $return = $this->fetchArray($reset);
236 2
    }
237
238 2
    return $return;
239
  }
240
241
  /**
242
   * fetchObject
243
   *
244
   * @param string     $class
245
   * @param null|array $params
246
   * @param bool       $reset
247
   *
248
   * @return object|false false on error
249
   */
250 5
  public function fetchObject($class = '', $params = null, $reset = false)
251
  {
252 5
    if ($reset === true) {
253 1
      $this->reset();
254 1
    }
255
256 5
    if ($class && $params) {
257 1
      return ($row = mysqli_fetch_object($this->_result, $class, $params)) ? $row : false;
258
    }
259
260 5
    if ($class) {
261 1
      return ($row = mysqli_fetch_object($this->_result, $class)) ? $row : false;
262
    }
263
264 5
    return ($row = mysqli_fetch_object($this->_result)) ? $row : false;
265
  }
266
267
  /**
268
   * fetch as array
269
   *
270
   * @param bool $reset
271
   *
272
   * @return array|false false on error
273
   */
274 5 View Code Duplication
  public function fetchArray($reset = false)
0 ignored issues
show
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...
275
  {
276 5
    if ($reset === true) {
277 1
      $this->reset();
278 1
    }
279
280 5
    $row = mysqli_fetch_assoc($this->_result);
281 5
    if ($row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
282 5
      return $row;
283
    }
284
285
    return false;
286
  }
287
288
  /**
289
   * fetch as Arrayy-Object
290
   *
291
   * @param bool $reset
292
   *
293
   * @return Arrayy|false false on error
294
   */
295 1 View Code Duplication
  public function fetchArrayy($reset = false)
0 ignored issues
show
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...
296
  {
297 1
    if ($reset === true) {
298
      $this->reset();
299
    }
300
301 1
    $row = mysqli_fetch_assoc($this->_result);
302 1
    if ($row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
303 1
      return Arrayy::create($row);
304
    }
305
306
    return false;
307
  }
308
309
  /**
310
   * getAll
311
   *
312
   * @return array
313
   */
314 1
  public function getAll()
315
  {
316 1
    return $this->fetchAll();
317
  }
318
319
  /**
320
   * fetchAll
321
   *
322
   * @return array
323
   */
324 2
  public function fetchAll()
325
  {
326 2
    $return = array();
327
328 2
    if ($this->_default_result_type === 'object') {
329 2
      $return = $this->fetchAllObject();
330 2
    } elseif ($this->_default_result_type === 'array') {
331 1
      $return = $this->fetchAllArray();
332 1
    }
333
334 2
    return $return;
335
  }
336
337
  /**
338
   * fetchAllObject
339
   *
340
   * @param string     $class
341
   * @param null|array $params
342
   *
343
   * @return array
344
   */
345 3
  public function fetchAllObject($class = '', $params = null)
346
  {
347
    // init
348 3
    $data = array();
349
350 3
    if (!$this->is_empty()) {
351 3
      $this->reset();
352
353 3
      if ($class && $params) {
354
        /** @noinspection PhpAssignmentInConditionInspection */
355 1
        while ($row = mysqli_fetch_object($this->_result, $class, $params)) {
356 1
          $data[] = $row;
357 1
        }
358 3
      } elseif ($class) {
359
        /** @noinspection PhpAssignmentInConditionInspection */
360 1
        while ($row = mysqli_fetch_object($this->_result, $class)) {
361 1
          $data[] = $row;
362 1
        }
363 1
      } else {
364
        /** @noinspection PhpAssignmentInConditionInspection */
365 3
        while ($row = mysqli_fetch_object($this->_result)) {
366 3
          $data[] = $row;
367 3
        }
368
      }
369 3
    }
370
371 3
    return $data;
372
  }
373
374
  /**
375
   * getObject
376
   *
377
   * @return array of mysql-objects
378
   */
379 1
  public function getObject()
380
  {
381 1
    return $this->fetchAllObject();
382
  }
383
384
  /**
385
   * getArray
386
   *
387
   * @return array
388
   */
389 1
  public function getArray()
390
  {
391 1
    return $this->fetchAllArray();
392
  }
393
394
  /**
395
   * getColumn
396
   *
397
   * @param $key
398
   *
399
   * @return string
400
   */
401 1
  public function getColumn($key)
402
  {
403 1
    return $this->fetchColumn($key);
404
  }
405
406
  /**
407
   * fetchColumn
408
   *
409
   * @param string $column
410
   *
411
   * @return string empty string if the $column wasn't found
412
   */
413 2
  public function fetchColumn($column = '')
414
  {
415 2
    $columnData = '';
416 2
    $data = $this->fetchAllArray();
417
418 2
    foreach ($data as $_row) {
419 2
      if (isset($_row[$column])) {
420 2
        $columnData = $_row[$column];
421 2
      }
422 2
    }
423
424 2
    return $columnData;
425
  }
426
427
  /**
428
   * get the num-rows as string
429
   *
430
   * @return string
431
   */
432 1
  public function __toString()
433
  {
434 1
    return (string)$this->num_rows;
435
  }
436
}
437