cyrilleinvalides/choupas/www/admin/app/libs/sense/Entity/Element.php

64 lines
1.3 KiB
PHP
Executable File

<?php
abstract class Element {
private static $systemType;
final public static function getSystemType ( $root = false ) {
if ( static::$systemType )
return static::$systemType;
$class = get_called_class();
return static::$systemType = lcfirst(preg_replace('!$Element!', '',
( $parent = get_parent_class($class) ) && ( __CLASS__ !== $parent ) ?
$parent::getSystemType() :
$class));
}
public static function getDefinition () {
throw new Exception('Children class must reimplement Element::getSchema !');
}
public static function getSchema ( $params ) {
throw new Exception('Children class must reimplement Element::getParams !');
}
public static function invalid ( $schema, $value, $data = array() ) {
if ( is_null($value) && !empty($schema['null']) )
return false;
if ( is_null($value) )
return 'Value should not be null !';
return false;
}
public static function apply ( $schema, $value, $data = array() ) {
if ( $error = static::invalid($schema, $value, $data) ) {
$value = array_key_exists('default', $schema) ? $schema['default'] : null;
if ( is_null($value) && empty($schema['null']) )
throw new Exception('No default to set for missing value for type ' . $schema['type'] . "\n" . $error);
return $value;
}
return $value;
}
}
?>