Completed
Push — master ( 099e03...6bc12c )
by Lars
06:51
created

Result::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
  public function __destruct()
70 2
  {
71
    $this->free();
72 2
  }
73
74 2
  /**
75 2
   * Runs a user-provided callback with the MySQLi_Result object given as
76 2
   * argument and returns the result, or returns the MySQLi_Result object if
77 2
   * called without an argument.
78
   *
79
   * @param callable $callback User-provided callback (optional)
80
   *
81
   * @return mixed|\mysqli_result
82
   */
83
  public function __invoke($callback = null)
84
  {
85
    if (isset($callback)) {
86
      return call_user_func($callback, $this->_result);
87
    }
88
89
    return $this->_result;
90
  }
91
92
  /**
93
   * Get the current "num_rows" as string.
94
   *
95
   * @return string
96
   */
97
  public function __toString()
98
  {
99
    return (string)$this->num_rows;
100 1
  }
101
102 1
  /**
103 1
   * Cast data into int, float or string.
104
   *
105 1
   * <p>
106
   *   <br />
107 1
   *   INFO: install / use "mysqlnd"-driver for better performance
108 1
   * </p>
109 1
   *
110 1
   * @param array|object $data
111 1
   *
112 1
   * @return array|object|false <p><strong>false</strong> on error</p>
113 1
   */
114 1
  private function cast(&$data)
115 1
  {
116
    if (Helper::isMysqlndIsUsed() === true) {
117 1
      return $data;
118
    }
119
120
    // init
121
    if (Bootup::is_php('5.4')) {
122
      static $FIELDS_CACHE = array();
123
      static $TYPES_CACHE = array();
124
    } else {
125
      $FIELDS_CACHE = array();
126
      $TYPES_CACHE = array();
127
    }
128
129
    $result_hash = spl_object_hash($this->_result);
130
131
    if (!isset($FIELDS_CACHE[$result_hash])) {
132 26
      $FIELDS_CACHE[$result_hash] = \mysqli_fetch_fields($this->_result);
133
    }
134 26
135 26
    if ($FIELDS_CACHE[$result_hash] === false) {
136
      return false;
137
    }
138
139
    if (!isset($TYPES_CACHE[$result_hash])) {
140
      foreach ($FIELDS_CACHE[$result_hash] as $field) {
141
        switch ($field->type) {
142
          case 3:
143
            $TYPES_CACHE[$result_hash][$field->name] = 'int';
144
            break;
145
          case 4:
146
            $TYPES_CACHE[$result_hash][$field->name] = 'float';
147
            break;
148
          default:
149
            $TYPES_CACHE[$result_hash][$field->name] = 'string';
150
            break;
151
        }
152
      }
153
    }
154
155
    if (is_array($data) === true) {
156 View Code Duplication
      foreach ($TYPES_CACHE[$result_hash] as $type_name => $type) {
157
        if (isset($data[$type_name])) {
158
          settype($data[$type_name], $type);
159
        }
160
      }
161
    } elseif (is_object($data)) {
162 View Code Duplication
      foreach ($TYPES_CACHE[$result_hash] as $type_name => $type) {
163
        if (isset($data->{$type_name})) {
164
          settype($data->{$type_name}, $type);
165
        }
166
      }
167
    }
168
169
    return $data;
170
  }
171
172
  /**
173
   * Countable interface implementation.
174
   *
175
   * @return int The number of rows in the result
176
   */
177
  public function count()
178
  {
179
    return $this->num_rows;
180
  }
181
182
  /**
183
   * Iterator interface implementation.
184
   *
185
   * @return mixed The current element
186
   */
187
  public function current()
188
  {
189
    return $this->fetch_callable($this->current_row);
190
  }
191
192
  /**
193
   * Iterator interface implementation.
194
   *
195 10
   * @return int The current element key (row index; zero-based)
196
   */
197
  public function key()
198 10
  {
199
    return $this->current_row;
200
  }
201 10
202 10
  /**
203 10
   * Iterator interface implementation.
204 10
   *
205 10
   * @return void
206
   */
207
  public function next()
208 10
  {
209 10
    $this->current_row++;
210 10
  }
211 10
212
  /**
213 10
   * Iterator interface implementation.
214
   *
215
   * @param int $row Row position to rewind to; defaults to 0
216
   *
217
   * @return void
218
   */
219
  public function rewind($row = 0)
220
  {
221 3
    if ($this->seek($row)) {
222
      $this->current_row = $row;
223
    }
224 3
  }
225
226
  /**
227 3
   * Moves the internal pointer to the specified row position.
228 3
   *
229 3
   * @param int $row Row position; zero-based and set to 0 by default
230 3
   *
231 3
   * @return bool Boolean true on success, false otherwise
232
   */
233
  public function seek($row = 0)
234 3
  {
235 3
    if (is_int($row) && $row >= 0 && $row < $this->num_rows) {
236 3
      return mysqli_data_seek($this->_result, $row);
237 3
    }
238
239 3
    return false;
240
  }
241
242
  /**
243
   * Iterator interface implementation.
244
   *
245
   * @return bool Boolean true if the current index is valid, false otherwise
246
   */
247 12
  public function valid()
248
  {
249 12
    return $this->current_row < $this->num_rows;
250 12
  }
251
252
  /**
253 1
   * Fetch.
254
   *
255
   * <p>
256
   *   <br />
257
   *   INFO: this will return an object by default, not an array<br />
258
   *   and you can change the behaviour via "Result->setDefaultResultType()"
259
   * </p>
260
   *
261 11
   * @param $reset
262
   *
263 11
   * @return array|object|false <p><strong>false</strong> on error</p>
264 11
   */
265 11
  public function fetch($reset = false)
266
  {
267 11
    $return = false;
268
269
    if ($this->_default_result_type === 'object') {
270
      $return = $this->fetchObject('', '', $reset);
271
    } elseif ($this->_default_result_type === 'array') {
272
      $return = $this->fetchArray($reset);
273
    } elseif ($this->_default_result_type === 'Arrayy') {
274
      $return = $this->fetchArrayy($reset);
275 1
    }
276
277 1
    return $return;
278
  }
279 1
280
  /**
281
   * Fetch all results.
282
   *
283
   * <p>
284
   *   <br />
285
   *   INFO: this will return an object by default, not an array<br />
286 30
   *   and you can change the behaviour via "Result->setDefaultResultType()"
287
   * </p>
288 30
   *
289 30
   * @return array
290
   */
291
  public function fetchAll()
292
  {
293
    $return = array();
294 30
295
    if ($this->_default_result_type === 'object') {
296 30
      $return = $this->fetchAllObject();
297 30
    } elseif ($this->_default_result_type === 'array') {
298
      $return = $this->fetchAllArray();
299
    } elseif ($this->_default_result_type === 'Arrayy') {
300
      $return = $this->fetchAllArray();
301
    }
302
303
    return $return;
304
  }
305
306 1
  /**
307
   * Fetch all results as array.
308 1
   *
309
   * @return array
310
   */
311 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...
312
  {
313
    // init
314
    $data = array();
315
316
    if (
317
        $this->_result
318
        &&
319
        !$this->is_empty()
320
    ) {
321
      $this->reset();
322
323
      /** @noinspection PhpAssignmentInConditionInspection */
324 2
      while ($row = \mysqli_fetch_assoc($this->_result)) {
325
        $data[] = $this->cast($row);
326 2
      }
327
    }
328 2
329 2
    return $data;
330 2
  }
331 2
332 2
  /**
333
   * Fetch all results as "Arrayy"-object.
334
   *
335
   * @return Arrayy
336 2
   */
337 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...
338
  {
339
    // init
340
    $data = array();
341
342
    if (
343
        $this->_result
344
        &&
345
        !$this->is_empty()
346
    ) {
347
      $this->reset();
348 7
349
      /** @noinspection PhpAssignmentInConditionInspection */
350 7
      while ($row = \mysqli_fetch_assoc($this->_result)) {
351 1
        $data[] = $this->cast($row);
352 1
      }
353
    }
354 7
355 1
    return Arrayy::create($data);
356
  }
357
358 7
  /**
359 1
   * Fetch a single column as an 1-dimension array.
360
   *
361
   * @param string $column
362 7
   * @param bool   $skipNullValues <p>Skip "NULL"-values. | default: false</p>
363
   *
364
   * @return array <p>Return an empty array if the "$column" wasn't found</p>
365
   */
366
  public function fetchAllColumn($column, $skipNullValues = false)
367
  {
368
    return $this->fetchColumn($column, $skipNullValues, true);
369
  }
370
371
  /**
372 14
   * Fetch all results as array with objects.
373
   *
374 14
   * @param string     $class
375 1
   * @param null|array $params
376 1
   *
377
   * @return array
378 14
   */
379 14
  public function fetchAllObject($class = '', $params = null)
380 13
  {
381
    // init
382
    $data = array();
383 3
384 3
    if (!$this->is_empty()) {
385
      $this->reset();
386
387
      if ($class && $params) {
388
        /** @noinspection PhpAssignmentInConditionInspection */
389
        while ($row = \mysqli_fetch_object($this->_result, $class, $params)) {
390
          $data[] = $row;
391
        }
392
      } elseif ($class) {
393
        /** @noinspection PhpAssignmentInConditionInspection */
394
        while ($row = \mysqli_fetch_object($this->_result, $class)) {
395
          $data[] = $row;
396
        }
397 2
      } else {
398
        /** @noinspection PhpAssignmentInConditionInspection */
399 2
        while ($row = \mysqli_fetch_object($this->_result)) {
400
          $data[] = $this->cast($row);
401
        }
402
      }
403 2
    }
404 2
405 1
    return $data;
406
  }
407
408 1
  /**
409 1
   * Fetch as array.
410
   *
411
   * @param bool $reset
412
   *
413
   * @return array|false <p><strong>false</strong> on error</p>
414
   */
415 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...
416
  {
417
    if ($reset === true) {
418
      $this->reset();
419
    }
420
421
    $row = \mysqli_fetch_assoc($this->_result);
422 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...
423
      return $this->cast($row);
424 1
    }
425
426
    if ($row === null) {
427
      return array();
428
    }
429
430
    return false;
431
  }
432
433
  /**
434
   * Fetch data as a key/value pair array.
435
   *
436
   * <p>
437
   *   <br />
438 2
   *   INFO: both "key" and "value" must exists in the fetched data
439
   *   the key will be the new key of the result-array
440 2
   *   <br /><br />
441
   * </p>
442 2
   *
443 2
   * e.g.:
444 2
   * <code>
445 1
   *    fetchArrayPair('some_id', 'some_value');
446 1
   *    // array(127 => 'some value', 128 => 'some other value')
447
   * </code>
448
   *
449
   * @param string $key
450 2
   * @param string $value
451
   *
452
   * @return array
453
   */
454
  public function fetchArrayPair($key, $value)
455
  {
456
    $arrayPair = array();
457
    $data = $this->fetchAllArray();
458
459
    foreach ($data as $_row) {
460
      if (
461 3
          array_key_exists($key, $_row) === true
462
          &&
463
          array_key_exists($value, $_row) === true
464 3
      ) {
465
        $_key = $_row[$key];
466 3
        $_value = $_row[$value];
467 3
        $arrayPair[$_key] = $_value;
468
      }
469 3
    }
470
471 1
    return $arrayPair;
472 1
  }
473 1
474 3
  /**
475
   * Fetch as "Arrayy"-object.
476 1
   *
477 1
   * @param bool $reset
478 1
   *
479 1
   * @return Arrayy|false <p><strong>false</strong> on error</p>
480
   */
481 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...
482 3
  {
483 3
    if ($reset === true) {
484
      $this->reset();
485 3
    }
486
487 3
    $row = \mysqli_fetch_assoc($this->_result);
488
    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...
489
      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...
490
    }
491
492
    if ($row === null) {
493
      return Arrayy::create();
494
    }
495
496
    return false;
497 1
  }
498
499 1
  /**
500
   * Fetch a single column as string (or as 1-dimension array).
501
   *
502
   * @param string $column
503
   * @param bool   $skipNullValues <p>Skip "NULL"-values. | default: true</p>
504
   * @param bool   $asArray        <p>Get all values and not only the last one. | default: false</p>
505
   *
506
   * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on
507
   *                      "$asArray"</p>
508
   */
509
  public function fetchColumn($column = '', $skipNullValues = true, $asArray = false)
510
  {
511
    if ($asArray === false) {
512
      $columnData = '';
513
514
      $data = $this->fetchAllArrayy()->reverse();
515 View Code Duplication
      foreach ($data as $_row) {
516
517
        if ($skipNullValues === true) {
518
          if (isset($_row[$column]) === false) {
519
            continue;
520
          }
521 1
        } else {
522
          if (array_key_exists($column, $_row) === false) {
523 1
            break;
524
          }
525
        }
526
527
        $columnData = $_row[$column];
528
        break;
529
      }
530
531
      return $columnData;
532
    }
533
534 1
    // -- return as array -->
535
536 1
    $columnData = array();
537
538
    $data = $this->fetchAllArray();
539
540 View Code Duplication
    foreach ($data as $_row) {
541
542
      if ($skipNullValues === true) {
543
        if (isset($_row[$column]) === false) {
544
          continue;
545
        }
546
      } else {
547
        if (array_key_exists($column, $_row) === false) {
548
          break;
549
        }
550
      }
551
552
      $columnData[] = $_row[$column];
553
    }
554
555
    return $columnData;
556
  }
557
558
  /**
559
   * Fetch as object.
560
   *
561
   * @param string     $class
562
   * @param null|array $params
563
   * @param bool       $reset
564
   *
565
   * @return object|false <p><strong>false</strong> on error</p>
566 1
   */
567
  public function fetchObject($class = '', $params = null, $reset = false)
568 1
  {
569
    if ($reset === true) {
570
      $this->reset();
571
    }
572
573
    if ($class && $params) {
574
      return ($row = \mysqli_fetch_object($this->_result, $class, $params)) ? $row : false;
575
    }
576
577
    if ($class) {
578
      return ($row = \mysqli_fetch_object($this->_result, $class)) ? $row : false;
579
    }
580
581 2
    return ($row = \mysqli_fetch_object($this->_result)) ? $this->cast($row) : false;
582
  }
583 2
584 2
  /**
585
   * Fetches a row or a single column within a row. Returns null if there are
586 2
   * no more rows in the result.
587 2
   *
588
   * @param int    $row    The row number (optional)
589 2
   * @param string $column The column name (optional)
590 2
   *
591 1
   * @return mixed An associative array or a scalar value
592
   */
593 2
  public function fetch_callable($row = null, $column = null)
594 1
  {
595 1
    if (!$this->num_rows) {
596
      return null;
597
    }
598
599 2
    if (isset($row)) {
600 2
      $this->seek($row);
601 2
    }
602
603 2
    $rows = \mysqli_fetch_assoc($this->_result);
604
605
    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...
606
      return is_array($rows) && isset($rows[$column]) ? $rows[$column] : null;
607
    }
608 1
609
    return is_callable($this->_mapper) ? call_user_func($this->_mapper, $rows) : $rows;
610 1
  }
611
612 1
  /**
613
   * Return rows of field information in a result set. This function is a
614 1
   * basically a wrapper on the native mysqli_fetch_fields function.
615 1
   *
616 1
   * @param bool $as_array Return each field info as array; defaults to false
617
   *
618 1
   * @return array Array of field information each as an associative array
619 1
   */
620 1
  public function fetch_fields($as_array = false)
621
  {
622
    if ($as_array) {
623
      return array_map(
624 1
          function ($object) {
625 1
            return (array)$object;
626
          },
627 1
          \mysqli_fetch_fields($this->_result)
628
      );
629
    }
630
631
    return \mysqli_fetch_fields($this->_result);
632
  }
633
634
  /**
635
   * Returns all rows at once as a grouped array of scalar values or arrays.
636
   *
637
   * @param string $group  The column name to use for grouping
638
   * @param string $column The column name to use as values (optional)
639
   *
640
   * @return array A grouped array of scalar values or arrays
641
   */
642 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...
643
  {
644
    // init
645
    $groups = array();
646
    $pos = $this->current_row;
647
648
    foreach ($this as $row) {
649
650
      if (!array_key_exists($group, $row)) {
651
        continue;
652
      }
653
654
      if (isset($column)) {
655
656
        if (!array_key_exists($column, $row)) {
657
          continue;
658
        }
659
660
        $groups[$row[$group]][] = $row[$column];
661
      } else {
662
        $groups[$row[$group]][] = $row;
663
      }
664
    }
665
666
    $this->rewind($pos);
667
668
    return $groups;
669
  }
670
671
  /**
672
   * Returns all rows at once as key-value pairs.
673
   *
674
   * @param string $key    The column name to use as keys
675
   * @param string $column The column name to use as values (optional)
676
   *
677
   * @return array An array of key-value pairs
678
   */
679 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...
680
  {
681
    // init
682
    $pairs = array();
683
    $pos = $this->current_row;
684
685
    foreach ($this as $row) {
686
687
      if (!array_key_exists($key, $row)) {
688
        continue;
689
      }
690
691
      if (isset($column)) {
692
693
        if (!array_key_exists($column, $row)) {
694
          continue;
695
        }
696
697
        $pairs[$row[$key]] = $row[$column];
698
      } else {
699
        $pairs[$row[$key]] = $row;
700
      }
701
    }
702
703
    $this->rewind($pos);
704
705
    return $pairs;
706
  }
707
708
  /**
709
   * Returns all rows at once, transposed as an array of arrays. Instead of
710
   * returning rows of columns, this method returns columns of rows.
711
   *
712
   * @param string $column The column name to use as keys (optional)
713
   *
714
   * @return mixed A transposed array of arrays
715
   */
716
  public function fetch_transpose($column = null)
717
  {
718
    // init
719
    $keys = isset($column) ? $this->fetchAllColumn($column) : array();
720
    $rows = array();
721
    $pos = $this->current_row;
722
723
    foreach ($this as $row) {
724
      foreach ($row as $key => $value) {
725
        $rows[$key][] = $value;
726
      }
727
    }
728
729
    $this->rewind($pos);
730
731
    if (empty($keys)) {
732
      return $rows;
733
    }
734
735
    return array_map(
736
        function ($values) use ($keys) {
737
          return array_combine($keys, $values);
738
        }, $rows
739
    );
740
  }
741
742
  /**
743
   * Returns the first row element from the result.
744
   *
745
   * @param string $column The column name to use as value (optional)
746
   *
747
   * @return mixed A row array or a single scalar value
748
   */
749
  public function first($column = null)
750
  {
751
    $pos = $this->current_row;
752
    $first = $this->fetch_callable(0, $column);
753
    $this->rewind($pos);
754
755
    return $first;
756
  }
757
758
  /**
759
   * free the memory
760
   */
761
  public function free()
762
  {
763
    if (isset($this->_result) && $this->_result) {
764
      /** @noinspection PhpUsageOfSilenceOperatorInspection */
765
      /** @noinspection UsageOfSilenceOperatorInspection */
766
      @\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...
767
      $this->_result = null;
768
769
      return true;
770
    }
771
772
    return false;
773
  }
774
775
  /**
776
   * alias for "Result->fetch()"
777
   *
778
   * @see Result::fetch()
779
   *
780
   * @return array|object|false <p><strong>false</strong> on error</p>
781
   */
782
  public function get()
783
  {
784
    return $this->fetch();
785
  }
786
787
  /**
788
   * alias for "Result->fetchAll()"
789
   *
790
   * @see Result::fetchAll()
791
   *
792
   * @return array
793
   */
794
  public function getAll()
795
  {
796
    return $this->fetchAll();
797
  }
798
799
  /**
800
   * alias for "Result->fetchAllColumn()"
801
   *
802
   * @see Result::fetchAllColumn()
803
   *
804
   * @param string $column
805
   * @param bool   $skipNullValues
806
   *
807
   * @return array
808
   */
809
  public function getAllColumn($column, $skipNullValues = false)
810
  {
811
    return $this->fetchAllColumn($column, $skipNullValues);
812
  }
813
814
  /**
815
   * alias for "Result->fetchAllArray()"
816
   *
817
   * @see Result::fetchAllArray()
818
   *
819
   * @return array
820
   */
821
  public function getArray()
822
  {
823
    return $this->fetchAllArray();
824
  }
825
826
  /**
827
   * alias for "Result->fetchAllArrayy()"
828
   *
829
   * @see Result::fetchAllArrayy()
830
   *
831
   * @return Arrayy
832
   */
833
  public function getArrayy()
834
  {
835
    return $this->fetchAllArrayy();
836
  }
837
838
  /**
839
   * alias for "Result->fetchColumn()"
840
   *
841
   * @see Result::fetchColumn()
842
   *
843
   * @param $column
844
   * @param $asArray
845
   * @param $skipNullValues
846
   *
847
   * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on
848
   *                      "$asArray"</p>
849
   */
850
  public function getColumn($column, $skipNullValues = true, $asArray = false)
851
  {
852
    return $this->fetchColumn($column, $skipNullValues, $asArray);
853
  }
854
855
  /**
856
   * @return string
857
   */
858
  public function getDefaultResultType()
859
  {
860
    return $this->_default_result_type;
861
  }
862
863
  /**
864
   * alias for "Result->fetchAllObject()"
865
   *
866
   * @see Result::fetchAllObject()
867
   *
868
   * @return array of mysql-objects
869
   */
870
  public function getObject()
871
  {
872
    return $this->fetchAllObject();
873
  }
874
875
  /**
876
   * Check if the result is empty.
877
   *
878
   * @return bool
879
   */
880
  public function is_empty()
881
  {
882
    if ($this->num_rows > 0) {
883
      return false;
884
    }
885
886
    return true;
887
  }
888
889
  /**
890
   * Fetch all results as "json"-string.
891
   *
892
   * @return string
893
   */
894
  public function json()
895
  {
896
    $data = $this->fetchAllArray();
897
898
    return UTF8::json_encode($data);
899
  }
900
901
  /**
902
   * Returns the last row element from the result.
903
   *
904
   * @param string $column The column name to use as value (optional)
905
   *
906
   * @return mixed A row array or a single scalar value
907
   */
908
  public function last($column = null)
909
  {
910
    $pos = $this->current_row;
911
    $last = $this->fetch_callable($this->num_rows - 1, $column);
912
    $this->rewind($pos);
913
914
    return $last;
915
  }
916
917
  /**
918
   * Set the mapper...
919
   *
920
   * @param \Closure $callable
921
   *
922
   * @return $this
923
   */
924
  public function map(\Closure $callable)
925
  {
926
    $this->_mapper = $callable;
927
928
    return $this;
929
  }
930
931
  /**
932
   * Alias of count(). Deprecated.
933
   *
934
   * @return int The number of rows in the result
935
   */
936
  public function num_rows()
937
  {
938
    return $this->count();
939
  }
940
941
  /**
942
   * ArrayAccess interface implementation.
943
   *
944
   * @param int $offset Offset number
945
   *
946
   * @return bool Boolean true if offset exists, false otherwise
947
   */
948
  public function offsetExists($offset)
949
  {
950
    return is_int($offset) && $offset >= 0 && $offset < $this->num_rows;
951
  }
952
953
  /**
954
   * ArrayAccess interface implementation.
955
   *
956
   * @param int $offset Offset number
957
   *
958
   * @return mixed
959
   */
960
  public function offsetGet($offset)
961
  {
962
    if ($this->offsetExists($offset)) {
963
      return $this->fetch_callable($offset);
964
    }
965
966
    throw new \OutOfBoundsException("undefined offset ($offset)");
967
  }
968
969
  /**
970
   * ArrayAccess interface implementation. Not implemented by design.
971
   *
972
   * @param mixed $offset
973
   * @param mixed $value
974
   */
975
  public function offsetSet($offset, $value)
976
  {
977
    /** @noinspection UselessReturnInspection */
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
    /** @noinspection UselessReturnInspection */
989
    return;
990
  }
991
992
  /**
993
   * Reset the offset (data_seek) for the results.
994
   *
995
   * @return Result
996
   */
997
  public function reset()
998
  {
999
    if (!$this->is_empty()) {
1000
      \mysqli_data_seek($this->_result, 0);
1001
    }
1002
1003
    return $this;
1004
  }
1005
1006
  /**
1007
   * You can set the default result-type to 'object', 'array' or 'Arrayy'.
1008
   *
1009
   * INFO: used for "fetch()" and "fetchAll()"
1010
   *
1011
   * @param string $default_result_type
1012
   */
1013
  public function setDefaultResultType($default_result_type = 'object')
1014
  {
1015
    if (
1016
        $default_result_type === 'object'
1017
        ||
1018
        $default_result_type === 'array'
1019
        ||
1020
        $default_result_type === 'Arrayy'
1021
    ) {
1022
      $this->_default_result_type = $default_result_type;
1023
    }
1024
  }
1025
1026
  /**
1027
   * @param int      $offset
1028
   * @param null|int $length
1029
   * @param bool     $preserve_keys
1030
   *
1031
   * @return array
1032
   */
1033
  public function slice($offset = 0, $length = null, $preserve_keys = false)
1034
  {
1035
    // init
1036
    $slice = array();
1037
    $offset = (int)$offset;
1038
1039
    if ($offset < 0) {
1040
      if (abs($offset) > $this->num_rows) {
1041
        $offset = 0;
1042
      } else {
1043
        $offset = $this->num_rows - abs($offset);
1044
      }
1045
    }
1046
1047
    $length = isset($length) ? (int)$length : $this->num_rows;
1048
    $n = 0;
1049
    for ($i = $offset; $i < $this->num_rows && $n < $length; $i++) {
1050
      if ($preserve_keys) {
1051
        $slice[$i] = $this->fetch_callable($i);
1052
      } else {
1053
        $slice[] = $this->fetch_callable($i);
1054
      }
1055
      ++$n;
1056
    }
1057
1058
    return $slice;
1059
  }
1060
}
1061