Last active
December 19, 2021 10:50
-
-
Save ismail1432/8162ca1601d87da7a76d6c3333d3ef95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
final class ArticleStatus | |
{ | |
public DRAFT = 'draft'; | |
public PUBLISHED = 'published'; | |
public DELETED = 'deleted'; | |
private string $value; | |
public function ALL = [ | |
self::DRAFT, | |
self::PUBLISHED, | |
self::DELETED, | |
]; | |
public static function create(string $status): self | |
{ | |
if(!in_array($status, self::ALL)) { | |
throw new LogicException("Unsupported value blabla..."); | |
} | |
$articleStatus = new self; | |
$articleStatus->value = $status; | |
return $articleStatus; | |
} | |
public function getValue(): string | |
{ | |
return $this->getValue(); | |
} | |
} | |
final class Article | |
{ | |
private ArticleStatus $status; | |
public function markAsPublished(): void | |
{ | |
if ($this->status->getValue() === ArticleStatus::DELETED)) { | |
throw new LogicException(sprintf("Cannot mark as published as it is deleted")); | |
} | |
$this->status = ArticleStatus::create(ArticleStatus::PUBLISHED); | |
} | |
} | |
class PublishArticleHandler | |
{ | |
public function __invoke(Article $article) | |
{ | |
$article->markAsPublished(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment