/**
* 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;
}