, columns: list, required: list, key: list}> */ public const TARGETS = [ 'posts' => [ 'model' => Post::class, 'columns' => ['title', 'slug', 'excerpt', 'body', 'cover_image', 'category', 'tags', 'is_featured', 'published_at'], 'required' => ['title', 'body'], 'key' => ['slug'], ], 'pages' => [ 'model' => Page::class, 'columns' => ['slug', 'title', 'body', 'is_published'], 'required' => ['slug', 'title'], 'key' => ['slug'], ], 'reviews' => [ 'model' => Review::class, 'columns' => ['author', 'place', 'rating', 'body', 'is_visible'], 'required' => ['author', 'body'], 'key' => ['author', 'body'], ], ]; /** * @param array $map package column => column of the source table; same name when left out * @return array{created: int, updated: int, skipped: int, problems: list} */ public function run(string $target, ConnectionInterface $source, string $table, array $map = [], bool $dry = false, ?int $limit = null): array { $spec = self::TARGETS[$target] ?? throw new InvalidArgumentException("Unknown target {$target}; use one of: ".implode(', ', array_keys(self::TARGETS))); foreach (array_keys($map) as $column) { if (! in_array($column, $spec['columns'], true)) { throw new InvalidArgumentException("{$target} has no column {$column}; known: ".implode(', ', $spec['columns'])); } } $result = ['created' => 0, 'updated' => 0, 'skipped' => 0, 'problems' => []]; $query = $source->table($table); $limit !== null && $query->limit($limit); foreach ($query->get() as $number => $row) { $row = (array) $row; $values = []; foreach ($spec['columns'] as $column) { $from = $map[$column] ?? $column; if (array_key_exists($from, $row)) { $values[$column] = $this->convert($column, $row[$from]); } } if ($target === 'posts' || $target === 'pages') { $values['slug'] = ($values['slug'] ?? '') !== '' ? Str::slug((string) $values['slug']) : Str::slug((string) ($values['title'] ?? '')); } foreach ($spec['required'] as $column) { if (! isset($values[$column]) || $values[$column] === '') { $result['skipped']++; $result['problems'][] = 'row '.($number + 1).": no value for {$column}"; continue 2; } } $key = Arr::only($values, $spec['key']); $model = $spec['model']; $existing = $model::query()->where($key)->first(); $existing ? $result['updated']++ : $result['created']++; if (! $dry) { $existing ? $existing->update($values) : $model::query()->create($values); } } return $result; } private function convert(string $column, mixed $value): mixed { return match ($column) { 'tags' => $this->tags($value), 'is_featured', 'is_published', 'is_visible' => filter_var($value, FILTER_VALIDATE_BOOLEAN), 'rating' => $value === null || $value === '' ? null : max(1, min(5, (int) $value)), 'published_at' => $value === null || $value === '' ? null : $value, default => is_string($value) ? trim($value) : $value, }; } /** @return list */ private function tags(mixed $value): array { if (is_string($value)) { $decoded = json_decode($value, true); $value = is_array($decoded) ? $decoded : array_map('trim', explode(',', $value)); } return array_values(array_filter(array_map(fn ($tag) => is_string($tag) ? trim($tag) : '', (array) $value), fn ($tag) => $tag !== '')); } }