/** * If Schema fields are specified, parse * them into an array of objects. * * So: name:string, age:integer * Becomes: [ ((object)['name' => 'string'], (object)['age' => 'integer'] ] * * @returns mixed */ protected function convertFieldsToArray() { $fields = $this->fields; if (!$fields) { return; } $fields = GeneratorsServiceProvider::splitFields($fields, true); $indices = [0]; // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list $keyindices = $indices; // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list $primaryindices = $indices; // first element is last used index number, keys are _i where i is passed from the parameters, or auto generated, _i => _n_f where n is from params and f index of the field in the fields list $dropIndices = []; $foreignKeys = []; $relationsModelList = GeneratorsServiceProvider::getRelationsModelVarsList($fields); $fieldIndex = 0; foreach ($fields as $field) { $fieldIndex++; // If there is a third key, then // the user is setting any number // of options $options = $field->options; $field->options = ''; $hadUnsigned = false; $hadNullable = false; $hadDefault = false; $hadOnDelete = false; $foreignTable = null; foreach ($options as $option) { $isKey = null; $isUnique = null; if (($isPrimary = strpos($option, 'primary') === 0) || ($isUnique = strpos($option, 'unique') === 0) || ($isKey = strpos($option, 'keyindex') === 0) || strpos($option, 'index') === 0) { if ($isPrimary) { $keyIndex =& $primaryindices; } elseif ($isKey || $isUnique) { $keyIndex =& $keyindices; } else { $keyIndex =& $indices; } self::processIndexOption($keyIndex, $option, $field->name, $fieldIndex); } if ($option === 'ondelete' || $option === 'ondelete') { $hadOnDelete = true; } if (GeneratorsServiceProvider::isFieldHintOption($option)) { continue; } if ($option === 'unsigned' || $option === 'unsigned()') { $hadUnsigned = true; } if ($option === 'nullable' || $option === 'nullable()') { $hadNullable = true; } if ($option === 'default' || starts_with($option, 'default(')) { if ($option === 'default') { $option = 'default(null)'; } $hadDefault = true; } $field->options .= str_contains($option, '(') ? "->{$option}" : "->{$option}()"; } // add foreign keys $name = $field->name; if (array_key_exists($name, $relationsModelList)) { $table_name = $relationsModelList[$name]['snake_models']; if (!$hadUnsigned) { $field->options .= "->unsigned()"; } $indexName = "ixf_{$this->tableName}_{$name}_{$table_name}_id"; if (strlen($indexName) > 64) { $indexName = substr($indexName, 0, 64); } $onDeleteCascade = $hadOnDelete ? "->onDelete('cascade')" : ''; $foreignKeys[] = "\$table->foreign('{$name}','{$indexName}')->references('id')->on({{prefix}}'{$table_name}'){$onDeleteCascade}"; $dropIndices[] = "\$table->dropIndex('{$indexName}')"; } if ($hadNullable && !$hadDefault && !$field->type === 'text') { $field->options .= "->default(null)"; } } // now append the indices $inds = $foreignKeys; $inds = array_merge($inds, $this->generateIndex('index', $indices, $dropIndices)); $inds = array_merge($inds, $this->generateIndex('unique', $keyindices, $dropIndices)); $inds = array_merge($inds, $this->generateIndex('primary', $primaryindices, $dropIndices)); $fields[] = [$inds, $dropIndices]; return $fields; }