Added interface PlExportable and implemented it on PlFilter, PlFilterCondition and...
authorRiton <riton@melix.net>
Fri, 5 Nov 2010 20:39:48 +0000 (21:39 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 5 Nov 2010 20:42:41 +0000 (21:42 +0100)
Classes which implement PlExportable must expose an export() method.
It should return an associative array representing the instance.

Signed-off-by: Riton <riton@melix.net>
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/plexportable.php [new file with mode: 0644]
classes/plfilter.php

diff --git a/classes/plexportable.php b/classes/plexportable.php
new file mode 100644 (file)
index 0000000..0f1545d
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+// {{{ interface PlExportable
+/** PlExportable intends to enable a robust exportation of objects.
+ *  By explicitly implementing the exportation process, PlExportable classes are
+ *  able to be rebuilt from their exportation, even if the class has been modified
+ *  inbetween, a case that classic php serialization badly handle.
+ */
+interface PlExportable
+{
+    /** Returns an associative array containing the neccessary
+     *  datas to rebuild the instance. The result can then be serialized,
+     *  for example thank the json_encode() function.
+     */
+    public function export();
+}
+// }}}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index af8141d..1a6d525 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)
@@ -131,11 +131,19 @@ 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';
@@ -160,6 +168,11 @@ abstract class PFC_OneChild implements PlFilterCondition
     {
         $this->child =& $cond;
     }
+
+    public function export()
+    {
+        return array('child' => $child->export());
+    }
 }
 // }}}
 
@@ -197,6 +210,14 @@ 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);
+    }
 }
 // }}}
 
@@ -207,6 +228,11 @@ class PFC_True implements PlFilterCondition
     {
         return self::COND_TRUE;
     }
+
+    public function export()
+    {
+        return array('type' => 'true');
+    }
 }
 // }}}
 
@@ -217,6 +243,11 @@ class PFC_False implements PlFilterCondition
     {
         return self::COND_FALSE;
     }
+
+    public function export()
+    {
+        return array('type' => 'false');
+    }
 }
 // }}}
 
@@ -234,6 +265,13 @@ class PFC_Not extends PFC_OneChild
             return 'NOT (' . $val . ')';
         }
     }
+
+    public function export()
+    {
+        $export = parent::export();
+        $export['type'] = 'not';
+        return $export;
+    }
 }
 // }}}
 
@@ -260,6 +298,12 @@ class PFC_And extends PFC_NChildren
             return $this->catConds($conds, 'AND', $true);
         }
     }
+
+    public function export() {
+        $export = parent::export();
+        $export['type'] = 'and';
+        return $export;
+    }
 }
 // }}}
 
@@ -286,11 +330,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