Add setters for the existing entities.

This commit is contained in:
Dave Smith-Hayes 2024-05-29 21:56:42 -04:00
parent 8d7bcd0c2a
commit db1e7e04f6
3 changed files with 61 additions and 18 deletions

View File

@ -7,9 +7,14 @@ use DateTimeImmutable;
trait Entity trait Entity
{ {
protected int $id; protected ?int $id;
protected DateTimeImmutable $createdAt; protected ?DateTimeImmutable $createdAt;
protected DateTime $updatedAt; protected ?DateTime $updatedAt;
public function isNew(): bool
{
return (bool) $this->id;
}
public function setId(int $id): void public function setId(int $id): void
{ {

View File

@ -10,7 +10,7 @@ class Channel
public function __construct( public function __construct(
private string $name, private string $name,
private ?string $description = '', private string $description,
private ?string $link = '', private ?string $link = '',
private ?string $language = '', private ?string $language = '',
private ?string $copyright = '', private ?string $copyright = '',
@ -32,16 +32,45 @@ class Channel
return $this->link; return $this->link;
} }
public function setLink(string $link): Channel
{
$this->link = $link;
return $this;
}
public function getLanguage(): string public function getLanguage(): string
{ {
return $this->language; return $this->language;
} }
public function setLanguage(string $language): Channel
{
$this->language = $language;
return $this;
}
public function getCopyright(): string public function getCopyright(): string
{ {
return $this->copyright; return $this->copyright;
} }
public function setCopyright(string $copyright): Channel
{
$this->copyright = $copyright;
return $this;
}
public function isExplicit(): bool
{
return $this->explicit;
}
public function setExplicit(bool $explicit): Channel
{
$this->explicit = $explicit;
return $this;
}
/** /**
* @param array $props The properties of the Channel model * @param array $props The properties of the Channel model
* @return Channel * @return Channel

View File

@ -19,24 +19,32 @@ class User
return $this->email; return $this->email;
} }
public function setEmail(string $email): User
{
$this->email = $email;
return $this;
}
public function getPassword(): string public function getPassword(): string
{ {
return $this->password; return $this->password;
} }
public function setPassword(string $password): User
{
$this->password = $password;
return $this;
}
public function getName(): string public function getName(): string
{ {
return $this->name; return $this->name;
} }
/** public function setName(string $name): User
* If the `id` property exists, we can assume this entity already exists.
*
* @return bool
*/
public function isNew(): bool
{ {
return (bool) $this->getId(); $this->name = $name;
return $this;
} }
/** /**
@ -45,18 +53,19 @@ class User
*/ */
public static function fromArray(array $props): User public static function fromArray(array $props): User
{ {
$user = new self( $user = new self($props['email'], $props['password'], $props['name']);
$props['email'],
$props['password'],
$props['name']
);
if ($props['id']) { if ($props['id']) {
$user->setId($props['id']); $user->setId($props['id']);
} }
if ($props['createdAt']) {
$user->setCreatedAt($props['createdAt']); $user->setCreatedAt($props['createdAt']);
}
if ($props['updatedAt']) {
$user->setUpdatedAt($props['updatedAt']); $user->setUpdatedAt($props['updatedAt']);
}
return $user; return $user;
} }