@ 2020-01-15T10:31:57Z <?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);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.4.1 0.000 0.016 16.86 7.4.0 0.011 0.006 16.66 7.3.13 0.009 0.006 16.41 7.3.12 0.009 0.014 16.23 7.3.11 0.009 0.005 16.15 7.3.10 0.007 0.011 16.20 7.3.9 0.012 0.003 16.05 7.3.8 0.006 0.016 16.10 7.3.7 0.012 0.006 16.20 7.3.6 0.007 0.010 16.14 7.3.5 0.009 0.006 16.24 7.3.4 0.013 0.003 16.12 7.3.3 0.000 0.018 16.20 7.3.2 0.004 0.014 16.11 7.3.1 0.012 0.003 16.07 7.3.0 0.011 0.004 16.17 7.2.26 0.009 0.006 16.48 7.2.25 0.010 0.010 16.52 7.2.24 0.006 0.009 16.25 7.2.23 0.006 0.012 16.37 7.2.22 0.013 0.003 16.32 7.2.21 0.006 0.015 16.19 7.2.20 0.006 0.011 16.26 7.2.19 0.009 0.006 16.36 7.2.18 0.011 0.007 16.34 7.2.17 0.009 0.006 16.31 7.2.16 0.003 0.013 16.20 7.2.15 0.004 0.016 16.47 7.2.14 0.012 0.006 16.18 7.2.13 0.006 0.009 16.10 7.2.12 0.008 0.012 16.18 7.2.11 0.014 0.007 16.47 7.2.10 0.014 0.007 16.32 7.2.9 0.003 0.013 16.25 7.2.8 0.009 0.006 16.18 7.2.7 0.004 0.012 16.50 7.2.6 0.007 0.010 16.41 7.2.5 0.013 0.003 16.33 7.2.4 0.010 0.007 16.56 7.2.3 0.011 0.007 16.53 7.2.2 0.000 0.017 16.40 7.2.1 0.012 0.003 16.36 7.2.0 0.012 0.004 16.36
preferences:dark mode live preview
31.61 ms | 400 KiB | 5 Q