Completed
Push — master ( 331f74...a01c40 )
by Lars
09:50
created

Result::getDefaultResultType()   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 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
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
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|null
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 18
  public function __construct($sql = '', $result = null)
42
  {
43 18
    $this->sql = $sql;
44
45 18
    if ($result === null) {
46 1
      return;
47
    }
48
49 18
    if ($result instanceof \mysqli_result) {
50 18
      $this->_result = $result;
51 18
      $this->num_rows = $this->_result->num_rows;
52 18
    }
53 18
  }
54
55
  /**
56
   * @return string
57
   */
58 1
  public function getDefaultResultType()
59
  {
60 1
    return $this->_default_result_type;
61
  }
62
63
  /**
64
   * you can set the default result-type to 'object' or 'array'
65
   *
66
   * used for "fetch()" and "fetchAll()"
67
   *
68
   * @param string $default_result_type
69
   */
70 2
  public function setDefaultResultType($default_result_type = 'object')
71
  {
72 2
    if ($default_result_type == 'object' || $default_result_type == 'array') {
73 2
      $this->_default_result_type = $default_result_type;
74 2
    }
75 2
  }
76
77
  /**
78
   * fetch array-pair
79
   *
80
   * both "key" and "value" must exists in the fetched data
81
   * the key will be the new key of the result-array
82
   *
83
   * e.g.:
84
   *    fetchArrayPair('some_id', 'some_value');
85
   *    // array(127 => 'some value', 128 => 'some other value')
86
   *
87
   * @param string $key
88
   * @param string $value
89
   *
90
   * @return array
91
   */
92 1
  public function fetchArrayPair($key, $value)
93
  {
94 1
    $arrayPair = array();
95 1
    $data = $this->fetchAllArray();
96
97 1
    foreach ($data as $_row) {
98 1
      if (isset($_row[$key], $_row[$value])) {
99 1
        $_key = $_row[$key];
100 1
        $_value = $_row[$value];
101 1
        $arrayPair[$_key] = $_value;
102 1
      }
103 1
    }
104
105 1
    return $arrayPair;
106
  }
107
108
  /**
109
   * fetchAllArray
110
   *
111
   * @return array
112
   */
113 9 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...
114
  {
115
    // init
116 9
    $data = array();
117
118
    if (
119 9
        $this->_result
120 9
        &&
121 9
        !$this->is_empty()
122 9
    ) {
123 9
      $this->reset();
124
125
      /** @noinspection PhpAssignmentInConditionInspection */
126 9
      while ($row = mysqli_fetch_assoc($this->_result)) {
127 9
        $data[] = $row;
128 9
      }
129 9
    }
130
131 9
    return $data;
132
  }
133
134
  /**
135
   * fetch all results, return via Arrayy
136
   *
137
   * @return Arrayy
138
   */
139 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...
140
  {
141
    // init
142 1
    $data = array();
143
144
    if (
145 1
        $this->_result
146 1
        &&
147 1
        !$this->is_empty()
148 1
    ) {
149 1
      $this->reset();
150
151
      /** @noinspection PhpAssignmentInConditionInspection */
152 1
      while ($row = mysqli_fetch_assoc($this->_result)) {
153 1
        $data[] = $row;
154 1
      }
155 1
    }
156
157 1
    return Arrayy::create($data);
158
  }
159
160
  /**
161
   * is_empty
162
   *
163
   * @return bool
164
   */
165 11
  public function is_empty()
166
  {
167 11
    if ($this->num_rows > 0) {
168 11
      return false;
169
    } else {
170 1
      return true;
171
    }
172
  }
173
174
  /**
175
   * reset
176
   *
177
   * @return Result
178
   */
179 10
  public function reset()
180
  {
181
    if (
182 10
        $this->_result
183 10
        &&
184 10
        !$this->is_empty()
185 10
    ) {
186 10
      mysqli_data_seek($this->_result, 0);
187 10
    }
188
189 10
    return $this;
190
  }
191
192
  /**
193
   * json
194
   *
195
   * @return string
196
   */
197 1
  public function json()
198
  {
199 1
    $data = $this->fetchAllArray();
200
201 1
    return json_encode($data);
202
  }
203
204
  /**
205
   * __destruct
206
   *
207
   */
208 18
  public function __destruct()
209
  {
210 18
    $this->free();
211 18
  }
212
213
  /**
214
   * free
215
   */
216 18
  public function free()
217
  {
218 18
    if ($this->_result && $this->_result instanceof \mysqli_result) {
219 18
      mysqli_free_result($this->_result);
220 18
    }
221 18
  }
222
223
  /**
224
   * get
225
   *
226
   * @return array|object|false false on error
227
   */
228 1
  public function get()
229
  {
230 1
    return $this->fetch();
231
  }
232
233
  /**
234
   * fetch (object -> not a array by default)
235
   *
236
   * @param $reset
237
   *
238
   * @return array|object|false false on error
239
   */
240 3
  public function fetch($reset = false)
241
  {
242 3
    $return = false;
243
244 3
    if ($this->_default_result_type == 'object') {
245 3
      $return = $this->fetchObject('', '', $reset);
246 3
    } elseif ($this->_default_result_type == 'array') {
247 2
      $return = $this->fetchArray($reset);
248 2
    }
249
250 3
    return $return;
251
  }
252
253
  /**
254
   * fetchObject
255
   *
256
   * @param string     $class
257
   * @param null|array $params
258
   * @param bool       $reset
259
   *
260
   * @return object|false false on error
261
   */
262 5
  public function fetchObject($class = '', $params = null, $reset = false)
263
  {
264 5
    if ($reset === true) {
265 1
      $this->reset();
266 1
    }
267
268 5
    if ($this->_result) {
269 5
      if ($class && $params) {
270 1
        return ($row = mysqli_fetch_object($this->_result, $class, $params)) ? $row : false;
271 5
      } elseif ($class) {
272 1
        return ($row = mysqli_fetch_object($this->_result, $class)) ? $row : false;
273
      } else {
274 5
        return ($row = mysqli_fetch_object($this->_result)) ? $row : false;
275
      }
276
    }
277
278 1
    return false;
279
  }
280
281
  /**
282
   * fetch as array
283
   *
284
   * @param bool $reset
285
   *
286
   * @return array|false false on error
287
   */
288 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...
289
  {
290 5
    if ($reset === true) {
291 1
      $this->reset();
292 1
    }
293
294 5
    if ($this->_result) {
295 4
      $row = mysqli_fetch_assoc($this->_result);
296 4
      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...
297 4
        return $row;
298
      }
299
    }
300
301 1
    return false;
302
  }
303
304
  /**
305
   * fetch as Arrayy-Object
306
   *
307
   * @param bool $reset
308
   *
309
   * @return Arrayy|false false on error
310
   */
311 2 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...
312
  {
313 2
    if ($reset === true) {
314
      $this->reset();
315
    }
316
317 2
    if ($this->_result) {
318 1
      $row = mysqli_fetch_assoc($this->_result);
319 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...
320 1
        return Arrayy::create($row);
321
      }
322
    }
323
324 1
    return false;
325
  }
326
327
  /**
328
   * getAll
329
   *
330
   * @return array
331
   */
332 1
  public function getAll()
333
  {
334 1
    return $this->fetchAll();
335
  }
336
337
  /**
338
   * fetchAll
339
   *
340
   * @return array
341
   */
342 2
  public function fetchAll()
343
  {
344 2
    $return = array();
345
346 2
    if ($this->_default_result_type == 'object') {
347 2
      $return = $this->fetchAllObject();
348 2
    } elseif ($this->_default_result_type == 'array') {
349 1
      $return = $this->fetchAllArray();
350 1
    }
351
352 2
    return $return;
353
  }
354
355
  /**
356
   * fetchAllObject
357
   *
358
   * @param string     $class
359
   * @param null|array $params
360
   *
361
   * @return array
362
   */
363 3
  public function fetchAllObject($class = '', $params = null)
364
  {
365
    // init
366 3
    $data = array();
367
368
    if (
369 3
        $this->_result
370 3
        &&
371 3
        !$this->is_empty()
372 3
    ) {
373 3
      $this->reset();
374
375 3
      if ($class && $params) {
376
        /** @noinspection PhpAssignmentInConditionInspection */
377 1
        while ($row = mysqli_fetch_object($this->_result, $class, $params)) {
378 1
          $data[] = $row;
379 1
        }
380 3
      } elseif ($class) {
381
        /** @noinspection PhpAssignmentInConditionInspection */
382 1
        while ($row = mysqli_fetch_object($this->_result, $class)) {
383 1
          $data[] = $row;
384 1
        }
385 1
      } else {
386
        /** @noinspection PhpAssignmentInConditionInspection */
387 3
        while ($row = mysqli_fetch_object($this->_result)) {
388 3
          $data[] = $row;
389 3
        }
390
      }
391 3
    }
392
393 3
    return $data;
394
  }
395
396
  /**
397
   * getObject
398
   *
399
   * @return array of mysql-objects
400
   */
401 1
  public function getObject()
402
  {
403 1
    return $this->fetchAllObject();
404
  }
405
406
  /**
407
   * getArray
408
   *
409
   * @return array
410
   */
411 1
  public function getArray()
412
  {
413 1
    return $this->fetchAllArray();
414
  }
415
416
  /**
417
   * getColumn
418
   *
419
   * @param $key
420
   *
421
   * @return string
422
   */
423 1
  public function getColumn($key)
424
  {
425 1
    return $this->fetchColumn($key);
426
  }
427
428
  /**
429
   * fetchColumn
430
   *
431
   * @param string $column
432
   *
433
   * @return string empty string if the $column wasn't found
434
   */
435 2
  public function fetchColumn($column = '')
436
  {
437 2
    $columnData = '';
438 2
    $data = $this->fetchAllArray();
439
440 2
    foreach ($data as $_row) {
441 2
      if (isset($_row[$column])) {
442 2
        $columnData = $_row[$column];
443 2
      }
444 2
    }
445
446 2
    return $columnData;
447
  }
448
449
  /**
450
   * get the num-rows as string
451
   *
452
   * @return string
453
   */
454 1
  public function __toString()
455
  {
456 1
    return (string)$this->num_rows;
457
  }
458
}
459