From 360f8799cf5de57c87e47f7e9c21c5d09e9af2ff Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 28 Aug 2008 02:02:21 +0000 Subject: [PATCH] - added configuration option 'resultClass' - updated class Object --- dibi/Nette/Object.php | 27 ++++++++++++++++++--------- dibi/libs/DibiConnection.php | 18 +++++++++++++++--- dibi/libs/DibiResult.php | 10 +++++----- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/dibi/Nette/Object.php b/dibi/Nette/Object.php index 78a80abe..406faba0 100644 --- a/dibi/Nette/Object.php +++ b/dibi/Nette/Object.php @@ -224,6 +224,7 @@ abstract class Object } // property getter support + $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character $m = 'get' . $name; if (self::hasAccessor($class, $m)) { // ampersands: @@ -231,10 +232,16 @@ abstract class Object // - doesn't call &$this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value'; $val = $this->$m(); return $val; - - } else { - throw new /*::*/MemberAccessException("Cannot read an undeclared property $class::\$$name."); } + + $m = 'is' . $name; + if (self::hasAccessor($class, $m)) { + $val = $this->$m(); + return $val; + } + + $name = func_get_arg(0); + throw new /*::*/MemberAccessException("Cannot read an undeclared property $class::\$$name."); } @@ -256,18 +263,21 @@ abstract class Object } // property setter support - if (self::hasAccessor($class, 'get' . $name)) { + $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character + if (self::hasAccessor($class, 'get' . $name) || self::hasAccessor($class, 'is' . $name)) { $m = 'set' . $name; if (self::hasAccessor($class, $m)) { $this->$m($value); + return; } else { + $name = func_get_arg(0); throw new /*::*/MemberAccessException("Cannot assign to a read-only property $class::\$$name."); } - - } else { - throw new /*::*/MemberAccessException("Cannot assign to an undeclared property $class::\$$name."); } + + $name = func_get_arg(0); + throw new /*::*/MemberAccessException("Cannot assign to an undeclared property $class::\$$name."); } @@ -280,6 +290,7 @@ abstract class Object */ public function __isset($name) { + $name[0] = $name[0] & "\xDF"; return $name !== '' && self::hasAccessor(get_class($this), 'get' . $name); } @@ -318,8 +329,6 @@ abstract class Object // (works good since 5.0.4) $cache[$c] = array_flip(get_class_methods($c)); } - // case-sensitive checking, capitalize the fourth character - $m[3] = $m[3] & "\xDF"; return isset($cache[$c][$m]); } diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index e1395087..1fc3ee2b 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -53,6 +53,12 @@ class DibiConnection extends /*Nette::*/Object */ private $inTxn = FALSE; + /** + * Result set encapsulation. + * @var string + */ + private $resultClass = 'DibiResult'; + /** @@ -103,12 +109,18 @@ class DibiConnection extends /*Nette::*/Object unset($config['result:objects']); } - if (isset($config['resultObjects'])) { - // normalize + if (isset($config['resultObjects'])) { // normalize $val = $config['resultObjects']; $config['resultObjects'] = is_string($val) && !is_numeric($val) ? $val : (bool) $val; } + if (isset($config['resultClass'])) { + if (strcasecmp($config['resultClass'], 'DibiResult') && !is_subclass_of($config['resultClass'], 'DibiResult')) { + throw new InvalidArgumentException("Class '$config[resultClass]' is not DibiResult descendant."); + } + $this->resultClass = $config['resultClass']; + } + $config['name'] = $name; $this->config = $config; $this->driver = new $class; @@ -294,7 +306,7 @@ class DibiConnection extends /*Nette::*/Object dibi::notify($this, 'beforeQuery', $sql); if ($res = $this->driver->query($sql)) { // intentionally = - $res = new DibiResult($res, $this->config); + $res = new $this->resultClass($res, $this->config); } $time += microtime(TRUE); diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 5e1278b4..5c4ceb91 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -21,7 +21,7 @@ /** - * dibi result-set abstract class. + * dibi result-set. * * * $result = dibi::query('SELECT * FROM [table]'); @@ -288,7 +288,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource * * @return mixed value on success, FALSE if no next record */ - final function fetchSingle() + final public function fetchSingle() { $row = $this->getDriver()->fetch(TRUE); if (!is_array($row)) return FALSE; @@ -315,7 +315,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource * @param bool simplify one-column result set? * @return array */ - final function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE) + final public function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE) { $limit = $limit === NULL ? -1 : (int) $limit; $this->seek((int) $offset); @@ -354,7 +354,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource * @return array * @throws InvalidArgumentException */ - final function fetchAssoc($assoc) + final public function fetchAssoc($assoc) { $this->seek(0); $row = $this->fetch(FALSE); @@ -437,7 +437,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource * @return array * @throws InvalidArgumentException */ - final function fetchPairs($key = NULL, $value = NULL) + final public function fetchPairs($key = NULL, $value = NULL) { $this->seek(0); $row = $this->fetch(FALSE);