Release plat/al core v1.1.13
[platal.git] / classes / plflagset.php
index 1971751..baa7bf9 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /*
- * Copyright (C) 2003-2008 Polytechnique.org
+ * Copyright (C) 2003-2011 Polytechnique.org
  * http://opensource.polytechnique.org/
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,7 +21,7 @@
 
 /** class for describing flags
  */
-class PlFlagSet
+class PlFlagSet extends PlAbstractIterable implements XDBFormat, PlExportable
 {
     /** string that holds the PlFlagSet */
     private $values = array();
@@ -39,18 +39,21 @@ class PlFlagSet
         $this->sep = $sep;
         $splitted = explode($sep, $flags);
         foreach ($splitted as $part) {
-            $this->values[$part] = true;
+            if (!empty($part)) {
+                $this->values[$part] = true;
+            }
         }
     }
 
 
     /** add flag
-     * @param $flag XXX
+     * @param $flag name of the flag
+     * @param $cond if true, add the flag (default is true), else, ignore.
      * @return VOID
      */
-    public function addFlag($flag)
+    public function addFlag($flag, $cond = true)
     {
-        if (empty($flag)) {
+        if (empty($flag) || !$cond) {
             return;
         }
         $this->values[$flag] = true;
@@ -101,6 +104,13 @@ class PlFlagSet
         }
     }
 
+    /** Remove all the flags.
+     */
+    public function clear()
+    {
+        $this->values = array();
+    }
+
 
     /** return the PlFlagSet
      */
@@ -117,7 +127,35 @@ class PlFlagSet
         }
         return $flags;
     }
+
+    /** export the PlFlagSet
+     */
+    public function export()
+    {
+        $array = array();
+        foreach ($this->values as $key=>$value) {
+            if ($value) {
+                $array[] = $key;
+            }
+        }
+        return $array;
+    }
+
+    /** format for XDB
+     */
+    public function format()
+    {
+        return XDB::escape($this->flags());
+    }
+
+
+    /** Build an iterator for this PlFlagSet.
+     */
+    public function iterate()
+    {
+        return PlIteratorUtils::fromArray($this->export(), 1, true);
+    }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>