Completed
Push — master ( 87c372...9db816 )
by Lars
01:48
created

Result::fetchAll()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 14
ccs 3
cts 3
cp 1
rs 9.2
c 1
b 0
f 1
cc 4
eloc 9
nc 4
nop 0
crap 4
1
<?php
2
3
namespace voku\db;
4
5
use Arrayy\Arrayy;
6
use voku\helper\Bootup;
7
use voku\helper\UTF8;
8
9
/**
10
 * Result: this handles the result from "DB"-Class
11
 *
12
 * @package   voku\db
13
 */
14
final class Result implements \Countable, \SeekableIterator, \ArrayAccess
15
{
16
17
  /**
18
   * @var int
19
   */
20
  public $num_rows;
21
22
  /**
23
   * @var string
24
   */
25
  public $sql;
26
27
  /**
28
   * @var \mysqli_result
29
   */
30
  private $_result;
31
32
  /**
33
   * @var int
34
   */
35
  private $current_row;
36
37
  /**
38
   * @var \Closure|null
39
   */
40
  private $_mapper;
41
42
  /**
43 30
   * @var string
44
   */
45 30
  private $_default_result_type = 'object';
46
47 30
  /**
48 30
   * Result constructor.
49 30
   *
50
   * @param string         $sql
51
   * @param \mysqli_result $result
52
   * @param \Closure       $mapper Optional callback mapper for the "fetch_callable()" method
53
   */
54 1
  public function __construct($sql = '', \mysqli_result $result, $mapper = null)
55
  {
56 1
    $this->sql = $sql;
57
58
    $this->_result = $result;
59
60
    $this->current_row = 0;
61
    $this->num_rows = (int)$this->_result->num_rows;
62
63
    $this->_mapper = $mapper;
64
  }
65
66 2
  /**
67
   * __destruct
68
   *
69
   */
70 2
  public function __destruct()
71
  {
72 2
    $this->free();
73
  }
74 2
75 2
  /**
76 2
   * Runs a user-provided callback with the MySQLi_Result object given as
77 2
   * argument and returns the result, or returns the MySQLi_Result object if
78
   * called without an argument.
79
   *
80
   * @param callable $callback User-provided callback (optional)
81
   *
82
   * @return mixed|\mysqli_result
83
   */
84
  public function __invoke($callback = null)
85
  {
86
    if (isset($callback)) {
87
      return call_user_func($callback, $this->_result);
88
    }
89
90
    return $this->_result;
91
  }
92
93
  /**
94
   * Get the current "num_rows" as string.
95
   *
96
   * @return string
97
   */
98
  public function __toString()
99
  {
100 1
    return (string)$this->num_rows;
101
  }
102 1
103 1
  /**
104
   * Cast data into int, float or string.
105 1
   *
106
   * <p>
107 1
   *   <br />
108 1
   *   INFO: install / use "mysqlnd"-driver for better performance
109 1
   * </p>
110 1
   *
111 1
   * @param array|object $data
112 1
   *
113 1
   * @return array|object|false <p><strong>false</strong> on error</p>
114 1
   */
115 1
  private function cast(&$data)
116
  {
117 1
    if (Helper::isMysqlndIsUsed() === true) {
118
      return $data;
119
    }
120
121
    // init
122
    if (Bootup::is_php('5.4')) {
123
      static $FIELDS_CACHE = array();
124
      static $TYPES_CACHE = array();
125
    } else {
126
      $FIELDS_CACHE = array();
127
      $TYPES_CACHE = array();
128
    }
129
130
    $result_hash = spl_object_hash($this->_result);
131
132 26
    if (!isset($FIELDS_CACHE[$result_hash])) {
133
      $FIELDS_CACHE[$result_hash] = \mysqli_fetch_fields($this->_result);
134 26
    }
135 26
136
    if ($FIELDS_CACHE[$result_hash] === false) {
137
      return false;
138
    }
139
140
    if (!isset($TYPES_CACHE[$result_hash])) {
141
      foreach ($FIELDS_CACHE[$result_hash] as $field) {
142
        switch ($field->type) {
143
          case 3:
144
            $TYPES_CACHE[$result_hash][$field->name] = 'int';
145
            break;
146
          case 4:
147
            $TYPES_CACHE[$result_hash][$field->name] = 'float';
148
            break;
149
          default:
150
            $TYPES_CACHE[$result_hash][$field->name] = 'string';
151
            break;
152
        }
153
      }
154
    }
155
156
    if (is_array($data) === true) {
157 View Code Duplication
      foreach ($TYPES_CACHE[$result_hash] as $type_name => $type) {
158
        if (isset($data[$type_name])) {
159
          settype($data[$type_name], $type);
160
        }
161
      }
162
    } elseif (is_object($data)) {
163 View Code Duplication
      foreach ($TYPES_CACHE[$result_hash] as $type_name => $type) {
164
        if (isset($data->{$type_name})) {
165
          settype($data->{$type_name}, $type);
166
        }
167
      }
168
    }
169
170
    return $data;
171
  }
172
173
  /**
174
   * Countable interface implementation.
175
   *
176
   * @return int The number of rows in the result
177
   */
178
  public function count()
179
  {
180
    return $this->num_rows;
181
  }
182
183
  /**
184
   * Iterator interface implementation.
185
   *
186
   * @return mixed The current element
187
   */
188
  public function current()
189
  {
190
    return $this->fetch_callable($this->current_row);
191
  }
192
193
  /**
194
   * Iterator interface implementation.
195 10
   *
196
   * @return int The current element key (row index; zero-based)
197
   */
198 10
  public function key()
199
  {
200
    return $this->current_row;
201 10
  }
202 10
203 10
  /**
204 10
   * Iterator interface implementation.
205 10
   *
206
   * @return void
207
   */
208 10
  public function next()
209 10
  {
210 10
    $this->current_row++;
211 10
  }
212
213 10
  /**
214
   * Iterator interface implementation.
215
   *
216
   * @param int $row Row position to rewind to; defaults to 0
217
   *
218
   * @return void
219
   */
220
  public function rewind($row = 0)
221 3
  {
222
    if ($this->seek($row)) {
223
      $this->current_row = $row;
224 3
    }
225
  }
226
227 3
  /**
228 3
   * Moves the internal pointer to the specified row position.
229 3
   *
230 3
   * @param int $row Row position; zero-based and set to 0 by default
231 3
   *
232
   * @return bool Boolean true on success, false otherwise
233
   */
234 3
  public function seek($row = 0)
235 3
  {
236 3
    if (is_int($row) && $row >= 0 && $row < $this->num_rows) {
237 3
      return mysqli_data_seek($this->_result, $row);
238
    }
239 3
240
    return false;
241
  }
242
243
  /**
244
   * Iterator interface implementation.
245
   *
246
   * @return bool Boolean true if the current index is valid, false otherwise
247 12
   */
248
  public function valid()
249 12
  {
250 12
    return $this->current_row < $this->num_rows;
251
  }
252
253 1
  /**
254
   * Fetch.
255
   *
256
   * <p>
257
   *   <br />
258
   *   INFO: this will return an object by default, not an array<br />
259
   *   and you can change the behaviour via "Result->setDefaultResultType()"
260
   * </p>
261 11
   *
262
   * @param $reset
263 11
   *
264 11
   * @return array|object|false <p><strong>false</strong> on error</p>
265 11
   */
266
  public function fetch($reset = false)
267 11
  {
268
    $return = false;
269
270
    if ($this->_default_result_type === 'object') {
271
      $return = $this->fetchObject('', '', $reset);
272
    } elseif ($this->_default_result_type === 'array') {
273
      $return = $this->fetchArray($reset);
274
    } elseif ($this->_default_result_type === 'Arrayy') {
275 1
      $return = $this->fetchArrayy($reset);
276
    }
277 1
278
    return $return;
279 1
  }
280
281
  /**
282
   * Fetch all results.
283
   *
284
   * <p>
285
   *   <br />
286 30
   *   INFO: this will return an object by default, not an array<br />
287
   *   and you can change the behaviour via "Result->setDefaultResultType()"
288 30
   * </p>
289 30
   *
290
   * @return array
291
   */
292
  public function fetchAll()
293
  {
294 30
    $return = array();
295
296 30
    if ($this->_default_result_type === 'object') {
297 30
      $return = $this->fetchAllObject();
298
    } elseif ($this->_default_result_type === 'array') {
299
      $return = $this->fetchAllArray();
300
    } elseif ($this->_default_result_type === 'Arrayy') {
301
      $return = $this->fetchAllArray();
302
    }
303
304
    return $return;
305
  }
306 1
307
  /**
308 1
   * Fetch all results as array.
309
   *
310
   * @return array
311
   */
312 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...
313
  {
314
    // init
315
    $data = array();
316
317
    if (
318
        $this->_result
319
        &&
320
        !$this->is_empty()
321
    ) {
322
      $this->reset();
323
324 2
      /** @noinspection PhpAssignmentInConditionInspection */
325
      while ($row = \mysqli_fetch_assoc($this->_result)) {
326 2
        $data[] = $this->cast($row);
327
      }
328 2
    }
329 2
330 2
    return $data;
331 2
  }
332 2
333
  /**
334
   * Fetch all results as "Arrayy"-object.
335
   *
336 2
   * @return Arrayy
337
   */
338 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...
339
  {
340
    // init
341
    $data = array();
342
343
    if (
344
        $this->_result
345
        &&
346
        !$this->is_empty()
347
    ) {
348 7
      $this->reset();
349
350 7
      /** @noinspection PhpAssignmentInConditionInspection */
351 1
      while ($row = \mysqli_fetch_assoc($this->_result)) {
352 1
        $data[] = $this->cast($row);
353
      }
354 7
    }
355 1
356
    return Arrayy::create($data);
357
  }
358 7
359 1
  /**
360
   * Fetch a single column as an 1-dimension array.
361
   *
362 7
   * @param string $column
363
   * @param bool   $skipNullValues <p>Skip "NULL"-values. | default: false</p>
364
   *
365
   * @return array <p>Return an empty array if the "$column" wasn't found</p>
366
   */
367
  public function fetchAllColumn($column, $skipNullValues = false)
368
  {
369
    return $this->fetchColumn($column, $skipNullValues, true);
370
  }
371
372 14
  /**
373
   * Fetch all results as array with objects.
374 14
   *
375 1
   * @param string     $class
376 1
   * @param null|array $params
377
   *
378 14
   * @return array
379 14
   */
380 13
  public function fetchAllObject($class = '', $params = null)
381
  {
382
    // init
383 3
    $data = array();
384 3
385
    if (!$this->is_empty()) {
386
      $this->reset();
387
388
      if ($class && $params) {
389
        /** @noinspection PhpAssignmentInConditionInspection */
390
        while ($row = \mysqli_fetch_object($this->_result, $class, $params)) {
391
          $data[] = $row;
392
        }
393
      } elseif ($class) {
394
        /** @noinspection PhpAssignmentInConditionInspection */
395
        while ($row = \mysqli_fetch_object($this->_result, $class)) {
396
          $data[] = $row;
397 2
        }
398
      } else {
399 2
        /** @noinspection PhpAssignmentInConditionInspection */
400
        while ($row = \mysqli_fetch_object($this->_result)) {
401
          $data[] = $this->cast($row);
402
        }
403 2
      }
404 2
    }
405 1
406
    return $data;
407
  }
408 1
409 1
  /**
410
   * Fetch as array.
411
   *
412
   * @param bool $reset
413
   *
414
   * @return array|false <p><strong>false</strong> on error</p>
415
   */
416 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...
417
  {
418
    if ($reset === true) {
419
      $this->reset();
420
    }
421
422 1
    $row = \mysqli_fetch_assoc($this->_result);
423
    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...
424 1
      return $this->cast($row);
425
    }
426
427
    if ($row === null) {
428
      return array();
429
    }
430
431
    return false;
432
  }
433
434
  /**
435
   * Fetch data as a key/value pair array.
436
   *
437
   * <p>
438 2
   *   <br />
439
   *   INFO: both "key" and "value" must exists in the fetched data
440 2
   *   the key will be the new key of the result-array
441
   *   <br /><br />
442 2
   * </p>
443 2
   *
444 2
   * e.g.:
445 1
   * <code>
446 1
   *    fetchArrayPair('some_id', 'some_value');
447
   *    // array(127 => 'some value', 128 => 'some other value')
448
   * </code>
449
   *
450 2
   * @param string $key
451
   * @param string $value
452
   *
453
   * @return array
454
   */
455
  public function fetchArrayPair($key, $value)
456
  {
457
    $arrayPair = array();
458
    $data = $this->fetchAllArray();
459
460
    foreach ($data as $_row) {
461 3
      if (
462
          array_key_exists($key, $_row) === true
463
          &&
464 3
          array_key_exists($value, $_row) === true
465
      ) {
466 3
        $_key = $_row[$key];
467 3
        $_value = $_row[$value];
468
        $arrayPair[$_key] = $_value;
469 3
      }
470
    }
471 1
472 1
    return $arrayPair;
473 1
  }
474 3
475
  /**
476 1
   * Fetch as "Arrayy"-object.
477 1
   *
478 1
   * @param bool $reset
479 1
   *
480
   * @return Arrayy|false <p><strong>false</strong> on error</p>
481 3
   */
482 3 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...
483 3
  {
484
    if ($reset === true) {
485 3
      $this->reset();
486
    }
487 3
488
    $row = \mysqli_fetch_assoc($this->_result);
489
    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...
490
      return Arrayy::create($this->cast($row));
0 ignored issues
show
Bug introduced by
It seems like $this->cast($row) targeting voku\db\Result::cast() can also be of type false or object; however, Arrayy\Arrayy::create() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
491
    }
492
493
    if ($row === null) {
494
      return Arrayy::create();
495
    }
496
497 1
    return false;
498
  }
499 1
500
  /**
501
   * Fetch a single column as string (or as 1-dimension array).
502
   *
503
   * @param string $column
504
   * @param bool   $skipNullValues <p>Skip "NULL"-values. | default: true</p>
505
   * @param bool   $asArray        <p>Get all values and not only the last one. | default: false</p>
506
   *
507
   * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on
508
   *                      "$asArray"</p>
509
   */
510
  public function fetchColumn($column = '', $skipNullValues = true, $asArray = false)
511
  {
512
    if ($asArray === false) {
513
      $columnData = '';
514
515
      $data = $this->fetchAllArrayy()->reverse();
516 View Code Duplication
      foreach ($data as $_row) {
517
518
        if ($skipNullValues === true) {
519
          if (isset($_row[$column]) === false) {
520
            continue;
521 1
          }
522
        } else {
523 1
          if (array_key_exists($column, $_row) === false) {
524
            break;
525
          }
526
        }
527
528
        $columnData = $_row[$column];
529
        break;
530
      }
531
532
      return $columnData;
533
    }
534 1
535
    // -- return as array -->
536 1
537
    $columnData = array();
538
539
    $data = $this->fetchAllArray();
540
541 View Code Duplication
    foreach ($data as $_row) {
542
543
      if ($skipNullValues === true) {
544
        if (isset($_row[$column]) === false) {
545
          continue;
546
        }
547
      } else {
548
        if (array_key_exists($column, $_row) === false) {
549
          break;
550
        }
551
      }
552
553
      $columnData[] = $_row[$column];
554
    }
555
556
    return $columnData;
557
  }
558
559
  /**
560
   * Fetch as object.
561
   *
562
   * @param string     $class
563
   * @param null|array $params
564
   * @param bool       $reset
565
   *
566 1
   * @return object|false <p><strong>false</strong> on error</p>
567
   */
568 1
  public function fetchObject($class = '', $params = null, $reset = false)
569
  {
570
    if ($reset === true) {
571
      $this->reset();
572
    }
573
574
    if ($class && $params) {
575
      return ($row = \mysqli_fetch_object($this->_result, $class, $params)) ? $row : false;
576
    }
577
578
    if ($class) {
579
      return ($row = \mysqli_fetch_object($this->_result, $class)) ? $row : false;
580
    }
581 2
582
    return ($row = \mysqli_fetch_object($this->_result)) ? $this->cast($row) : false;
583 2
  }
584 2
585
  /**
586 2
   * Fetches a row or a single column within a row. Returns null if there are
587 2
   * no more rows in the result.
588
   *
589 2
   * @param int    $row    The row number (optional)
590 2
   * @param string $column The column name (optional)
591 1
   *
592
   * @return mixed An associative array or a scalar value
593 2
   */
594 1
  public function fetch_callable($row = null, $column = null)
595 1
  {
596
    if (!$this->num_rows) {
597
      return null;
598
    }
599 2
600 2
    if (isset($row)) {
601 2
      $this->seek($row);
602
    }
603 2
604
    $rows = \mysqli_fetch_assoc($this->_result);
605
606
    if ($column) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $column of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
607
      return is_array($rows) && isset($rows[$column]) ? $rows[$column] : null;
608 1
    }
609
610 1
    return is_callable($this->_mapper) ? call_user_func($this->_mapper, $rows) : $rows;
611
  }
612 1
613
  /**
614 1
   * Return rows of field information in a result set. This function is a
615 1
   * basically a wrapper on the native mysqli_fetch_fields function.
616 1
   *
617
   * @param bool $as_array Return each field info as array; defaults to false
618 1
   *
619 1
   * @return array Array of field information each as an associative array
620 1
   */
621
  public function fetch_fields($as_array = false)
622
  {
623
    if ($as_array) {
624 1
      return array_map(
625 1
          function ($object) {
626
            return (array)$object;
627 1
          },
628
          \mysqli_fetch_fields($this->_result)
629
      );
630
    }
631
632
    return \mysqli_fetch_fields($this->_result);
633
  }
634
635
  /**
636
   * Returns all rows at once as a grouped array of scalar values or arrays.
637
   *
638
   * @param string $group  The column name to use for grouping
639
   * @param string $column The column name to use as values (optional)
640
   *
641
   * @return array A grouped array of scalar values or arrays
642
   */
643 View Code Duplication
  public function fetch_groups($group, $column = null)
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...
644
  {
645
    // init
646
    $groups = array();
647
    $pos = $this->current_row;
648
649
    foreach ($this as $row) {
650
651
      if (!array_key_exists($group, $row)) {
652
        continue;
653
      }
654
655
      if (isset($column)) {
656
657
        if (!array_key_exists($column, $row)) {
658
          continue;
659
        }
660
661
        $groups[$row[$group]][] = $row[$column];
662
      } else {
663
        $groups[$row[$group]][] = $row;
664
      }
665
    }
666
667
    $this->rewind($pos);
668
669
    return $groups;
670
  }
671
672
  /**
673
   * Returns all rows at once as key-value pairs.
674
   *
675
   * @param string $key    The column name to use as keys
676
   * @param string $column The column name to use as values (optional)
677
   *
678
   * @return array An array of key-value pairs
679
   */
680 View Code Duplication
  public function fetch_pairs($key, $column = null)
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...
681
  {
682
    // init
683
    $pairs = array();
684
    $pos = $this->current_row;
685
686
    foreach ($this as $row) {
687
688
      if (!array_key_exists($key, $row)) {
689
        continue;
690
      }
691
692
      if (isset($column)) {
693
694
        if (!array_key_exists($column, $row)) {
695
          continue;
696
        }
697
698
        $pairs[$row[$key]] = $row[$column];
699
      } else {
700
        $pairs[$row[$key]] = $row;
701
      }
702
    }
703
704
    $this->rewind($pos);
705
706
    return $pairs;
707
  }
708
709
  /**
710
   * Returns all rows at once, transposed as an array of arrays. Instead of
711
   * returning rows of columns, this method returns columns of rows.
712
   *
713
   * @param string $column The column name to use as keys (optional)
714
   *
715
   * @return mixed A transposed array of arrays
716
   */
717
  public function fetch_transpose($column = null)
718
  {
719
    // init
720
    $keys = isset($column) ? $this->fetchAllColumn($column) : array();
721
    $rows = array();
722
    $pos = $this->current_row;
723
724
    foreach ($this as $row) {
725
      foreach ($row as $key => $value) {
726
        $rows[$key][] = $value;
727
      }
728
    }
729
730
    $this->rewind($pos);
731
732
    if (empty($keys)) {
733
      return $rows;
734
    }
735
736
    return array_map(
737
        function ($values) use ($keys) {
738
          return array_combine($keys, $values);
739
        }, $rows
740
    );
741
  }
742
743
  /**
744
   * Returns the first row element from the result.
745
   *
746
   * @param string $column The column name to use as value (optional)
747
   *
748
   * @return mixed A row array or a single scalar value
749
   */
750
  public function first($column = null)
751
  {
752
    $pos = $this->current_row;
753
    $first = $this->fetch_callable(0, $column);
754
    $this->rewind($pos);
755
756
    return $first;
757
  }
758
759
  /**
760
   * free the memory
761
   */
762
  public function free()
763
  {
764
    if (isset($this->_result) && $this->_result) {
765
      /** @noinspection PhpUsageOfSilenceOperatorInspection */
766
      /** @noinspection UsageOfSilenceOperatorInspection */
767
      @\mysqli_free_result($this->_result);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
768
      $this->_result = null;
769
770
      return true;
771
    }
772
773
    return false;
774
  }
775
776
  /**
777
   * alias for "Result->fetch()"
778
   *
779
   * @see Result::fetch()
780
   *
781
   * @return array|object|false <p><strong>false</strong> on error</p>
782
   */
783
  public function get()
784
  {
785
    return $this->fetch();
786
  }
787
788
  /**
789
   * alias for "Result->fetchAll()"
790
   *
791
   * @see Result::fetchAll()
792
   *
793
   * @return array
794
   */
795
  public function getAll()
796
  {
797
    return $this->fetchAll();
798
  }
799
800
  /**
801
   * alias for "Result->fetchAllColumn()"
802
   *
803
   * @see Result::fetchAllColumn()
804
   *
805
   * @param string $column
806
   * @param bool   $skipNullValues
807
   *
808
   * @return array
809
   */
810
  public function getAllColumn($column, $skipNullValues = false)
811
  {
812
    return $this->fetchAllColumn($column, $skipNullValues);
813
  }
814
815
  /**
816
   * alias for "Result->fetchAllArray()"
817
   *
818
   * @see Result::fetchAllArray()
819
   *
820
   * @return array
821
   */
822
  public function getArray()
823
  {
824
    return $this->fetchAllArray();
825
  }
826
827
  /**
828
   * alias for "Result->fetchAllArrayy()"
829
   *
830
   * @see Result::fetchAllArrayy()
831
   *
832
   * @return Arrayy
833
   */
834
  public function getArrayy()
835
  {
836
    return $this->fetchAllArrayy();
837
  }
838
839
  /**
840
   * alias for "Result->fetchColumn()"
841
   *
842
   * @see Result::fetchColumn()
843
   *
844
   * @param $column
845
   * @param $asArray
846
   * @param $skipNullValues
847
   *
848
   * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on
849
   *                      "$asArray"</p>
850
   */
851
  public function getColumn($column, $skipNullValues = true, $asArray = false)
852
  {
853
    return $this->fetchColumn($column, $skipNullValues, $asArray);
854
  }
855
856
  /**
857
   * @return string
858
   */
859
  public function getDefaultResultType()
860
  {
861
    return $this->_default_result_type;
862
  }
863
864
  /**
865
   * alias for "Result->fetchAllObject()"
866
   *
867
   * @see Result::fetchAllObject()
868
   *
869
   * @return array of mysql-objects
870
   */
871
  public function getObject()
872
  {
873
    return $this->fetchAllObject();
874
  }
875
876
  /**
877
   * Check if the result is empty.
878
   *
879
   * @return bool
880
   */
881
  public function is_empty()
882
  {
883
    if ($this->num_rows > 0) {
884
      return false;
885
    }
886
887
    return true;
888
  }
889
890
  /**
891
   * Fetch all results as "json"-string.
892
   *
893
   * @return string
894
   */
895
  public function json()
896
  {
897
    $data = $this->fetchAllArray();
898
899
    return UTF8::json_encode($data);
900
  }
901
902
  /**
903
   * Returns the last row element from the result.
904
   *
905
   * @param string $column The column name to use as value (optional)
906
   *
907
   * @return mixed A row array or a single scalar value
908
   */
909
  public function last($column = null)
910
  {
911
    $pos = $this->current_row;
912
    $last = $this->fetch_callable($this->num_rows - 1, $column);
913
    $this->rewind($pos);
914
915
    return $last;
916
  }
917
918
  /**
919
   * Set the mapper...
920
   *
921
   * @param \Closure $callable
922
   *
923
   * @return $this
924
   */
925
  public function map(\Closure $callable)
926
  {
927
    $this->_mapper = $callable;
928
929
    return $this;
930
  }
931
932
  /**
933
   * Alias of count(). Deprecated.
934
   *
935
   * @return int The number of rows in the result
936
   */
937
  public function num_rows()
938
  {
939
    return $this->count();
940
  }
941
942
  /**
943
   * ArrayAccess interface implementation.
944
   *
945
   * @param int $offset Offset number
946
   *
947
   * @return bool Boolean true if offset exists, false otherwise
948
   */
949
  public function offsetExists($offset)
950
  {
951
    return is_int($offset) && $offset >= 0 && $offset < $this->num_rows;
952
  }
953
954
  /**
955
   * ArrayAccess interface implementation.
956
   *
957
   * @param int $offset Offset number
958
   *
959
   * @return mixed
960
   */
961
  public function offsetGet($offset)
962
  {
963
    if ($this->offsetExists($offset)) {
964
      return $this->fetch_callable($offset);
965
    }
966
967
    throw new \OutOfBoundsException("undefined offset ($offset)");
968
  }
969
970
  /**
971
   * ArrayAccess interface implementation. Not implemented by design.
972
   *
973
   * @param mixed $offset
974
   * @param mixed $value
975
   */
976
  public function offsetSet($offset, $value)
977
  {
978
    return;
979
  }
980
981
  /**
982
   * ArrayAccess interface implementation. Not implemented by design.
983
   *
984
   * @param mixed $offset
985
   */
986
  public function offsetUnset($offset)
987
  {
988
    return;
989
  }
990
991
  /**
992
   * Reset the offset (data_seek) for the results.
993
   *
994
   * @return Result
995
   */
996
  public function reset()
997
  {
998
    if (!$this->is_empty()) {
999
      \mysqli_data_seek($this->_result, 0);
1000
    }
1001
1002
    return $this;
1003
  }
1004
1005
  /**
1006
   * You can set the default result-type to 'object', 'array' or 'Arrayy'.
1007
   *
1008
   * INFO: used for "fetch()" and "fetchAll()"
1009
   *
1010
   * @param string $default_result_type
1011
   */
1012
  public function setDefaultResultType($default_result_type = 'object')
1013
  {
1014
    if (
1015
        $default_result_type === 'object'
1016
        ||
1017
        $default_result_type === 'array'
1018
        ||
1019
        $default_result_type === 'Arrayy'
1020
    ) {
1021
      $this->_default_result_type = $default_result_type;
1022
    }
1023
  }
1024
1025
  /**
1026
   * @param int      $offset
1027
   * @param null|int $length
1028
   * @param bool     $preserve_keys
1029
   *
1030
   * @return array
1031
   */
1032
  public function slice($offset = 0, $length = null, $preserve_keys = false)
1033
  {
1034
    // init
1035
    $slice = array();
1036
    $offset = (int)$offset;
1037
1038
    if ($offset < 0) {
1039
      if (abs($offset) > $this->num_rows) {
1040
        $offset = 0;
1041
      } else {
1042
        $offset = $this->num_rows - abs($offset);
1043
      }
1044
    }
1045
1046
    $length = isset($length) ? (int)$length : $this->num_rows;
1047
    $n = 0;
1048
    for ($i = $offset; $i < $this->num_rows && $n < $length; $i++) {
1049
      if ($preserve_keys) {
1050
        $slice[$i] = $this->fetch_callable($i);
1051
      } else {
1052
        $slice[] = $this->fetch_callable($i);
1053
      }
1054
      ++$n;
1055
    }
1056
1057
    return $slice;
1058
  }
1059
}
1060