| @@ 22-135 (lines=114) @@ | ||
| 19 | * |
|
| 20 | * @author Chris Andrew <[email protected]> |
|
| 21 | */ |
|
| 22 | class Result { |
|
| 23 | ||
| 24 | /** |
|
| 25 | * The database query that produced this result. |
|
| 26 | * |
|
| 27 | * @var Query |
|
| 28 | */ |
|
| 29 | protected $query; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * Associative array of the result's data. |
|
| 33 | * |
|
| 34 | * @var array |
|
| 35 | */ |
|
| 36 | protected $data; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * Number of rows affected by the query. Only applies to queries that modify |
|
| 40 | * the database. |
|
| 41 | * |
|
| 42 | * @var int |
|
| 43 | */ |
|
| 44 | protected $affected; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Number of rows in the result data. |
|
| 48 | * |
|
| 49 | * @var int |
|
| 50 | */ |
|
| 51 | protected $count; |
|
| 52 | ||
| 53 | /** |
|
| 54 | * Set of fields available for each row in the result. |
|
| 55 | * |
|
| 56 | * @var array |
|
| 57 | */ |
|
| 58 | protected $fields; |
|
| 59 | ||
| 60 | /** |
|
| 61 | * Auto incremented primary key of an inserted row. |
|
| 62 | * |
|
| 63 | * @var int |
|
| 64 | */ |
|
| 65 | protected $insertId; |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Error captured from the query that produced this result. |
|
| 69 | * |
|
| 70 | * @var Error |
|
| 71 | */ |
|
| 72 | protected $error; |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Convert a string from snake_case to camelCase. |
|
| 76 | * |
|
| 77 | * @param string $string |
|
| 78 | * @return string |
|
| 79 | */ |
|
| 80 | protected static function snakeToCamel($string) { |
|
| 81 | return preg_replace_callback('/_(.)/', function($matches) { |
|
| 82 | return strtoupper($matches[1]); |
|
| 83 | }, $string); |
|
| 84 | } |
|
| 85 | ||
| 86 | /** |
|
| 87 | * Instantiate a new database result. |
|
| 88 | * |
|
| 89 | * $info accepts the keys 'affected', 'count', 'insert_id' and 'fields'. |
|
| 90 | * |
|
| 91 | * @param Query $query |
|
| 92 | * @param array $data [optional] |
|
| 93 | * @param array $info [optional] |
|
| 94 | * @param Error $error [optional] |
|
| 95 | */ |
|
| 96 | public function __construct(Query $query, array $data = array(), array $info = array(), Error $error = null) { |
|
| 97 | $this->data = $data; |
|
| 98 | $this->error = $error; |
|
| 99 | $this->query = $query; |
|
| 100 | ||
| 101 | $this->setInfo($info); |
|
| 102 | } |
|
| 103 | ||
| 104 | /** |
|
| 105 | * Set the result info. |
|
| 106 | * |
|
| 107 | * Accepts the keys 'affected', 'count', 'insert_id' and 'fields'. |
|
| 108 | * |
|
| 109 | * @param array $info |
|
| 110 | */ |
|
| 111 | protected function setInfo(array $info) { |
|
| 112 | $defaults = array( |
|
| 113 | 'count' => 0, |
|
| 114 | 'fields' => array(), |
|
| 115 | 'affected' => 0, |
|
| 116 | 'insert_id' => 0 |
|
| 117 | ); |
|
| 118 | ||
| 119 | foreach ($defaults as $key => $default) { |
|
| 120 | $property = static::snakeToCamel($key); |
|
| 121 | $this->$property = isset($info[$key]) ? $info[$key] : $default; |
|
| 122 | } |
|
| 123 | } |
|
| 124 | ||
| 125 | /** |
|
| 126 | * Dynamically retrieve the given property. |
|
| 127 | * |
|
| 128 | * @param string $property |
|
| 129 | * @return mixed |
|
| 130 | */ |
|
| 131 | public function __get($property) { |
|
| 132 | return $this->$property; |
|
| 133 | } |
|
| 134 | ||
| 135 | } |
|
| 136 | ||
| @@ 20-130 (lines=111) @@ | ||
| 17 | * |
|
| 18 | * @author Chris Andrew <[email protected]> |
|
| 19 | */ |
|
| 20 | class Result { |
|
| 21 | ||
| 22 | /** |
|
| 23 | * The storage query that produced this result. |
|
| 24 | * |
|
| 25 | * @var Query |
|
| 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 Error|null |
|
| 40 | */ |
|
| 41 | protected $error; |
|
| 42 | ||
| 43 | /** |
|
| 44 | * The number of rows in the result data. |
|
| 45 | * |
|
| 46 | * @var int |
|
| 47 | */ |
|
| 48 | protected $count; |
|
| 49 | ||
| 50 | /** |
|
| 51 | * The set of fields available for each row in the result. |
|
| 52 | * |
|
| 53 | * @var array |
|
| 54 | */ |
|
| 55 | protected $fields; |
|
| 56 | ||
| 57 | /** |
|
| 58 | * Auto incremented primary key of an inserted row. |
|
| 59 | * |
|
| 60 | * @var int |
|
| 61 | */ |
|
| 62 | protected $insertId; |
|
| 63 | ||
| 64 | /** |
|
| 65 | * Error captured from the query that produced this result. |
|
| 66 | * |
|
| 67 | * @var Error |
|
| 68 | */ |
|
| 69 | protected $error; |
|
| 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 | * Instantiate a new storage query result. |
|
| 85 | * |
|
| 86 | * @param Query $query |
|
| 87 | * @param array $data [optional] |
|
| 88 | * @param array $info [optional] |
|
| 89 | * @param Error $error [optional] |
|
| 90 | */ |
|
| 91 | public function __construct(Query $query, array $data = array(), array $info = array(), Error $error = null) { |
|
| 92 | $this->query = $query; |
|
| 93 | $this->data = $data; |
|
| 94 | $this->error = $error; |
|
| 95 | ||
| 96 | $this->setInfo($info); |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * Set the result info. |
|
| 101 | * |
|
| 102 | * Accepts the keys 'affected', 'count', 'insert_id' and 'fields'. |
|
| 103 | * |
|
| 104 | * @param array $info |
|
| 105 | */ |
|
| 106 | protected function setInfo(array $info) { |
|
| 107 | $defaults = array( |
|
| 108 | 'count' => 0, |
|
| 109 | 'fields' => array(), |
|
| 110 | 'affected' => 0, |
|
| 111 | 'insert_id' => 0 |
|
| 112 | ); |
|
| 113 | ||
| 114 | foreach ($defaults as $key => $default) { |
|
| 115 | $property = static::snakeToCamel($key); |
|
| 116 | $this->$property = isset($info[$key]) ? $info[$key] : $default; |
|
| 117 | } |
|
| 118 | } |
|
| 119 | ||
| 120 | /** |
|
| 121 | * Dynamically retrieve the given property. |
|
| 122 | * |
|
| 123 | * @param string $property |
|
| 124 | * @return mixed |
|
| 125 | */ |
|
| 126 | public function __get($property) { |
|
| 127 | return $this->$property; |
|
| 128 | } |
|
| 129 | ||
| 130 | } |
|
| 131 | ||