Take into account PlExportable interface.
[platal.git] / classes / plfilter.php
index af8141d..5dd77e6 100644 (file)
@@ -53,7 +53,7 @@ class PlLimit
  *     descending order).
  * The getSortTokens function is used to get actual ordering part of the query.
  */
-abstract class PlFilterOrder
+abstract class PlFilterOrder implements PlExportable
 {
     protected $desc = false;
     public function __construct($desc = false)
@@ -62,6 +62,20 @@ abstract class PlFilterOrder
         $this->_tokens = null;
     }
 
+    protected function buildExport($type)
+    {
+        $export = array('type' => $type);
+        if ($this->desc) {
+            $export['order'] = 'desc';
+        }
+        return $export;
+    }
+
+    public function export()
+    {
+        throw new Exception("This instance is not exportable");
+    }
+
     public function toggleDesc()
     {
         $this->desc = !$this->desc;
@@ -72,7 +86,7 @@ abstract class PlFilterOrder
         $this->desc = $desc;
     }
 
-    public function buildSort(PlFilter &$pf)
+    public function buildSort(PlFilter $pf)
     {
         $sel = $this->getSortTokens($pf);
         $this->_tokens = $sel;
@@ -88,10 +102,10 @@ abstract class PlFilterOrder
     }
 
     /** This function must return the tokens to use for ordering
-     * @param &$pf The PlFilter whose results must be ordered
+     * @param $pf The PlFilter whose results must be ordered
      * @return The name of the field to use for ordering results
      */
-    abstract protected function getSortTokens(PlFilter &$pf);
+    abstract protected function getSortTokens(PlFilter $pf);
 }
 // }}}
 
@@ -105,7 +119,7 @@ abstract class PlFilterGroupableOrder extends PlFilterOrder
      * the returned token will be used to group the values.
      * It will always be called AFTER getSortTokens().
      */
-    public function getGroupToken(PlFilter &$pf)
+    public function getGroupToken(PlFilter $pf)
     {
         return $this->_tokens;
     }
@@ -123,7 +137,7 @@ class PFO_Random extends PlFilterOrder
         $this->seed = $seed;
     }
 
-    protected function getSortTokens(PlFilter &$pf)
+    protected function getSortTokens(PlFilter $pf)
     {
         if ($this->seed == null) {
             return 'RAND()';
@@ -131,16 +145,24 @@ class PFO_Random extends PlFilterOrder
             return XDB::format('RAND({?})', $this->seed);
         }
     }
+
+    public function export()
+    {
+        $export = array('type' => 'random',);
+        if ($this->seed !== null)
+            $export['seed'] = $this->seed;
+        return $export;
+    }
 }
 // }}}
 
 // {{{ interface PlFilterCondition
-interface PlFilterCondition
+interface PlFilterCondition extends PlExportable
 {
     const COND_TRUE  = 'TRUE';
     const COND_FALSE = 'FALSE';
 
-    public function buildCondition(PlFilter &$pf);
+    public function buildCondition(PlFilter $pf);
 }
 // }}}
 
@@ -149,16 +171,21 @@ abstract class PFC_OneChild implements PlFilterCondition
 {
     protected $child;
 
-    public function __construct(&$child = null)
+    public function __construct($child = null)
     {
         if (!is_null($child) && ($child instanceof PlFilterCondition)) {
             $this->setChild($child);
         }
     }
 
-    public function setChild(PlFilterCondition &$cond)
+    public function setChild(PlFilterCondition $cond)
+    {
+        $this->child = $cond;
+    }
+
+    public function export()
     {
-        $this->child =& $cond;
+        return array('child' => $child->export());
     }
 }
 // }}}
@@ -175,16 +202,16 @@ abstract class PFC_NChildren implements PlFilterCondition
 
     public function addChildren(array $conds)
     {
-        foreach ($conds as &$cond) {
+        foreach ($conds as $cond) {
             if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
                 $this->addChild($cond);
             }
         }
     }
 
-    public function addChild(PlFilterCondition &$cond)
+    public function addChild(PlFilterCondition $cond)
     {
-        $this->children[] =& $cond;
+        $this->children[] = $cond;
     }
 
     protected function catConds(array $cond, $op, $fallback)
@@ -197,33 +224,52 @@ abstract class PFC_NChildren implements PlFilterCondition
             return '(' . implode(') ' . $op . ' (', $cond) . ')';
         }
     }
+
+    public function export()
+    {
+        $export = array();
+        foreach ($this->children as $child) {
+            $export[] = $child->export();
+        }
+        return array('children' => $export);
+    }
 }
 // }}}
 
 // {{{ class PFC_True
 class PFC_True implements PlFilterCondition
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         return self::COND_TRUE;
     }
+
+    public function export()
+    {
+        return array('type' => 'true');
+    }
 }
 // }}}
 
 // {{{ class PFC_False
 class PFC_False implements PlFilterCondition
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         return self::COND_FALSE;
     }
+
+    public function export()
+    {
+        return array('type' => 'false');
+    }
 }
 // }}}
 
 // {{{ class PFC_Not
 class PFC_Not extends PFC_OneChild
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         $val = $this->child->buildCondition($uf);
         if ($val == self::COND_TRUE) {
@@ -234,20 +280,27 @@ class PFC_Not extends PFC_OneChild
             return 'NOT (' . $val . ')';
         }
     }
+
+    public function export()
+    {
+        $export = parent::export();
+        $export['type'] = 'not';
+        return $export;
+    }
 }
 // }}}
 
 // {{{ class PFC_And
 class PFC_And extends PFC_NChildren
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         if (empty($this->children)) {
             return self::COND_FALSE;
         } else {
             $true = self::COND_FALSE;
             $conds = array();
-            foreach ($this->children as &$child) {
+            foreach ($this->children as $child) {
                 $val = $child->buildCondition($uf);
                 if ($val == self::COND_TRUE) {
                     $true = self::COND_TRUE;
@@ -260,20 +313,26 @@ class PFC_And extends PFC_NChildren
             return $this->catConds($conds, 'AND', $true);
         }
     }
+
+    public function export() {
+        $export = parent::export();
+        $export['type'] = 'and';
+        return $export;
+    }
 }
 // }}}
 
 // {{{ class PFC_Or
 class PFC_Or extends PFC_NChildren
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         if (empty($this->children)) {
             return self::COND_TRUE;
         } else {
             $true = self::COND_TRUE;
             $conds = array();
-            foreach ($this->children as &$child) {
+            foreach ($this->children as $child) {
                 $val = $child->buildCondition($uf);
                 if ($val == self::COND_TRUE) {
                     return self::COND_TRUE;
@@ -286,11 +345,17 @@ class PFC_Or extends PFC_NChildren
             return $this->catConds($conds, 'OR', $true);
         }
     }
+
+    public function export() {
+        $export = parent::export();
+        $export['type'] = 'or';
+        return $export;
+    }
 }
 // }}}
 
 // {{{ class PlFilter
-abstract class PlFilter
+abstract class PlFilter implements PlExportable
 {
     /** Filters objects matching the PlFilter
      * @param $objects The objects to filter
@@ -298,9 +363,9 @@ abstract class PlFilter
      */
     public abstract function filter(array $objects, $limit = null);
 
-    public abstract function setCondition(PlFilterCondition &$cond);
+    public abstract function setCondition(PlFilterCondition $cond);
 
-    public abstract function addSort(PlFilterOrder &$sort);
+    public abstract function addSort(PlFilterOrder $sort);
 
     public abstract function getTotalCount();