<?php
class Assert
{
private const UUID_LENGTH = 32;
public static function validUuid(string $value) : string
{
if (strlen($value) !== static::UUID_LENGTH || false === ctype_xdigit($value)) {
throw new \InvalidArgumentException('Wrong uuid format.');
}
return __CLASS__;
}
public static function notEmptyString(string $value) : string
{
if ('' === $value) {
throw new InvalidArgumentException('Value must not be empty.');
}
return __CLASS__;
}
public static function notEmptyArray(array $value) : string
{
if (0 >= count($value)) {
throw new InvalidArgumentException('Array must not be empty.');
}
return __CLASS__;
}
}
class Id
{
private string $value;
public function __construct(?string $uuid = null)
{
$uuid = $uuid ?? $this->generateuuidV4();
Assert::validUuid($uuid);
$this->value = $uuid;
}
public function __toString() : string
{
return $this->value;
}
public function isEqual(Id $id) : bool
{
return $this->value === (string)$id;
}
private function generateUuidV4() : string
{
return bin2hex(random_bytes(16));
}
}
interface Entity
{
public function getId() : Id;
}
class Comment implements Entity
{
private Id $id;
private Id $userId;
private string $text;
public function __construct(Id $id, Id $userId, string $text)
{
Assert::notEmptyString($text);
$this->id = $id;
$this->userId = $userId;
$this->text = $text;
}
public function getId() : Id
{
return $this->id;
}
public function getText() : string
{
return $this->text;
}
public function changeText(string $text) : void
{
$this->text = $text;
}
}
class ModeratedComment extends Comment
{
private Comment $comment;
public function __construct(Comment $comment)
{
$this->comment = $this->moderate($comment);
}
public function getId() : Id
{
return $this->comment->getId();
}
public function getText() : string
{
return $this->comment->getText();
}
public function changeText(string $text) : void
{
$this->comment->changeText($text);
$this->comment = $this->moderate($this->comment);
}
private function moderate(Comment $comment) : Comment
{
$comment->changeText('Моча - сила');
return $comment;
}
}
class Feed implements Entity
{
private Id $id;
private Id $ownerId;
private array $publishers;
private array $comments;
public function __construct(
Id $id,
Id $ownerId,
array $publishers,
array $comments = []
) {
Assert::notEmptyArray($publishers);
$this->id = $id;
$this->ownerId = $ownerId;
$this->publishers = $publishers;
$this->comments = $comments;
}
public function getId() : Id
{
return $this->id;
}
public function addComment(ModeratedComment $moderatedComment) {
$this->comments[(string)$moderatedComment->getId()] = $moderatedComment;
}
}
$feedId = new Id('3a90426a01e414d00f281ae92436f11d');
$feedOwnerId = new Id('da41d5f6b9ef685ddc3fc58f8f2b33f8');
$firstPublisherId = new Id('da41d5f6b9ef685ddc3fc58f8f2b33f8');
$secondPublisherId = new Id('76319d3a35d3ec306dc299d446ab46e6');
$publishers = [
(string)$firstPublisherId => $firstPublisherId,
(string)$secondPublisherId => $secondPublisherId,
];
$feed = new Feed($feedId, $feedOwnerId, $publishers);
$userId = new Id('da41d5f6b9ef685ddc3fc58f8f2b33f8');
$moderatedComment = new ModeratedComment(new Comment(new Id, $userId, 'Test comment'));
$feed->addComment($moderatedComment);
var_dump($feed);
preferences:
36.11 ms | 402 KiB | 5 Q