From 06a6a4f777272421970bb827c898f6d36f1ab669 Mon Sep 17 00:00:00 2001 From: "Johan \"Papa\" Cwiklinski" Date: Sun, 1 Aug 2010 10:55:01 +0200 Subject: Applying PEAR coding standards, refs #48 --- class/TableIterator.php | 90 +++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 41 deletions(-) (limited to 'class/TableIterator.php') diff --git a/class/TableIterator.php b/class/TableIterator.php index 0903f37..dfae859 100644 --- a/class/TableIterator.php +++ b/class/TableIterator.php @@ -38,14 +38,22 @@ * Freely inspired from DBmysqlIterator class from GLPI * (already written by Remi, and ported to PDO) * See http://www.glpi-project.org/ + * + * @category Main + * @package RPMPHP + * + * @author Remi Collet + * @author Johan Cwiklinski + * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ */ class TableIterator implements Iterator { - private $con; - private $sql; - private $res = false; - private $row; - private $pos; + private $_con; + private $_sql; + private $_res = false; + private $_row; + private $_pos; /** * Constructor @@ -59,9 +67,9 @@ class TableIterator implements Iterator function __construct (PDO $dbconnexion, $table, $crit='') { - $this->conn = $dbconnexion; + $this->_conn = $dbconnexion; if (is_string($table) && strpos($table, " ")) { - $this->sql = $table; + $this->_sql = $table; } else { // check field, orderby, limit, start in criterias $field=""; @@ -89,55 +97,55 @@ class TableIterator implements Iterator // SELECT field list if (is_array($field)) { - $this->sql = ""; + $this->_sql = ""; foreach ($field as $t => $f) { if (is_numeric($t)) { - $this->sql .= (empty($this->sql) + $this->_sql .= (empty($this->_sql) ? "SELECT " : ",") . $f; } else if (is_array($f)) { - $this->sql .= (empty($this->sql) + $this->_sql .= (empty($this->_sql) ? "SELECT $t." : ",$t.") . implode(",$t.", $f); } else { - $this->sql .= (empty($this->sql) + $this->_sql .= (empty($this->_sql) ? "SELECT " : ",") . "$t.$f"; } } } else if (empty($field)) { - $this->sql = "SELECT *"; + $this->_sql = "SELECT *"; } else { - $this->sql = "SELECT `$field`"; + $this->_sql = "SELECT `$field`"; } // FROM table list if (is_array($table)) { - $this->sql .= " FROM `".implode("`, `", $table)."`"; + $this->_sql .= " FROM `".implode("`, `", $table)."`"; } else { - $this->sql .= " FROM `$table`"; + $this->_sql .= " FROM `$table`"; } // WHERE criteria list if (!empty($crit)) { - $this->sql .= " WHERE ".$this->_analyseCrit($crit); + $this->_sql .= " WHERE ".$this->_analyseCrit($crit); } // ORDER BY if (is_array($orderby)) { - $this->sql .= " ORDER BY `".implode("`, `", $orderby)."`"; + $this->_sql .= " ORDER BY `".implode("`, `", $orderby)."`"; } else if (!empty($orderby)) { - $this->sql .= " ORDER BY `$orderby`"; + $this->_sql .= " ORDER BY `$orderby`"; } if (is_numeric($limit) && $limit>0) { - $this->sql .= " LIMIT $limit"; + $this->_sql .= " LIMIT $limit"; if (is_numeric($start) && $start>0) { - $this->sql .= " OFFSET $start"; + $this->_sql .= " OFFSET $start"; } } } - //echo "SQL: ".$this->sql."\n"; - $this->res = $this->conn->prepare($this->sql); - if ($this->res===false) { + //echo "SQL: ".$this->_sql."\n"; + $this->_res = $this->_conn->prepare($this->_sql); + if ($this->_res===false) { $err = $this->db->errorInfo(); throw new Exception($err[2]); } - $this->pos = -1; + $this->_pos = -1; } /** @@ -145,8 +153,8 @@ class TableIterator implements Iterator */ function __destruct () { - if ($this->res) { - $this->res->closeCursor(); + if ($this->_res) { + $this->_res->closeCursor(); } } @@ -154,7 +162,7 @@ class TableIterator implements Iterator * Build WHERE clause * * @param string|array $crit To document - * @param string $bool logical operator between criteria + * @param string $bool logical operator between criteria * * @return To document */ @@ -214,14 +222,14 @@ class TableIterator implements Iterator public function rewind () { - if ($this->res && $this->pos>=0) { - $this->res->closeCursor(); - $this->pos = -1; + if ($this->_res && $this->_pos>=0) { + $this->_res->closeCursor(); + $this->_pos = -1; } - if ($this->res && $this->pos<0) { - if (!$this->res->execute()) { - $err = $this->res->errorInfo(); + if ($this->_res && $this->_pos<0) { + if (!$this->_res->execute()) { + $err = $this->_res->errorInfo(); throw new Exception($err[2]); } } @@ -235,7 +243,7 @@ class TableIterator implements Iterator */ public function current() { - return $this->row; + return $this->_row; } /** @@ -245,7 +253,7 @@ class TableIterator implements Iterator */ public function key() { - return (isset($this->row["id"]) ? $this->row["id"] : $this->pos); + return (isset($this->_row["id"]) ? $this->_row["id"] : $this->_pos); } /** @@ -255,12 +263,12 @@ class TableIterator implements Iterator */ public function next() { - if (!$this->res) { + if (!$this->_res) { return false; } - $this->row = $this->res->fetch(PDO::FETCH_ASSOC); - $this->pos++; - return $this->row; + $this->_row = $this->_res->fetch(PDO::FETCH_ASSOC); + $this->_pos++; + return $this->_row; } /** @@ -270,7 +278,7 @@ class TableIterator implements Iterator */ public function valid() { - return $this->res && $this->row; + return $this->_res && $this->_row; } /** @@ -280,7 +288,7 @@ class TableIterator implements Iterator */ public function numrows() { - return ($this->res ? $this->res->rowCount() : 0); + return ($this->_res ? $this->_res->rowCount() : 0); } } ?> \ No newline at end of file -- cgit