Skip to content

Instantly share code, notes, and snippets.

@steveheinsch
Last active June 7, 2017 17:13
Show Gist options
  • Save steveheinsch/6d5e0c2cc5f1bbcac1fefe8c95b9ea74 to your computer and use it in GitHub Desktop.
Save steveheinsch/6d5e0c2cc5f1bbcac1fefe8c95b9ea74 to your computer and use it in GitHub Desktop.
/**
* Helper to translate "RETS" field definitions to mysql datatypes and eloquent methods
* @param $retsFieldDefinition
*
* @return object
*/
public function translateRetsField($retsFieldDefinition)
{
$retsInterpretation = $retsFieldDefinition->Interpretation;
$retsDataType = $retsFieldDefinition->DataType;
$MaximumLength = $retsFieldDefinition->MaximumLength;
$Precision = (! empty($retsFieldDefinition->Precision)) ? $retsFieldDefinition->Precision : 0;
// Lookup fk tables are treated differently, catch first
if ($retsInterpretation == 'Lookup') {
$DataType = 'varchar';
$eloquentMethod = 'string';
$MaximumLength = 255;
} elseif ($retsDataType == "Int" || $retsDataType == "Small" || $retsDataType == "Tiny") {
$DataType = 'int';
$eloquentMethod = 'integer';
} elseif ($retsDataType == "Long") {
$DataType = 'bigint';
$eloquentMethod = 'bigInteger';
} elseif ($retsDataType == "DateTime") {
$DataType = 'datetime';
$eloquentMethod = 'dateTime';
} elseif ($retsDataType == "Character" && $MaximumLength <= 1024) {
$DataType = 'varchar';
$eloquentMethod = 'string';
} elseif ($retsDataType == "Character" && $MaximumLength > 1024) {
$DataType = 'text';
$eloquentMethod = 'text';
} elseif ($retsDataType == "Decimal") {
$DataType = 'decimal';
$eloquentMethod = 'decimal';
} elseif ($retsDataType == "Boolean") {
$DataType = 'char';
$eloquentMethod = 'char';
$MaximumLength = 1;
} elseif ($retsDataType == "Date") {
$DataType = 'date';
$eloquentMethod = 'date';
} elseif ($retsDataType == "Time") {
$DataType = 'time';
$eloquentMethod = 'time';
} else {
// Unknown, set a default?
$DataType = 'varchar';
$eloquentMethod = 'string';
$MaximumLength = 255;
}
return (object) compact(['DataType', 'eloquentMethod', 'MaximumLength', 'Precision']);
}
/**
* Create or update a Field, and its lookups
*
* @param $field
* @param Blueprint $table
* @param bool $alter Whether we are altering an existing field
*
* @return Blueprint
*/
public function createField($field, Blueprint $table, $alter = false)
{
$fieldDefinition = $this->translateRetsField($field);
$dataType = $fieldDefinition->DataType;
$method = $fieldDefinition->eloquentMethod;
$maximumLength = $fieldDefinition->MaximumLength;
$precision = $fieldDefinition->Precision;
// If it's a lookup, create the lookup index
if ($field->Interpretation === 'Lookup') {
if ($alter) {
$table->string($field->name, 255)->nullable()->index()->change();
} else {
$table->string($field->name, 255)->nullable()->index();
}
// Decimal types have a precision and scale
} elseif ($dataType === 'decimal') {
if ($alter) {
$table->$method($field->name, $maximumLength, $precision)->nullable()->change();
} else {
$table->$method($field->name, $maximumLength, $precision)->nullable();
}
// String Types have a length
} elseif ($dataType === 'varchar' OR $dataType === 'char') {
if ($alter) {
$table->$method($field->name, $maximumLength)->nullable()->change();
} else {
$table->$method($field->name, $maximumLength)->nullable();
}
// Fields that don't require any special parameters
} else {
if ($alter) {
$table->$method($field->name)->nullable()->change();
} else {
$table->$method($field->name)->nullable();
}
}
// Add the primary index
if ($field->name == 'id') {
$table->primary('id');
}
return $table;
}
//Usage example (creates the main database table along with fields):
Schema::connection($this->connection)->create($this->mainTableName, function (Blueprint $table) {
$table->engine = 'InnoDB';
foreach ($this->getMainFields()->merge($this->getLookupFields()) as $field) { //these are the exact rets field definitions coming from rets/phrets
$this->createField($field, $table);
if ($field->Interpretation === 'Lookup') {
$this->createLookupTable($field);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment