public function setDefinition(array $attributes) { if (!isset($attributes['fields']) && !isset($attributes['field'])) { throw new InvalidArgumentException('Schema index definition must specify one or more fields.'); } if (!isset($attributes['fields'])) { $attributes['fields'] = head((array) $attributes['field']); } if (!isset($attributes['index'])) { $attributes['index'] = true; } if ($attributes['index'] == 'foreign') { if (is_array($attributes['fields'])) { throw new InvalidArgumentException('A foreign key index ' . json_encode($attributes['fields']) . ' must not be an array of fields.'); } if (ends_with($attributes['fields'], '_id')) { $references = 'id'; $on = str_plural(substr($attributes['fields'], 0, -3)); } if (!isset($attributes['references']) && isset($references)) { $attributes['references'] = $references; } else { throw new InvalidArgumentException('A foreign key index ' . json_encode($attributes['fields']) . ' must have a \'references\' parameter.'); } if (!isset($attributes['on']) & isset($on)) { $attributes['on'] = str_plural($on); } else { throw new InvalidArgumentException('A foreign key index ' . json_encode($attributes['fields']) . ' must have an \'on\' parameter.'); } } else { unset($attributes['references']); unset($attributes['on']); unset($attributes['onDelete']); } $attributes['name'] = $this->createIndexName($attributes['table'], $attributes['index'], (array) $attributes['fields']); if (empty(@$attributes['indexName'])) { $attributes['name'] = $this->createIndexName($attributes['table'], $attributes['index'], (array) $attributes['fields']); } else { $attributes['name'] = $attributes['indexName']; } parent::setDefinition($attributes); }
public function setDefinition(array $attributes) { if (!isset($attributes['title'])) { if (isset($attributes['model'])) { $attributes['title'] = mb_convert_case(preg_replace('/(.)(?=[A-Z])/', '$1 ', $attributes['model']), MB_CASE_TITLE, 'UTF-8'); $attributes['plural'] = str_plural($attributes['title']); } elseif (isset($attributes['table'])) { $attributes['plural'] = mb_convert_case(str_replace('_', ' ', $attributes['table']), MB_CASE_TITLE, 'UTF-8'); $attributes['title'] = str_singular($attributes['plural']); } else { throw new InvalidArgumentException('Lang definition must have either a title, a table name or a model name.'); } } elseif (is_array($attributes['title'])) { if (!isset($attributes['title']['plural']) && isset($attributes['title']['singular'])) { $attributes['plural'] = str_plural($attributes['title']['singular']); $attributes['title'] = $attributes['title']['singular']; } elseif (isset($attributes['title']['plural']) && !isset($attributes['title']['singular'])) { $attributes['plural'] = $attributes['title']['plural']; $attributes['title'] = str_singular($attributes['title']['plural']); } elseif (isset($attributes['title']['plural']) && isset($attributes['title']['singular'])) { $attributes['plural'] = $attributes['title']['plural']; $attributes['title'] = $attributes['title']['singular']; } else { throw new InvalidArgumentException('Lang definition must have either a singular or plural title.'); } } else { $attributes['plural'] = str_plural($attributes['title']); } $labels = []; $helps = []; // foreach ($attributes['fields'] as $field) { // $labels[] = new LangLabelDefinition($field, $this->block); // $helps[] = new LangHelpDefinition($field, $this->block); // } $attributes['labels'] = collect($labels)->keyBy('name'); $attributes['helps'] = collect($labels)->keyBy('name'); parent::setDefinition($attributes); }
public function setDefinition(array $attributes) { if (is_string($attributes)) { if (in_array((string) $attributes, ['timestamps', 'timestampsTz', 'nullableTimestamps', 'rememberToken'])) { $attributes = ['predefined' => true, 'name' => $attributes, 'type' => $attributes]; } else { $attributes = ['name' => $attributes, 'type' => $attributes]; } } else { if (in_array((string) @$attributes['type'], ['timestamps', 'timestampsTz', 'nullableTimestamps', 'rememberToken'])) { $attributes = ['predefined' => true, 'name' => $attributes['type'], 'type' => $attributes['type']]; } } if (empty(@$attributes['name'])) { throw new InvalidArgumentException('Schema column definition must have a name.'); } if (empty(@$attributes['type'])) { $attributes['type'] == 'string'; } if (starts_with($attributes['type'], 'unsigned')) { $attributes['unsigned'] = true; $attributes['type'] = lcfirst(substr($attributes['type'], 8)); } if (isset($this->allowedParameters[$attributes['type']])) { $allowedParameters = $this->allowedParameters[$attributes['type']]; if (!isset($attributes['parameters']) && ($parameters = array_only($attributes, $allowedParameters))) { $attributes['parameters'] = $parameters; } } if (!isset($attributes['index']) && @$attributes['unique']) { $attributes['index'] = 'unique'; } parent::setDefinition($attributes); if ($this->predefined) { $this->pattern = ' $table->{:type:}();' . PHP_EOL; } }
public function setDefinition(array $attributes) { if (!isset($attributes['table']) && !isset($attributes['model'])) { throw new InvalidArgumentException('Schema definition must have either a table or model name.'); } $attributes = array_merge(array_except($attributes, ['migration']), $attributes['migration']); if (!isset($attributes['table'])) { $attributes['table'] = snake_case(str_plural($attributes['model'])); } if (!isset($attributes['create']) || $attributes['create']) { $attributes['name'] = 'create_' . $attributes['table'] . '_table'; $attributes['action'] = 'create'; } else { $attributes['name'] = 'modify_' . $attributes['table'] . '_table'; $attributes['action'] = 'table'; $this->patternFile = 'migrations/update'; } $attributes['class'] = studly_case($attributes['name']); foreach ($attributes['fields'] as &$field) { $field['table'] = $attributes['table']; if ($attributes['action'] == 'create') { unset($field['first']); unset($field['after']); } $field = new SchemaColumnDefinition($field, $this->block); } $attributes['fields'] = collect($attributes['fields'])->keyBy('name'); foreach ($attributes['indexes'] as &$field) { $field['table'] = $attributes['table']; $field = new SchemaIndexDefinition($field, $this->block); } $attributes['indexes'] = collect($attributes['indexes'])->keyBy('name'); parent::setDefinition($attributes); // foreach ($this->indexes as $index) { // CHECK INDEXES HAVE MATCHING COLUMNS // } }