Completed
Push — master ( 6ae902...a7bf2b )
by Chris
02:53
created

AbstractResult::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Darya\Storage;
3
4
use ArrayIterator;
5
use IteratorAggregate;
6
7
/**
8
 * Darya's abstract storage query result.
9
 * 
10
 * @property array  $data     Result data
11
 * @property object $query    Query that produced this result
12
 * @property int    $count    Result count
13
 * @property object $error    Result error
14
 * @property array  $fields   Field names for each result data row
15
 * @property int    $insertId Insert ID
16
 * @property int    $affected Rows affected
17
 * 
18
 * @author Chris Andrew <[email protected]>
19
 */
20
abstract class AbstractResult implements IteratorAggregate {
21
	
22
	/**
23
	 * The query that produced this result.
24
	 * 
25
	 * @var mixed
26
	 */
27
	protected $query;
28
	
29
	/**
30
	 * An associative array of the result data.
31
	 * 
32
	 * @var array
33
	 */
34
	protected $data;
35
	
36
	/**
37
	 * The error that occurred when executing the query, if any.
38
	 * 
39
	 * @var mixed|null
40
	 */
41
	protected $error;
42
	
43
	/**
44
	 * The number of rows in the result data.
45
	 * 
46
	 * @var int
47
	 */
48
	protected $count = 0;
49
	
50
	/**
51
	 * The set of fields available for each row in the result.
52
	 * 
53
	 * @var array
54
	 */
55
	protected $fields = array();
56
	
57
	/**
58
	 * The number of rows affected by the query.
59
	 * 
60
	 * @var string
61
	 */
62
	protected $affected = 0;
63
	
64
	/**
65
	 * Auto incremented primary key of an inserted row.
66
	 * 
67
	 * @var int
68
	 */
69
	protected $insertId = 0;
70
	
71
	/**
72
	 * Convert a string from snake_case to camelCase.
73
	 * 
74
	 * @param string $string
75
	 * @return string
76
	 */
77
	protected static function snakeToCamel($string) {
78
		return preg_replace_callback('/_(.)/', function($matches) {
79
			return strtoupper($matches[1]);
80
		}, $string);
81
	}
82
	
83
	/**
84
	 * Get the result info.
85
	 * 
86
	 * Retrieves the affected, count, insertId and fields properties
87
	 * as a key-value array, with snake-case equivalent keys.
88
	 * 
89
	 * @return array
90
	 */
91
	public function getInfo() {
92
		return array(
93
			'count' => $this->count,
94
			'fields' => $this->fields,
95
			'affected' => $this->affected,
96
			'insert_id' => $this->insertId
97
		);
98
	}
99
	
100
	/**
101
	 * Set the result info.
102
	 * 
103
	 * Accepts the keys 'affected', 'count', 'insert_id' and 'fields'.
104
	 * 
105
	 * @param array $info
106
	 */
107
	protected function setInfo(array $info) {
108
		$keys = array_keys($this->getInfo());
109
		
110
		foreach ($keys as $key) {
111
			$property = static::snakeToCamel($key);
112
			
113
			if (isset($info[$key])) {
114
				$this->$property = $info[$key];
115
			}
116
		}
117
	}
118
	
119
	/**
120
	 * Retrieve an external iterator.
121
	 * 
122
	 * @return \Traversable
123
	 */
124
	public function getIterator() {
125
		return new ArrayIterator($this->data);
126
	}
127
	
128
	/**
129
	 * Dynamically retrieve the given property.
130
	 * 
131
	 * @param string $property
132
	 * @return mixed
133
	 */
134
	public function __get($property) {
135
		return $this->$property;
136
	}
137
	
138
	/**
139
	 * Dynamically determine whether the given property exists.
140
	 * 
141
	 * @param string $property
142
	 * @return bool
143
	 */
144
	public function __isset($property) {
145
		return isset($this->$property);
146
	}
147
}
148