Created
July 21, 2022 12:11
-
-
Save Nex-Otaku/65536abe0a5b947f1189e48c243a00be to your computer and use it in GitHub Desktop.
Пример сущности с необязательными полями
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Act | |
{ | |
private int $id; | |
private string $title; | |
private ?\DateTimeInterface $signedAt; | |
private function __construct( | |
int $id, | |
string $title, | |
?\DateTimeInterface $signedAt | |
) { | |
$this->id = $id; | |
$this->title = $title; | |
$this->signedAt = $signedAt; | |
} | |
public static function fromUserForm(string $title): self | |
{ | |
$record = Db::insert('acts', [ | |
'title' => $title, | |
'signed_at' => null, | |
]); | |
return new self( | |
$record->id, | |
$title, | |
null | |
); | |
} | |
public static function fromDb(int $id): self | |
{ | |
$record = Db::get('acts', [ | |
'id' => $id, | |
]); | |
return new self( | |
$record->id, | |
$record->title, | |
$record->signed_at | |
); | |
} | |
public function sign(\DateTimeInterface $dateTime): void | |
{ | |
$this->signedAt = $dateTime; | |
Db::update( | |
'acts', | |
[ | |
'id' => $this->id, | |
], | |
[ | |
'signed_at' => $this->signedAt->format('Y-m-d'), | |
] | |
); | |
} | |
public function getTitle(): string | |
{ | |
return $this->title; | |
} | |
public function getSignedAt(): ?\DateTimeInterface | |
{ | |
return $this->signedAt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment