From 56628144b41a7ee4b2266e9eec5400d8490ddfdf Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 25 Oct 2008 00:26:56 +0000 Subject: [PATCH] - DibiFluent autodetects modifiers for associative arrays (after where, orderBy, ...) - json_encode in PHP < 5.2 --- dibi/libs/DibiFluent.php | 25 +++++++++++++++++++------ dibi/libs/DibiProfiler.php | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index b508cc74..30eb8e9d 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -38,6 +38,16 @@ class DibiFluent extends DibiObject 'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT', '%end'), ); + /** @var array */ + public static $modifiers = array( + 'VALUES' => '%l', + 'SET' => '%a', + 'WHERE' => '%and', + 'HAVING' => '%and', + 'ORDER BY' => '%by', + 'GROUP BY' => '%by', + ); + /** @var array */ public static $separators = array( 'SELECT' => ',', @@ -88,7 +98,7 @@ class DibiFluent extends DibiObject */ public function __call($clause, $args) { - $clause = self::_clause($clause); + $clause = self::_formatClause($clause); // lazy initialization if ($this->command === NULL) { @@ -104,11 +114,14 @@ class DibiFluent extends DibiObject if (count($args) === 1) { $arg = $args[0]; // TODO: really ignore TRUE? - if ($arg === TRUE) { + if ($arg === TRUE) { // flag $args = array(); - } elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) { + } elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) { // identifier $args = array('%n', $arg); + + } elseif (is_array($arg) && is_string(key($arg))) { // associative array + $args = array(isset(self::$modifiers[$clause]) ? self::$modifiers[$clause] : '%a', $arg); } } @@ -158,7 +171,7 @@ class DibiFluent extends DibiObject */ public function clause($clause, $remove = FALSE) { - $this->cursor = & $this->clauses[self::_clause($clause)]; + $this->cursor = & $this->clauses[self::_formatClause($clause)]; if ($remove) { $this->cursor = NULL; @@ -320,7 +333,7 @@ class DibiFluent extends DibiObject $data = $this->clauses; } else { - $clause = self::_clause($clause); + $clause = self::_formatClause($clause); if (array_key_exists($clause, $this->clauses)) { $data = array($clause => $this->clauses[$clause]); } else { @@ -350,7 +363,7 @@ class DibiFluent extends DibiObject * @param string * @return string */ - private static function _clause($s) + private static function _formatClause($s) { if ($s === 'order' || $s === 'group') { $s .= 'By'; diff --git a/dibi/libs/DibiProfiler.php b/dibi/libs/DibiProfiler.php index eaee26d9..0d4d6cc5 100644 --- a/dibi/libs/DibiProfiler.php +++ b/dibi/libs/DibiProfiler.php @@ -118,7 +118,7 @@ class DibiProfiler extends DibiObject implements IDibiProfiler $caption = 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)'; $payload['FirePHP.Firebug.Console'][] = array('TABLE', array($caption, self::$table)); - $payload = json_encode($payload); + $payload = function_exists('json_encode') ? json_encode($payload) : self::json_encode($payload); foreach (str_split($payload, 4998) as $num => $s) { header('X-FirePHP-Data-' . str_pad(++$num, 12, '0', STR_PAD_LEFT) . ': ' . $s); } @@ -179,4 +179,39 @@ class DibiProfiler extends DibiObject implements IDibiProfiler fclose($handle); } + + + public static function json_encode($val) + { + // indexed array + if (is_array($val) && (!$val + || array_keys($val) === range(0, count($val) - 1))) { + return '[' . implode(',', array_map(array(__CLASS__, 'json_encode'), $val)) . ']'; + } + + // associative array + if (is_array($val) || is_object($val)) { + $tmp = array(); + foreach ($val as $k => $v) { + $tmp[] = self::json_encode((string) $k) . ':' . self::json_encode($v); + } + return '{' . implode(',', $tmp) . '}'; + } + + if (is_string($val)) { + $val = str_replace(array("\\", "\x00"), array("\\\\", "\\u0000"), $val); // due to bug #40915 + return '"' . addcslashes($val, "\x8\x9\xA\xC\xD/\"") . '"'; + } + + if (is_int($val) || is_float($val)) { + return rtrim(rtrim(number_format($val, 5, '.', ''), '0'), '.'); + } + + if (is_bool($val)) { + return $val ? 'true' : 'false'; + } + + return 'null'; + } + }