public $defaultValue;
public $autoIncrement;
+ private $validator;
private $formatter;
public function __construct(array $column)
$this->formatter = $class;
}
+ public function registerValidator($class)
+ {
+ $this->validator = $class;
+ }
+
public function format($value, $badNullFallbackToDefault = false)
{
if (is_null($value)) {
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') {
}
}
-interface PlDBTableFieldFormatter extends XDBFormat
+interface PlDBTableFieldValidator
{
public function __construct(PlDBTableField $field, $value);
}
+interface PlDBTableFieldFormatter extends PlDBTableFieldValidator, XDBFormat, PlExportable
+{
+}
+
class DateFieldFormatter implements PlDBTableFieldFormatter
{
private $datetime;
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
return XDB::escape(json_encode($this->data));
}
+ public function export()
+ {
+ return $this->data;
+ }
+
public function offsetExists($offset)
{
return isset($this->data[$offset]);
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;
$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;
$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
}
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: