vlw.se/src/Database/Models/Work/Timeline.php

73 lines
No EOL
2.4 KiB
PHP

<?php
namespace VLW\Database\Models\Work;
use \VV;
use \vlw\MySQL\Order;
use VLW\Helpers\UUID;
use VLW\Database\Database;
use VLW\Database\Models\Model;
use VLW\Database\Models\Work\Work;
use VLW\Database\Tables\Work\Timeline as TimelineTable;
require_once VV::root("src/Helpers/UUID.php");
require_once VV::root("src/Database/Database.php");
require_once VV::root("src/Database/Models/Model.php");
require_once VV::root("src/Database/Models/Work/Work.php");
require_once VV::root("src/Database/Tables/Work/Timeline.php");
class Timeline extends Model {
final public static function new(Work $work): self {
$id = UUID::v4();
if (!parent::create(TimelineTable::TABLE, [
TimelineTable::ID->value => $id,
TimelineTable::REF_WORK_ID->value => $work->id,
TimelineTable::YEAR->value => (int) $work->date_created->format("Y"),
TimelineTable::MONTH->value => (int) $work->date_created->format("n"),
TimelineTable::DAY->value => (int) $work->date_created->format("j"),
])) { throw new Exception("Failed to create Work Timeline entity"); }
return new Timeline($id);
}
final public static function all(): array {
return array_map(fn(array $work): Timeline => new Timeline($work[TimelineTable::ID->value]), new Database()
->from(TimelineTable::TABLE)
->order([
TimelineTable::YEAR->value => Order::DESC,
TimelineTable::MONTH->value => Order::DESC,
TimelineTable::DAY->value => Order::DESC
])
->select(TimelineTable::ID->value)
->fetch_all(MYSQLI_ASSOC)
);
}
public function __construct(public readonly string $id) {
parent::__construct(TimelineTable::TABLE, TimelineTable::values(), [
TimelineTable::ID->value => $this->id
]);
}
final public Work $work {
get => new Work($this->get(TimelineTable::REF_WORK_ID->value));
set (Work $work) => $this->set(TimelineTable::REF_WORK_ID->value, $work->id);
}
final public int $year {
get => $this->get(TimelineTable::YEAR->value);
set (int $year) => $this->set(TimelineTable::YEAR->value, $year);
}
final public int $month {
get => $this->get(TimelineTable::MONTH->value);
set (int $month) => $this->set(TimelineTable::MONTH->value, $month);
}
final public int $day {
get => $this->get(TimelineTable::DAY->value);
set (int $day) => $this->set(TimelineTable::DAY->value, $day);
}
}