123 lines
3.2 KiB
PHP
123 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Database\ArcadiaReplicate\Factories;
|
|
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Infra\Config\ArcadiaStorageConfig;
|
|
|
|
abstract class AbstractTableFactory implements TableFactory
|
|
{
|
|
/** @var list<callable(array<string, mixed>): array<string, mixed>|array<string, mixed>> */
|
|
protected array $states = [];
|
|
|
|
protected string $connection;
|
|
|
|
public function __construct(?string $connection = null)
|
|
{
|
|
$this->connection = $connection ?? static::connectionName();
|
|
}
|
|
|
|
public static function connectionName(): string
|
|
{
|
|
return resolve(ArcadiaStorageConfig::class)->databaseConnection;
|
|
}
|
|
|
|
public static function tableName(?string $connection = null): string
|
|
{
|
|
return static::table()::table($connection ?? static::connectionName());
|
|
}
|
|
|
|
public static function query(?string $connection = null): Builder
|
|
{
|
|
$connName = $connection ?? static::connectionName();
|
|
|
|
return DB::connection($connName)
|
|
->table(static::tableName($connName));
|
|
}
|
|
|
|
public static function count(?string $connection = null): int
|
|
{
|
|
return static::query($connection)->count();
|
|
}
|
|
|
|
public static function truncate(?string $connection = null): void
|
|
{
|
|
static::query($connection)->delete();
|
|
}
|
|
|
|
public static function deleteAll(?string $connection = null): void
|
|
{
|
|
static::truncate($connection);
|
|
}
|
|
|
|
public static function new(?string $connection = null): static
|
|
{
|
|
return new static($connection);
|
|
}
|
|
|
|
/**
|
|
* @param callable(array<string, mixed>): array<string, mixed>|array<string, mixed> $state
|
|
*/
|
|
public function state(callable | array $state): static
|
|
{
|
|
$clone = clone $this;
|
|
$clone->states[] = $state;
|
|
|
|
return $clone;
|
|
}
|
|
|
|
/**
|
|
* Génère le tableau de données avec les states et surcharges appliqués.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function raw(array $attributes = []): array
|
|
{
|
|
$data = $this->definition();
|
|
|
|
foreach ($this->states as $state) {
|
|
$stateData = is_callable($state) ? $state($data) : $state;
|
|
$data = array_merge($data, $stateData);
|
|
}
|
|
|
|
return array_merge($data, $attributes);
|
|
}
|
|
|
|
/**
|
|
* Insère un enregistrement dans la base Oracle répliquée et retourne le tableau inséré.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function create(array $attributes = []): array
|
|
{
|
|
$data = $this->raw($attributes);
|
|
|
|
DB::connection($this->connection)
|
|
->table(static::tableName($this->connection))
|
|
->insert($data);
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Insère N enregistrements dans la table.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function createMany(int $count, array $attributes = []): array
|
|
{
|
|
$rows = [];
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$rows[] = $this->create($attributes);
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
}
|