Spot\Config::typeHandler PHP Method

typeHandler() public static method

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.)
return Spot_Adapter_Interface Spot adapter instance
    public static function typeHandler($type, $class = null)
    {
        if (null === $class) {
            if (!isset(self::$_typeHandlers[$type])) {
                throw new \InvalidArgumentException("Type '{$type}' not registered. Register the type class handler with \\Spot\\Config::typeHanlder('{$type}', '\\Namespaced\\Path\\Class').");
            }
            return self::$_typeHandlers[$type];
        }
        if (!class_exists($class)) {
            throw new \InvalidArgumentException("Second parameter must be valid className with full namespace. Check the className and ensure the class is loaded before registering it as a type handler.");
        }
        return self::$_typeHandlers[$type] = $class;
    }

Usage Example

Ejemplo n.º 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;
 }
All Usage Examples Of Spot\Config::typeHandler