init
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Lucent\Record;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Lucent\Schema\Schema;
|
||||
|
||||
class System
|
||||
{
|
||||
|
||||
|
||||
function __construct(
|
||||
public readonly string $schema,
|
||||
public readonly int $version,
|
||||
public readonly string $status,
|
||||
public readonly string $createdBy,
|
||||
public readonly string $updatedBy,
|
||||
public readonly string $createdAt,
|
||||
public readonly string $updatedAt,
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function fromArray(array $data): System
|
||||
{
|
||||
return new System(
|
||||
schema: data_get($data, "schema"),
|
||||
version: data_get($data, "version"),
|
||||
status: data_get($data, "status"),
|
||||
createdBy: data_get($data, "createdBy"),
|
||||
updatedBy: data_get($data, "updatedBy"),
|
||||
createdAt: data_get($data, "createdAt"),
|
||||
updatedAt: data_get($data, "updatedAt"),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static function newRecord(Schema $schema, string $userId, ?string $status = null): System
|
||||
{
|
||||
$now = Carbon::now()->toJson();
|
||||
return new System(
|
||||
schema: $schema->name,
|
||||
version: 1,
|
||||
status: (new RecordStatus($status))->value(),
|
||||
createdBy: $userId,
|
||||
updatedBy: $userId,
|
||||
createdAt: $now,
|
||||
updatedAt: $now,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function update(string $userId, ?string $status = null): System
|
||||
{
|
||||
$now = Carbon::now()->toJson();
|
||||
$newStatus = $status ?? $this->status;
|
||||
return new System(
|
||||
schema: $this->schema,
|
||||
version: $this->version + 1,
|
||||
status: (new RecordStatus($newStatus))->value(),
|
||||
createdBy: $this->createdBy,
|
||||
updatedBy: $userId,
|
||||
createdAt: $this->createdAt,
|
||||
updatedAt: $now,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user