Passed
Push — master ( a357e7...195a07 )
by Anton
04:22 queued 01:11
created

Result   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A getRow() 0 6 2
A getRows() 0 10 3
A __get() 0 4 1
A __isset() 0 4 1
1
<?php
2
3
/**
4
 * @package Framework\DB
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2016, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace DB {
11
12
	class Result {
13
14
		/**
15
		 * @property  bool                $status     the result status
16
		 * @property  mysqli_result|bool  $result     the mysqli_result object for successful select queries, true for other queries, or false on failure
17
		 * @property  string              $query      the query string
18
		 * @property  float               $time       the query execution time in seconds
19
		 *
20
		 * @property  int                 $rows       a number of selected/affected rows
21
		 * @property  int                 $id         an updated id (auto-increment field) or zero if the id were not affected
22
		 * @property  string              $error      an error description or an empty string if no error occurred
23
		 * @property  int                 $errno      an error code or zero if no error occurred
24
		 */
25
26
		private $status = false, $result = null, $query = '', $time = 0.0;
27
28
		private $rows = 0, $id = 0, $error = '', $errno = 0;
29
30
		/**
31
		 * Constructor
32
		 */
33
34
		public function __construct(\mysqli $link, $result, string $query, float $time) {
35
36
			$this->status       = (false !== $result);
37
38
			$this->result       = $result;
39
40
			$this->query        = $query;
41
42
			$this->time         = $time;
43
44
			$this->rows         = mysqli_affected_rows($link);
45
46
			$this->id           = mysqli_insert_id($link);
47
48
			$this->error        = mysqli_error($link);
49
50
			$this->errno        = mysqli_errno($link);
51
		}
52
53
		/**
54
		 * Get the next row
55
		 *
56
		 * @return the row data array or null if there are no more rows in the resultset
57
		 */
58
59
		public function getRow() {
60
61
			if (!is_object($this->result)) return null;
62
63
			return mysqli_fetch_assoc($this->result);
64
		}
65
66
		/**
67
		 * Get the rows array
68
		 */
69
70
		public function getRows() : array {
71
72
			if (!is_object($this->result)) return [];
73
74
			$rows = []; while (null !== ($row = $this->getRow())) $rows[] = $row;
75
76
			# ------------------------
77
78
			return $rows;
79
		}
80
81
		/**
82
		 * Get a property
83
		 */
84
85
		public function __get(string $property) {
86
87
			return ($this->$property ?? null);
88
		}
89
90
		/**
91
		 * Check if a property exists
92
		 */
93
94
		public function __isset(string $property) : bool {
95
96
			return isset($this->$property);
97
		}
98
	}
99
}
100