Files
lucent-laravel/src/Record/System.php
T

63 lines
1.3 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Record;
use Carbon\Carbon;
use Lucent\Schema\Schema;
2023-10-04 13:32:30 +03:00
readonly class System
2023-10-02 23:10:49 +03:00
{
function __construct(
2023-10-04 13:32:30 +03:00
public int $version,
public string $createdBy,
public string $updatedBy,
public string $createdAt,
public string $updatedAt,
2023-10-02 23:10:49 +03:00
)
{
}
public static function fromArray(array $data): System
{
return new System(
version: data_get($data, "version"),
createdBy: data_get($data, "createdBy"),
updatedBy: data_get($data, "updatedBy"),
createdAt: data_get($data, "createdAt"),
updatedAt: data_get($data, "updatedAt"),
);
}
2023-10-04 13:32:30 +03:00
public static function newRecord(string $userId): System
2023-10-02 23:10:49 +03:00
{
$now = Carbon::now()->toJson();
return new System(
version: 1,
createdBy: $userId,
updatedBy: $userId,
createdAt: $now,
updatedAt: $now,
);
}
2023-10-04 13:32:30 +03:00
public function update(string $userId): System
2023-10-02 23:10:49 +03:00
{
$now = Carbon::now()->toJson();
return new System(
version: $this->version + 1,
createdBy: $this->createdBy,
updatedBy: $userId,
createdAt: $this->createdAt,
updatedAt: $now,
);
}
}