More unit tests.
[platal.git] / classes / plfilter.php
index 15bcaf5..05c1edb 100644 (file)
@@ -19,6 +19,7 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+// {{{ class PlLimit
 class PlLimit
 {
     private $count = null;
@@ -42,7 +43,9 @@ class PlLimit
         return '';
     }
 }
+// }}}
 
+// {{{ class PlSqlJoin
 class PlSqlJoin
 {
     private $mode;
@@ -53,8 +56,10 @@ class PlSqlJoin
     const MODE_RIGHT = 'RIGHT';
     const MODE_INNER = 'INNER';
 
-    public function __construct($mode, $table, $condition)
+    private function __construct($mode, $params)
     {
+        $table = array_shift($params);
+        $condition = call_user_func_array(array('XDB', 'format'), $params);
         if ($mode != self::MODE_LEFT && $mode != self::MODE_RIGHT && $mode != self::MODE_INNER) {
             Platal::page()->kill("Join mode error: unknown mode $mode");
             return;
@@ -111,8 +116,40 @@ class PlSqlJoin
         }
         return $str;
     }
+
+    /** Build a left join
+     * @param table The name of the table.
+     * @param condition The condition of the jointure
+     */
+    public static function left()
+    {
+        $params = func_get_args();
+        return new PlSqlJoin(self::MODE_LEFT, $params);
+    }
+
+    /** Build a right join
+     * @param table The name of the table.
+     * @param condition The condition of the jointure
+     */
+    public static function right()
+    {
+        $params = func_get_args();
+        return new PlSqlJoin(self::MODE_RIGHT, $params);
+    }
+
+    /** Build a inner join
+     * @param table The name of the table.
+     * @param condition The condition of the jointure
+     */
+    public static function inner()
+    {
+        $params = func_get_args();
+        return new PlSqlJoin(self::MODE_INNER, $params);
+    }
 }
+// }}}
 
+// {{{ class PlFilterOrder
 abstract class PlFilterOrder
 {
     protected $desc = false;
@@ -145,8 +182,31 @@ abstract class PlFilterOrder
         return $sel;
     }
 
-    abstract protected function getSortTokens(&$pf);
+    abstract protected function getSortTokens(PlFilter &$pf);
 }
+// }}}
+
+// {{{ class PFO_Random
+class PFO_Random extends PlFilterOrder
+{
+    private $seed = null;
+
+    public function __construct($seed = null, $desc = false)
+    {
+        parent::__construct($desc);
+        $this->seed = $seed;
+    }
+
+    protected function getSortTokens(PlFilter &$pf)
+    {
+        if ($this->seed == null) {
+            return 'RAND()';
+        } else {
+            return XDB::format('RAND({?})', $this->seed);
+        }
+    }
+}
+// }}}
 
 // {{{ interface PlFilterCondition
 interface PlFilterCondition
@@ -299,14 +359,14 @@ class PFC_Or extends PFC_NChildren
 }
 // }}}
 
-
+// {{{ class PlFilter
 abstract class PlFilter
 {
     /** Filters objects matching the PlFilter
      * @param $objects The objects to filter
      * @param $limit The portion of the matching objects to show
      */
-    public abstract function filter(array $objects, PlLimit &$limit);
+    public abstract function filter(array $objects, $limit = null);
 
     public abstract function setCondition(PlFilterCondition &$cond);
 
@@ -317,7 +377,7 @@ abstract class PlFilter
     /** Get objects, selecting only those within a limit
      * @param $limit The portion of the matching objects to select
      */
-    public abstract function get(PlLimit &$limit);
+    public abstract function get($limit = null);
 
     /** PRIVATE FUNCTIONS
      */
@@ -345,5 +405,7 @@ abstract class PlFilter
     }
 
 }
+// }}}
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>