From dce6547851eb20c8b94ea9774b31988ca75227c7 Mon Sep 17 00:00:00 2001 From: Simple AI Date: Sun, 28 Aug 2016 20:59:39 +0000 Subject: [PATCH] Update sparrow.php 'BINARY' (case sensitivity) option added to 'WHERE' clause. --- sparrow.php | 347 ++++++++++++++++++++++++++++------------------------ 1 file changed, 188 insertions(+), 159 deletions(-) diff --git a/sparrow.php b/sparrow.php index 5d9ae4b..1bfccba 100644 --- a/sparrow.php +++ b/sparrow.php @@ -1,11 +1,13 @@ * @license MIT, http://www.opensource.org/licenses/mit-license.php */ -class Sparrow { +class Sparrow +{ protected $table; protected $where; protected $joins; @@ -44,7 +46,8 @@ class Sparrow { /** * Class constructor. */ - public function __construct() { + public function __construct() + { } /*** Core Methods ***/ @@ -56,8 +59,9 @@ public function __construct() { * @param string $input Input string to append * @return string New SQL statement */ - public function build($sql, $input) { - return (strlen($input) > 0) ? ($sql.' '.$input) : $sql; + public function build($sql, $input) + { + return (strlen($input) > 0) ? ($sql . ' ' . $input) : $sql; } /** @@ -67,7 +71,8 @@ public function build($sql, $input) { * @return array Connection information * @throws Exception For invalid connection string */ - public function parseConnection($connection) { + public function parseConnection($connection) + { $url = parse_url($connection); if (empty($url)) { @@ -78,7 +83,7 @@ public function parseConnection($connection) { $cfg['type'] = isset($url['scheme']) ? $url['scheme'] : $url['path']; $cfg['hostname'] = isset($url['host']) ? $url['host'] : null; - $cfg['database'] = isset($url['path']) ? substr($url['path'],1) : null; + $cfg['database'] = isset($url['path']) ? substr($url['path'], 1) : null; $cfg['username'] = isset($url['user']) ? $url['user'] : null; $cfg['password'] = isset($url['pass']) ? $url['pass'] : null; $cfg['port'] = isset($url['port']) ? $url['port'] : null; @@ -89,7 +94,8 @@ public function parseConnection($connection) { /** * Gets the query statistics. */ - public function getStats() { + public function getStats() + { $this->stats['total_time'] = 0; $this->stats['num_queries'] = 0; $this->stats['num_rows'] = 0; @@ -114,7 +120,8 @@ public function getStats() { /** * Checks whether the table property has been set. */ - public function checkTable() { + public function checkTable() + { if (!$this->table) { throw new Exception('Table is not defined.'); } @@ -123,7 +130,8 @@ public function checkTable() { /** * Checks whether the class property has been set. */ - public function checkClass() { + public function checkClass() + { if (!$this->class) { throw new Exception('Class is not defined.'); } @@ -132,7 +140,8 @@ public function checkClass() { /** * Resets class properties. */ - public function reset() { + public function reset() + { $this->where = ''; $this->joins = ''; $this->order = ''; @@ -156,9 +165,10 @@ public function reset() { * @return string Condition as a string * @throws Exception For invalid where condition */ - protected function parseCondition($field, $value = null, $join = '', $escape = true) { + protected function parseCondition($field, $value = null, $join = '', $escape = true) + { if (is_string($field)) { - if ($value === null) return $join.' '.trim($field); + if ($value === null) return $join . ' ' . trim($field); $operator = ''; @@ -187,34 +197,30 @@ protected function parseCondition($field, $value = null, $join = '', $escape = t default: $condition = $operator; } - } - else { + } else { $condition = '='; } - if (empty($join)) { + if (empty($join)) { $join = ($field{0} == '|') ? ' OR' : ' AND'; } if (is_array($value)) { if (strpos($operator, '@') === false) $condition = ' IN '; - $value = '('.implode(',', array_map(array($this, 'quote'), $value)).')'; - } - else { + $value = '(' . implode(',', array_map(array($this, 'quote'), $value)) . ')'; + } else { $value = ($escape && !is_numeric($value)) ? $this->quote($value) : $value; } - return $join.' '.str_replace('|', '', $field).$condition.$value; - } - else if (is_array($field)) { + return $join . ' ' . str_replace('|', '', $field) . $condition . $value; + } else if (is_array($field)) { $str = ''; foreach ($field as $key => $value) { $str .= $this->parseCondition($key, $value, $join, $escape); $join = ''; } return $str; - } - else { + } else { throw new Exception('Invalid where condition.'); } } @@ -226,7 +232,8 @@ protected function parseCondition($field, $value = null, $join = '', $escape = t * @param boolean $reset Reset class properties * @return object Self reference */ - public function from($table, $reset = true) { + public function from($table, $reset = true) + { $this->table = $table; if ($reset) { $this->reset(); @@ -244,7 +251,8 @@ public function from($table, $reset = true) { * @return object Self reference * @throws Exception For invalid join type */ - public function join($table, array $fields, $type = 'INNER') { + public function join($table, array $fields, $type = 'INNER') + { static $joins = array( 'INNER', 'LEFT OUTER', @@ -256,7 +264,7 @@ public function join($table, array $fields, $type = 'INNER') { throw new Exception('Invalid join type.'); } - $this->joins .= ' '.$type.' JOIN '.$table. + $this->joins .= ' ' . $type . ' JOIN ' . $table . $this->parseCondition($fields, null, ' ON', false); return $this; @@ -269,7 +277,8 @@ public function join($table, array $fields, $type = 'INNER') { * @param array $fields Fields to join on * @return object Self reference */ - public function leftJoin($table, array $fields) { + public function leftJoin($table, array $fields) + { return $this->join($table, $fields, 'LEFT OUTER'); } @@ -280,7 +289,8 @@ public function leftJoin($table, array $fields) { * @param array $fields Fields to join on * @return object Self reference */ - public function rightJoin($table, array $fields) { + public function rightJoin($table, array $fields) + { return $this->join($table, $fields, 'RIGHT OUTER'); } @@ -291,7 +301,8 @@ public function rightJoin($table, array $fields) { * @param array $fields Fields to join on * @return object Self reference */ - public function fullJoin($table, array $fields) { + public function fullJoin($table, array $fields) + { return $this->join($table, $fields, 'FULL OUTER'); } @@ -299,11 +310,13 @@ public function fullJoin($table, array $fields) { * Adds where conditions. * * @param string|array $field A field name or an array of fields and values. + * @param boolean $binary adds case sensitivity to the WHERE clause * @param string $value A field value to compare to * @return object Self reference */ - public function where($field, $value = null) { - $join = (empty($this->where)) ? 'WHERE' : ''; + public function where($field, $binary = false, $value = null) + { + $join = (empty($this->where)) ? $binary ? 'WHERE BINARY' : 'WHERE' : ''; $this->where .= $this->parseCondition($field, $value, $join); return $this; @@ -314,8 +327,9 @@ public function where($field, $value = null) { * * @param string $field Field name * @return object Self reference - */ - public function sortAsc($field) { + */ + public function sortAsc($field) + { return $this->orderBy($field, 'ASC'); } @@ -324,9 +338,10 @@ public function sortAsc($field) { * * @param string $field Field name * @return object Self reference - */ - public function sortDesc($field) { - return $this->orderBy($field, 'DESC'); + */ + public function sortDesc($field) + { + return $this->orderBy($field, 'DESC'); } /** @@ -336,21 +351,21 @@ public function sortDesc($field) { * @param string $direction Sort direction * @return object Self reference */ - public function orderBy($field, $direction = 'ASC') { + public function orderBy($field, $direction = 'ASC') + { $join = (empty($this->order)) ? 'ORDER BY' : ','; if (is_array($field)) { foreach ($field as $key => $value) { - $field[$key] = $value.' '.$direction; + $field[$key] = $value . ' ' . $direction; } - } - else { - $field .= ' '.$direction; + } else { + $field .= ' ' . $direction; } $fields = (is_array($field)) ? implode(', ', $field) : $field; - $this->order .= $join.' '.$fields; + $this->order .= $join . ' ' . $fields; return $this; } @@ -361,11 +376,12 @@ public function orderBy($field, $direction = 'ASC') { * @param string|array $field Field name or array of field names * @return object Self reference */ - public function groupBy($field) { + public function groupBy($field) + { $join = (empty($this->order)) ? 'GROUP BY' : ','; $fields = (is_array($field)) ? implode(',', $field) : $field; - $this->groups .= $join.' '.$fields; + $this->groups .= $join . ' ' . $fields; return $this; } @@ -377,7 +393,8 @@ public function groupBy($field) { * @param string $value A field value to compare to * @return object Self reference */ - public function having($field, $value = null) { + public function having($field, $value = null) + { $join = (empty($this->having)) ? 'HAVING' : ''; $this->having .= $this->parseCondition($field, $value, $join); @@ -391,9 +408,10 @@ public function having($field, $value = null) { * @param int $offset Number of rows to offset * @return object Self reference */ - public function limit($limit, $offset = null) { + public function limit($limit, $offset = null) + { if ($limit !== null) { - $this->limit = 'LIMIT '.$limit; + $this->limit = 'LIMIT ' . $limit; } if ($offset !== null) { $this->offset($offset); @@ -409,9 +427,10 @@ public function limit($limit, $offset = null) { * @param int $limit Number of rows to limit * @return object Self reference */ - public function offset($offset, $limit = null) { + public function offset($offset, $limit = null) + { if ($offset !== null) { - $this->offset = 'OFFSET '.$offset; + $this->offset = 'OFFSET ' . $offset; } if ($limit !== null) { $this->limit($limit); @@ -423,7 +442,8 @@ public function offset($offset, $limit = null) { /** * Sets the distinct keyword for a query. */ - public function distinct($value = true) { + public function distinct($value = true) + { $this->distinct = ($value) ? 'DISTINCT' : ''; return $this; @@ -436,7 +456,8 @@ public function distinct($value = true) { * @param string $value1 First value * @param string $value2 Second value */ - public function between($field, $value1, $value2) { + public function between($field, $value1, $value2) + { $this->where(sprintf( '%s BETWEEN %s AND %s', $field, @@ -453,7 +474,8 @@ public function between($field, $value1, $value2) { * @param int $offset Offset condition * @return object Self reference */ - public function select($fields = '*', $limit = null, $offset = null) { + public function select($fields = '*', $limit = null, $offset = null) + { $this->checkTable(); $fields = (is_array($fields)) ? implode(',', $fields) : $fields; @@ -483,7 +505,8 @@ public function select($fields = '*', $limit = null, $offset = null) { * @param array $data Array of key and values to insert * @return object Self reference */ - public function insert(array $data) { + public function insert(array $data) + { $this->checkTable(); if (empty($data)) return $this; @@ -499,9 +522,9 @@ public function insert(array $data) { $this->sql(array( 'INSERT INTO', $this->table, - '('.$keys.')', + '(' . $keys . ')', 'VALUES', - '('.$values.')' + '(' . $values . ')' )); return $this; @@ -513,7 +536,8 @@ public function insert(array $data) { * @param string|array $data Array of keys and values, or string literal * @return object Self reference */ - public function update($data) { + public function update($data) + { $this->checkTable(); if (empty($data)) return $this; @@ -522,10 +546,9 @@ public function update($data) { if (is_array($data)) { foreach ($data as $key => $value) { - $values[] = (is_numeric($key)) ? $value : $key.'='.$this->quote($value); + $values[] = (is_numeric($key)) ? $value : $key . '=' . $this->quote($value); } - } - else { + } else { $values[] = (string)$data; } @@ -546,7 +569,8 @@ public function update($data) { * @param array $where Where conditions * @return object Self reference */ - public function delete($where = null) { + public function delete($where = null) + { $this->checkTable(); if ($where !== null) { @@ -568,7 +592,8 @@ public function delete($where = null) { * @param string|array SQL statement * @return string SQL statement */ - public function sql($sql = null) { + public function sql($sql = null) + { if ($sql !== null) { $this->sql = trim( (is_array($sql)) ? @@ -590,14 +615,14 @@ public function sql($sql = null) { * @param string|array|object $db Database connection string, array or object * @throws Exception For connection error */ - public function setDb($db) { + public function setDb($db) + { $this->db = null; // Connection string if (is_string($db)) { $this->setDb($this->parseConnection($db)); - } - // Connection information + } // Connection information else if (is_array($db)) { switch ($db['type']) { case 'mysqli': @@ -609,7 +634,7 @@ public function setDb($db) { ); if ($this->db->connect_error) { - throw new Exception('Connection error: '.$this->db->connect_error); + throw new Exception('Connection error: ' . $this->db->connect_error); } break; @@ -622,7 +647,7 @@ public function setDb($db) { ); if (!$this->db) { - throw new Exception('Connection error: '.mysql_error()); + throw new Exception('Connection error: ' . mysql_error()); } mysql_select_db($db['database'], $this->db); @@ -646,7 +671,7 @@ public function setDb($db) { $this->db = sqlite_open($db['database'], 0666, $error); if (!$this->db) { - throw new Exception('Connection error: '.$error); + throw new Exception('Connection error: ' . $error); } break; @@ -685,10 +710,10 @@ public function setDb($db) { break; case 'pdosqlite': - $this->db = new PDO('sqlite:/'.$db['database']); + $this->db = new PDO('sqlite:/' . $db['database']); $db['type'] = 'pdo'; - break; + break; } if ($this->db == null) { @@ -696,8 +721,7 @@ public function setDb($db) { } $this->db_type = $db['type']; - } - // Connection object or resource + } // Connection object or resource else { $type = $this->getDbType($db); @@ -715,21 +739,22 @@ public function setDb($db) { * * @return object Database connection */ - public function getDb() { + public function getDb() + { return $this->db; } /** * Gets the database type. - * + * * @param object|resource $db Database object or resource * @return string Database type */ - public function getDbType($db) { + public function getDbType($db) + { if (is_object($db)) { return strtolower(get_class($db)); - } - else if (is_resource($db)) { + } else if (is_resource($db)) { switch (get_resource_type($db)) { case 'mysql link': return 'mysql'; @@ -753,7 +778,8 @@ public function getDbType($db) { * @return object Query results object * @throws Exception When database is not defined */ - public function execute($key = null, $expire = 0) { + public function execute($key = null, $expire = 0) + { if (!$this->db) { throw new Exception('Database is not defined.'); } @@ -794,16 +820,14 @@ public function execute($key = null, $expire = 0) { if (!$result) { $error = $this->db->errorInfo(); - } - else { + } else { $result->execute(); $this->num_rows = $result->rowCount(); $this->affected_rows = $result->rowCount(); $this->insert_id = $this->db->lastInsertId(); } - } - catch (PDOException $ex) { + } catch (PDOException $ex) { $error = $ex->getMessage(); } @@ -814,12 +838,10 @@ public function execute($key = null, $expire = 0) { if (!$result) { $error = $this->db->error; - } - else { + } else { if (is_object($result)) { $this->num_rows = $result->num_rows; - } - else { + } else { $this->affected_rows = $this->db->affected_rows; } $this->insert_id = $this->db->insert_id; @@ -832,12 +854,10 @@ public function execute($key = null, $expire = 0) { if (!$result) { $error = mysql_error(); - } - else { + } else { if (!is_bool($result)) { $this->num_rows = mysql_num_rows($result); - } - else { + } else { $this->affected_rows = mysql_affected_rows($this->db); } $this->insert_id = mysql_insert_id($this->db); @@ -849,9 +869,8 @@ public function execute($key = null, $expire = 0) { $result = pg_query($this->db, $this->sql); if (!$result) { - $error = pg_last_error($this->db); - } - else { + $error = pg_last_error($this->db); + } else { $this->num_rows = pg_num_rows($result); $this->affected_rows = pg_affected_rows($result); $this->insert_id = pg_last_oid($result); @@ -875,8 +894,7 @@ public function execute($key = null, $expire = 0) { if ($result === false) { $error = $this->db->lastErrorMsg(); - } - else { + } else { $this->num_rows = 0; $this->affected_rows = ($result) ? $this->db->changes() : 0; $this->insert_id = $this->db->lastInsertRowId(); @@ -887,9 +905,9 @@ public function execute($key = null, $expire = 0) { if ($error !== null) { if ($this->show_sql) { - $error .= "\nSQL: ".$this->sql; + $error .= "\nSQL: " . $this->sql; } - throw new Exception('Database error: '.$error); + throw new Exception('Database error: ' . $error); } } @@ -913,7 +931,8 @@ public function execute($key = null, $expire = 0) { * @param int $expire Expiration time in seconds * @return array Rows */ - public function many($key = null, $expire = 0) { + public function many($key = null, $expire = 0) + { if (empty($this->sql)) { $this->select(); } @@ -926,29 +945,27 @@ public function many($key = null, $expire = 0) { $data = $result; if ($this->stats_enabled) { - $this->stats['cached'][$this->key_prefix.$key] = $this->sql; + $this->stats['cached'][$this->key_prefix . $key] = $this->sql; } - } - else { + } else { switch ($this->db_type) { case 'pdo': $data = $result->fetchAll(PDO::FETCH_ASSOC); $this->num_rows = sizeof($data); - + break; case 'mysqli': if (function_exists('mysqli_fetch_all')) { $data = $result->fetch_all(MYSQLI_ASSOC); - } - else { + } else { while ($row = $result->fetch_assoc()) { $data[] = $row; } } $result->close(); break; - + case 'mysql': while ($row = mysql_fetch_assoc($result)) { $data[] = $row; @@ -991,7 +1008,8 @@ public function many($key = null, $expire = 0) { * @param int $expire Expiration time in seconds * @return array Row */ - public function one($key = null, $expire = 0) { + public function one($key = null, $expire = 0) + { if (empty($this->sql)) { $this->limit(1)->select(); } @@ -1011,7 +1029,8 @@ public function one($key = null, $expire = 0) { * @param int $expire Expiration time in seconds * @return mixed Row value */ - public function value($name, $key = null, $expire = 0) { + public function value($name, $key = null, $expire = 0) + { $row = $this->one($key, $expire); $value = (!empty($row)) ? $row[$name] : null; @@ -1027,8 +1046,9 @@ public function value($name, $key = null, $expire = 0) { * @param string $key Cache key * @return object Self reference */ - public function min($field, $key = null, $expire = 0) { - $this->select('MIN('.$field.') min_value'); + public function min($field, $key = null, $expire = 0) + { + $this->select('MIN(' . $field . ') min_value'); return $this->value( 'min_value', @@ -1045,8 +1065,9 @@ public function min($field, $key = null, $expire = 0) { * @param string $key Cache key * @return object Self reference */ - public function max($field, $key = null, $expire = 0) { - $this->select('MAX('.$field.') max_value'); + public function max($field, $key = null, $expire = 0) + { + $this->select('MAX(' . $field . ') max_value'); return $this->value( 'max_value', @@ -1063,8 +1084,9 @@ public function max($field, $key = null, $expire = 0) { * @param string $key Cache key * @return object Self reference */ - public function sum($field, $key = null, $expire = 0) { - $this->select('SUM('.$field.') sum_value'); + public function sum($field, $key = null, $expire = 0) + { + $this->select('SUM(' . $field . ') sum_value'); return $this->value( 'sum_value', @@ -1081,14 +1103,15 @@ public function sum($field, $key = null, $expire = 0) { * @param string $key Cache key * @return object Self reference */ - public function avg($field, $key = null, $expire = 0) { - $this->select('AVG('.$field.') avg_value'); + public function avg($field, $key = null, $expire = 0) + { + $this->select('AVG(' . $field . ') avg_value'); return $this->value( 'avg_value', $key, $expire - ); + ); } /** @@ -1099,8 +1122,9 @@ public function avg($field, $key = null, $expire = 0) { * @param int $expire Expiration time in seconds * @return object Self reference */ - public function count($field = '*', $key = null, $expire = 0) { - $this->select('COUNT('.$field.') num_rows'); + public function count($field = '*', $key = null, $expire = 0) + { + $this->select('COUNT(' . $field . ') num_rows'); return $this->value( 'num_rows', @@ -1115,7 +1139,8 @@ public function count($field = '*', $key = null, $expire = 0) { * @param mixed $value mixed value * @return mixed Quoted value */ - public function quote($value) { + public function quote($value) + { if ($value === null) return 'NULL'; if (is_string($value)) { @@ -1125,19 +1150,19 @@ public function quote($value) { return $this->db->quote($value); case 'mysqli': - return "'".$this->db->real_escape_string($value)."'"; + return "'" . $this->db->real_escape_string($value) . "'"; case 'mysql': - return "'".mysql_real_escape_string($value, $this->db)."'"; + return "'" . mysql_real_escape_string($value, $this->db) . "'"; case 'pgsql': - return "'".pg_escape_string($this->db, $value)."'"; + return "'" . pg_escape_string($this->db, $value) . "'"; case 'sqlite': - return "'".sqlite_escape_string($value)."'"; + return "'" . sqlite_escape_string($value) . "'"; case 'sqlite3': - return "'".$this->db->escapeString($value)."'"; + return "'" . $this->db->escapeString($value) . "'"; } } @@ -1161,7 +1186,8 @@ public function quote($value) { * @param string|object $cache Cache connection string or object * @throws Exception For invalid cache type */ - public function setCache($cache) { + public function setCache($cache) + { $this->cache = null; // Connection string @@ -1169,12 +1195,10 @@ public function setCache($cache) { if ($cache{0} == '.' || $cache{0} == '/') { $this->cache = $cache; $this->cache_type = 'file'; - } - else { + } else { $this->setCache($this->parseConnection($cache)); } - } - // Connection information + } // Connection information else if (is_array($cache)) { switch ($cache['type']) { case 'memcache': @@ -1198,8 +1222,7 @@ public function setCache($cache) { } $this->cache_type = $cache['type']; - } - // Cache object + } // Cache object else if (is_object($cache)) { $type = strtolower(get_class($cache)); @@ -1217,7 +1240,8 @@ public function setCache($cache) { * * @return object Cache instance */ - public function getCache() { + public function getCache() + { return $this->cache; } @@ -1228,8 +1252,9 @@ public function getCache() { * @param mixed $value Value to store * @param int $expire Expiration time in seconds */ - public function store($key, $value, $expire = 0) { - $key = $this->key_prefix.$key; + public function store($key, $value, $expire = 0) + { + $key = $this->key_prefix . $key; switch ($this->cache_type) { case 'memcached': @@ -1249,7 +1274,7 @@ public function store($key, $value, $expire = 0) { break; case 'file': - $file = $this->cache.'/'.md5($key); + $file = $this->cache . '/' . md5($key); $data = array( 'value' => $value, 'expire' => ($expire > 0) ? (time() + $expire) : 0 @@ -1268,8 +1293,9 @@ public function store($key, $value, $expire = 0) { * @param string $key Cache key * @return mixed Cached value */ - public function fetch($key) { - $key = $this->key_prefix.$key; + public function fetch($key) + { + $key = $this->key_prefix . $key; switch ($this->cache_type) { case 'memcached': @@ -1290,14 +1316,13 @@ public function fetch($key) { return xcache_get($key); case 'file': - $file = $this->cache.'/'.md5($key); + $file = $this->cache . '/' . md5($key); if ($this->is_cached = file_exists($file)) { $data = unserialize(file_get_contents($file)); if ($data['expire'] == 0 || time() < $data['expire']) { return $data['value']; - } - else { + } else { $this->is_cached = false; } } @@ -1315,8 +1340,9 @@ public function fetch($key) { * @param string $key Cache key * @return object Self reference */ - public function clear($key) { - $key = $this->key_prefix.$key; + public function clear($key) + { + $key = $this->key_prefix . $key; switch ($this->cache_type) { case 'memcached': @@ -1332,7 +1358,7 @@ public function clear($key) { return xcache_unset($key); case 'file': - $file = $this->cache.'/'.md5($key); + $file = $this->cache . '/' . md5($key); if (file_exists($file)) { return unlink($file); } @@ -1350,7 +1376,8 @@ public function clear($key) { /** * Flushes out the cache. */ - public function flush() { + public function flush() + { switch ($this->cache_type) { case 'memcached': $this->cache->flush(); @@ -1372,7 +1399,7 @@ public function flush() { if ($handle = opendir($this->cache)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { - unlink($this->cache.'/'.$file); + unlink($this->cache . '/' . $file); } } closedir($handle); @@ -1393,11 +1420,11 @@ public function flush() { * @param string|object $class Class name or instance * @return object Self reference */ - public function using($class) { + public function using($class) + { if (is_string($class)) { $this->class = $class; - } - else if (is_object($class)) { + } else if (is_object($class)) { $this->class = get_class($class); } @@ -1413,7 +1440,8 @@ public function using($class) { * @param array $data Property data * @return object Populated object */ - public function load($object, array $data) { + public function load($object, array $data) + { foreach ($data as $key => $value) { if (property_exists($object, $key)) { $object->$key = $value; @@ -1422,7 +1450,7 @@ public function load($object, array $data) { return $object; } - + /** * Finds and populates an object. * @@ -1430,7 +1458,8 @@ public function load($object, array $data) { * @param string $key Cache key * @return object Populated object */ - public function find($value = null, $key = null) { + public function find($value = null, $key = null) + { $this->checkClass(); $properties = $this->getProperties(); @@ -1440,11 +1469,9 @@ public function find($value = null, $key = null) { if ($value !== null) { if (is_int($value) && property_exists($properties, 'id_field')) { $this->where($properties->id_field, $value); - } - else if (is_string($value) && property_exists($properties, 'name_field')) { + } else if (is_string($value) && property_exists($properties, 'name_field')) { $this->where($properties->name_field, $value); - } - else if (is_array($value)) { + } else if (is_array($value)) { $this->where($value); } } @@ -1469,7 +1496,8 @@ public function find($value = null, $key = null) { * @param object $object Class instance * @param array $fields Select database fields to save */ - public function save($object, array $fields = null) { + public function save($object, array $fields = null) + { $this->using($object); $properties = $this->getProperties(); @@ -1486,8 +1514,7 @@ public function save($object, array $fields = null) { ->execute(); $object->{$properties->id_field} = $this->insert_id; - } - else { + } else { if ($fields !== null) { $keys = array_flip($fields); $data = array_intersect_key($data, $keys); @@ -1506,7 +1533,8 @@ public function save($object, array $fields = null) { * * @param object $object Class instance */ - public function remove($object) { + public function remove($object) + { $this->using($object); $properties = $this->getProperties(); @@ -1527,7 +1555,8 @@ public function remove($object) { * * @return object Class properties */ - public function getProperties() { + public function getProperties() + { static $properties = array(); if (!$this->class) return array();