PlFlagset and PlDBTableEntry are exportable.
[platal.git] / classes / pldbtableentry.php
index 363da58..466a4ff 100644 (file)
@@ -74,6 +74,7 @@ class PlDBTableField
     public $defaultValue;
     public $autoIncrement;
 
+    private $validator;
     private $formatter;
 
     public function __construct(array $column)
@@ -104,6 +105,11 @@ class PlDBTableField
         $this->formatter = $class;
     }
 
+    public function registerValidator($class)
+    {
+        $this->validator = $class;
+    }
+
     public function format($value, $badNullFallbackToDefault = false)
     {
         if (is_null($value)) {
@@ -114,7 +120,12 @@ class PlDBTableField
                 return $this->defaultValue;
             }
             throw new PlDBBadValueException($value, $this, 'null not allowed');
-        } else if (!is_null($this->formatter)) {
+        }
+        if (!is_null($this->validator)) {
+            $class = $this->validator;
+            new $class($this, $value);
+        }
+        if (!is_null($this->formatter)) {
             $class = $this->formatter;
             $value = new $class($this, $value);
         } else if ($this->type == 'enum') {
@@ -154,11 +165,15 @@ class PlDBTableField
     }
 }
 
-interface PlDBTableFieldFormatter extends XDBFormat
+interface PlDBTableFieldValidator
 {
     public function __construct(PlDBTableField $field, $value);
 }
 
+interface PlDBTableFieldFormatter extends PlDBTableFieldValidator, XDBFormat, PlExportable
+{
+}
+
 class DateFieldFormatter implements PlDBTableFieldFormatter
 {
     private $datetime;
@@ -166,8 +181,7 @@ class DateFieldFormatter implements PlDBTableFieldFormatter
 
     public function __construct(PlDBTableField $field, $date)
     {
-        $date = $value;
-        $this->datetime = make_datetime($value);
+        $this->datetime = make_datetime($date);
         if (is_null($this->datetime)) {
             throw new PlDBBadValueException($date, $field, 'value is expected to be a date/time, ' . $date . ' given');
         }
@@ -182,13 +196,18 @@ class DateFieldFormatter implements PlDBTableFieldFormatter
 
     public function format()
     {
-        return XDB::escape($this->datetime->format($this->storageFormat));
+        return XDB::escape($this->export());
     }
 
     public function date($format)
     {
         return $this->datetime->format($format);
     }
+
+    public function export()
+    {
+        return $this->datetime->format($this->storageFormat);
+    }
 }
 
 class JSonFieldFormatter implements PlDBTableFieldFormatter, ArrayAccess
@@ -219,6 +238,11 @@ class JSonFieldFormatter implements PlDBTableFieldFormatter, ArrayAccess
         return XDB::escape(json_encode($this->data));
     }
 
+    public function export()
+    {
+        return $this->data;
+    }
+
     public function offsetExists($offset)
     {
         return isset($this->data[$offset]);
@@ -326,6 +350,12 @@ class PlDBTable
         return $this->field($field)->registerFormatter($class);
     }
 
+    public function registerFieldValidator($field, $class)
+    {
+        return $this->field($field)->registerValidator($class);
+    }
+
+
     public function defaultValue($field)
     {
         return $this->field($field)->defaultValue;
@@ -515,13 +545,26 @@ class PlDBTable
                                                                   $allowIncomplete));
     }
 
+    public function exportEntry(PlDBTableEntry $entry)
+    {
+        $export = array();
+        foreach ($this->schema as $key=>$field) {
+            $value = $entry->$key;
+            if ($value instanceof PlExportable) {
+                $value = $value->export();
+            }
+            $export[$key] = $value;
+        }
+        return $export;
+    }
+
     public static function get($name)
     {
         return new PlDBTable($name);
     }
 }
 
-class PlDBTableEntry extends PlAbstractIterable
+class PlDBTableEntry extends PlAbstractIterable implements PlExportable
 {
     private $table;
     private $changed;
@@ -553,6 +596,16 @@ class PlDBTableEntry extends PlAbstractIterable
         $this->table->registerFieldFormatter($field, $formatterClass);
     }
 
+    /** Register a custom validator for a field.
+     *
+     * A validator perform a pre-filter on the value of a field. As opposed to the formatters, it does
+     * not affects how the value is stored in the database.
+     */
+    protected function registerFieldValidator($field, $validatorClass)
+    {
+        $this->table->registerFieldValidator($field, $validatorClass);
+    }
+
     /** This hook is called when the entry is going to be updated in the db.
      *
      * A typical usecase is a class that stores low-level representation of
@@ -650,7 +703,7 @@ class PlDBTableEntry extends PlAbstractIterable
     public function copy(PlDBTableEntry $other)
     {
         Platal::assert($this->table == $other->table,
-                       "Trying to fill an entry of table {$this->table} with content of {$other->table}.");
+                       "Trying to fill an entry of table {$this->table->table} with content of {$other->table->table}.");
         $this->changed = $other->changed;
         $this->fetched = $other->fetched;
         $this->data    = $other->data;
@@ -714,6 +767,11 @@ class PlDBTableEntry extends PlAbstractIterable
         }
         return $this->table->deleteEntry($this, true);
     }
+
+    public function export()
+    {
+        return $this->table->exportEntry($this);
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: