typeHandler() public static méthode

Get type handler class by type
public static typeHandler ( string $type, $class = null ) : Spot_Adapter_Interface
$type string Field type (i.e. 'string' or 'int', etc.)
Résultat Spot_Adapter_Interface Spot adapter instance
Exemple #1
0
 /**
  * Get formatted fields with all neccesary array keys and values.
  * Merges defaults with defined field values to ensure all options exist for each field.
  *
  * @param string $entityName Name of the entity class
  * @return array Defined fields plus all defaults for full array of all possible options
  */
 public function fields($entityName)
 {
     if (!is_string($entityName)) {
         throw new \Spot\Exception(__METHOD__ . " only accepts a string. Given (" . gettype($entityName) . ")");
     }
     if (!is_subclass_of($entityName, '\\Spot\\Entity')) {
         throw new \Spot\Exception($entityName . " must be subclass of '\\Spot\\Entity'.");
     }
     if (isset(self::$_fields[$entityName])) {
         $returnFields = self::$_fields[$entityName];
     } else {
         // Datasource info
         $entityDatasource = null;
         $entityDatasource = $entityName::datasource();
         if (null === $entityDatasource || !is_string($entityDatasource)) {
             echo "\n\n" . $entityName . "::datasource() = " . var_export($entityName::datasource(), true) . "\n\n";
             throw new \InvalidArgumentException("Entity must have a datasource defined. Please define a protected property named '_datasource' on your '" . $entityName . "' entity class.");
         }
         self::$_datasource[$entityName] = $entityDatasource;
         // Datasource Options
         $entityDatasourceOptions = $entityName::datasourceOptions();
         self::$_datasourceOptions[$entityName] = $entityDatasourceOptions;
         // Connection info
         $entityConnection = $entityName::connection();
         // If no adapter specified, Spot will use default one from config object (or first one set if default is not explicitly set)
         self::$_connection[$entityName] = $entityConnection ? $entityConnection : false;
         // Default settings for all fields
         $fieldDefaults = array('type' => 'string', 'default' => null, 'length' => null, 'required' => false, 'null' => true, 'unsigned' => false, 'fulltext' => false, 'primary' => false, 'index' => false, 'unique' => false, 'serial' => false);
         // Type default overrides for specific field types
         $fieldTypeDefaults = array('string' => array('length' => 255), 'float' => array('length' => array(10, 2)), 'int' => array('length' => 10, 'unsigned' => true));
         // Get entity fields from entity class
         $entityFields = false;
         $entityFields = $entityName::fields();
         if (!is_array($entityFields) || count($entityFields) < 1) {
             throw new \InvalidArgumentException($entityName . " Must have at least one field defined.");
         }
         $returnFields = array();
         self::$_fieldDefaultValues[$entityName] = array();
         foreach ($entityFields as $fieldName => $fieldOpts) {
             // Store field definition exactly how it is defined before modifying it below
             if ($fieldOpts['type'] != 'relation') {
                 self::$_fieldsDefined[$entityName][$fieldName] = $fieldOpts;
             }
             // Get adapter options and type from typeHandler
             $typeHandler = Config::typeHandler($fieldOpts['type']);
             $typeOptions = $typeHandler::adapterOptions();
             // Use typeOptions as base, merge field options, but keep 'type' from typeOptions
             $fieldOpts = array_merge($typeOptions, $fieldOpts, array('type' => $typeOptions['type']));
             // Format field will full set of default options
             if (isset($fieldOpts['type']) && isset($fieldTypeDefaults[$fieldOpts['type']])) {
                 // Include type defaults
                 $fieldOpts = array_merge($fieldDefaults, $fieldTypeDefaults[$fieldOpts['type']], $fieldOpts);
             } else {
                 // Merge with defaults
                 $fieldOpts = array_merge($fieldDefaults, $fieldOpts);
             }
             // Store primary key
             if (true === $fieldOpts['primary']) {
                 self::$_primaryKeyField[$entityName] = $fieldName;
             }
             // Store default value
             if (null !== $fieldOpts['default']) {
                 self::$_fieldDefaultValues[$entityName][$fieldName] = $fieldOpts['default'];
             } else {
                 self::$_fieldDefaultValues[$entityName][$fieldName] = null;
             }
             $returnFields[$fieldName] = $fieldOpts;
         }
         self::$_fields[$entityName] = $returnFields;
         // Relations
         $entityRelations = array();
         $entityRelations = $entityName::relations();
         if (!is_array($entityRelations)) {
             throw new \InvalidArgumentException($entityName . " Relation definitons must be formatted as an array.");
         }
         foreach ($entityRelations as $relationAlias => $relationOpts) {
             self::$_relations[$entityName][$relationAlias] = $relationOpts;
         }
     }
     return $returnFields;
 }
Exemple #2
0
 /**
  * Syntax for each column in CREATE TABLE command
  *
  * @param string $fieldName Field name
  * @param array $fieldInfo Array of field settings
  * @return string SQL syntax
  */
 public function migrateSyntaxFieldCreate($fieldName, array $fieldInfo)
 {
     // Get adapter options and type from typeHandler
     $typeHandler = Config::typeHandler($fieldInfo['type']);
     $fieldInfo = array_merge($fieldInfo, $typeHandler::adapterOptions());
     $adapterType = $this->_fieldTypeMap[$fieldInfo['type']]['adapter_type'];
     $syntax = "`" . $fieldName . "` " . $adapterType;
     // Column type and length
     $syntax .= $fieldInfo['length'] ? '(' . $fieldInfo['length'] . ')' : '';
     // Unsigned
     $syntax .= $fieldInfo['unsigned'] ? ' unsigned' : '';
     // Collate
     $syntax .= $fieldInfo['type'] == 'string' || $fieldInfo['type'] == 'text' ? ' COLLATE ' . $this->_collate : '';
     // Nullable
     $isNullable = true;
     if ($fieldInfo['required'] || !$fieldInfo['null']) {
         $syntax .= ' NOT NULL';
         $isNullable = false;
     }
     // Default value
     if ($fieldInfo['default'] === null && $isNullable) {
         $syntax .= " DEFAULT NULL";
     } elseif ($fieldInfo['default'] !== null) {
         $default = $fieldInfo['default'];
         // If it's a boolean and $default is boolean then it should be 1 or 0
         if (is_bool($default) && $fieldInfo['type'] == "boolean") {
             $default = $default ? 1 : 0;
         }
         if (is_scalar($default)) {
             $syntax .= " DEFAULT '" . $default . "'";
         }
     }
     // Extra
     $syntax .= $fieldInfo['primary'] && $fieldInfo['serial'] ? ' AUTO_INCREMENT' : '';
     return $syntax;
 }
Exemple #3
0
 /**
  * Retrieve data from the data store
  */
 public function loadEntity($entityName, $data)
 {
     $loadedData = array();
     $fields = $entityName::fields();
     $entityData = array_intersect_key($data, $fields);
     foreach ($data as $field => $value) {
         // Field is in the Entity definitions
         if (isset($entityData[$field])) {
             $typeHandler = \Spot\Config::typeHandler($fields[$field]['type']);
             $loadedData[$field] = $typeHandler::_load($value);
             // Extra data returned with query (like calculated valeus, etc.)
         } else {
             $loadedData[$field] = $value;
         }
     }
     return $loadedData;
 }