示例#1
0
 /**
  * Fluent constructor.
  *
  * When directly constructing a model that extends Fluent, no argument should be passed as these parameters accept
  * NULL values and they are only intended to be filled by AbstractTable::findBy[COL*] method
  *
  * @param string|null $table
  * @param array|null $row
  * @throws FluentException
  */
 public final function __construct(string $table = null, array $row = null)
 {
     // Set modelName
     $this->modelName = get_called_class();
     // Check if table relation is defined
     if (!defined("static::SCHEMA_TABLE")) {
         throw FluentException::initConstant("SCHEMA_TABLE", $this->modelName);
     }
     $modelSchemaTable = constant("static::SCHEMA_TABLE");
     // Check if $table is provided for cross-checking
     if (!is_null($table)) {
         // Cross-check $table with model's SCHEMA_TABLE
         if ($table !== $modelSchemaTable) {
             // On fail, Cross-check if $table has SCHEMA_TABLE constant and that matches
             $tableConstant = sprintf("%s::SCHEMA_TABLE", $table);
             if (!defined($tableConstant) || constant($tableConstant) !== $modelSchemaTable) {
                 // Model and table are NOT related
                 throw FluentException::tableModelMismatch($this->modelName, $modelSchemaTable, $table);
             }
         }
     }
     // Save AbstractTable instance
     $this->schemaTable = Schema::table($modelSchemaTable);
     // Bootstrap data mapping
     $this->private = [];
     // Check if $row is Array
     if (is_array($row)) {
         // Verify that $row has all columns defined for AbstractTable
         $columnsKeys = $this->schemaTable->getColumnsKeys();
         foreach ($columnsKeys as $column) {
             if (!array_key_exists($column, $row)) {
                 throw FluentException::missingColumn($column, $this->modelName);
             }
         }
         // Get all columns
         $columns = $this->schemaTable->getColumns();
         // Data mapping
         foreach ($row as $key => $value) {
             // Get column
             switch ($columns[$key]->scalarType) {
                 case "integer":
                     $value = (int) $value;
                     break;
                 case "double":
                     $d = $columns[$key]->attributes["d"];
                     $value = round($value, $d + 1);
                     break;
                 default:
                     break;
             }
             // Sort this key as public or private?
             $camelKey = Comely::camelCase($key);
             if (property_exists($this->modelName, $camelKey)) {
                 // Public property
                 $this->{$camelKey} = $value;
             } else {
                 // Private variable
                 $this->private[$camelKey] = $value;
             }
         }
     }
 }
示例#2
0
 /**
  * @param Database $db
  * @return StorageInterface
  */
 public static function Database(Database $db) : StorageInterface
 {
     Schema::loadTable($db, "Comely\\IO\\Session\\Storage\\Database");
     return Schema::table("comely_sessions");
 }