Completed
Push — master ( 3662e6...2c1d81 )
by Chris
02:54
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
/**
5
 * Darya's abstract storage query result.
6
 * 
7
 * TODO: Make this iterable for result data.
8
 * 
9
 * @property array  $data     Result data
10
 * @property object $query    Query that produced this result
11
 * @property int    $count    Result count
12
 * @property object $error    Result error
13
 * @property array  $fields   Field names for each result data row
14
 * @property int    $insertId Insert ID
15
 * @property int    $affected Rows affected
16
 * 
17
 * @author Chris Andrew <[email protected]>
18
 */
19
abstract class AbstractResult {
20
	
21
	/**
22
	 * The storage query that produced this result.
23
	 * 
24
	 * @var mixed
25
	 */
26
	protected $query;
27
	
28
	/**
29
	 * An associative array of the result data.
30
	 * 
31
	 * @var array
32
	 */
33
	protected $data;
34
	
35
	/**
36
	 * The error that occurred when executing the query, if any.
37
	 * 
38
	 * @var mixed|null
39
	 */
40
	protected $error;
41
	
42
	/**
43
	 * The number of rows in the result data.
44
	 * 
45
	 * @var int
46
	 */
47
	protected $count;
48
	
49
	/**
50
	 * The set of fields available for each row in the result.
51
	 * 
52
	 * @var array
53
	 */
54
	protected $fields;
55
	
56
	/**
57
	 * Auto incremented primary key of an inserted row.
58
	 * 
59
	 * @var int
60
	 */
61
	protected $insertId;
62
	
63
	/**
64
	 * Convert a string from snake_case to camelCase.
65
	 * 
66
	 * @param string $string
67
	 * @return string
68
	 */
69
	protected static function snakeToCamel($string) {
70
		return preg_replace_callback('/_(.)/', function($matches) {
71
			return strtoupper($matches[1]);
72
		}, $string);
73
	}
74
	
75
	/**
76
	 * Set the result info.
77
	 * 
78
	 * Accepts the keys 'affected', 'count', 'insert_id' and 'fields'.
79
	 * 
80
	 * @param array $info
81
	 */
82
	protected function setInfo(array $info) {
83
		$defaults = array(
84
			'count' => 0,
85
			'fields' => array(),
86
			'affected' => 0,
87
			'insert_id' => 0
88
		);
89
		
90
		foreach ($defaults as $key => $default) {
91
			$property = static::snakeToCamel($key);
92
			$this->$property = isset($info[$key]) ? $info[$key] : $default;
93
		}
94
	}
95
	
96
	/**
97
	 * Dynamically retrieve the given property.
98
	 * 
99
	 * @param string $property
100
	 * @return mixed
101
	 */
102
	public function __get($property) {
103
		return $this->$property;
104
	}
105
	
106
}
107