<?php
declare(strict_types=1);
namespace App\Infrastructure\UI\Http\Common;
abstract class ApiModel implements \JsonSerializable
{
/**
* @return array<string,string|int|float|\JsonSerializable>
*/
public function jsonSerialize(): array
{
return get_object_vars($this);
}
}
<?php
declare(strict_types=1);
namespace App\Infrastructure\UI\Http\Models;
use App\Domain\Comment\Comment;
use App\Infrastructure\UI\Http\Common\ApiModel;
use Assert\Assertion;
final class CommentApiModel extends ApiModel
{
/** @var string */
public $id;
/** @var string */
public $text;
/** @var string */
public $url;
/** @var string */
public $created;
/** @var string */
public $imported;
/** @var bool */
public $hidden;
/** @var null|CommentActionApiModel */
public $action;
/** @var null|CommentNluApiModel */
public $nlu;
/** @var string */
public $postId;
public function __construct(
string $id,
string $text,
string $url,
string $created,
string $imported,
bool $hidden,
?CommentActionApiModel $action,
?CommentNluApiModel $nlu,
string $postId
) {
$this->id = $id;
$this->text = $text;
$this->url = $url;
$this->created = $created;
$this->imported = $imported;
$this->hidden = $hidden;
$this->action = $action;
$this->nlu = $nlu;
$this->postId = $postId;
}
public static function fromEntity(Comment $comment): self
{
$facebookMetadata = $comment->getFacebookMetadata();
Assertion::notNull($facebookMetadata);
$dateTimeFormatter = function (?\DateTimeInterface $dateTime) {
if (null !== $dateTime) {
return $dateTime->format('d.m.Y H:i:s');
}
return null;
};
$id = $comment->getId();
$text = $comment->getText();
$imported = $dateTimeFormatter($comment->getImportedAt());
$url = $facebookMetadata->url;
$created = $dateTimeFormatter($facebookMetadata->commentDate);
$hidden = $facebookMetadata->isHidden;
$nlu = null;
$action = null;
$nluMetadata = $comment->getNluMetadata();
if (null !== $nluMetadata
&& null !== $nluMetadata->getFinalIntent()
&& null !== $nluMetadata->confidence) {
$nlu = new CommentNluApiModel(
$nluMetadata->nluStatus,
$nluMetadata->getFinalIntent(),
$nluMetadata->entities,
$nluMetadata->confidence
);
}
$action = null;
$actionData = $comment->getAction();
if (null !== $actionData) {
$action = new CommentActionApiModel(
$actionData->getStatus(),
$actionData->getType(),
$actionData->getReplyText()
);
}
return new self(
$id,
$text,
$url,
$created,
$imported,
$hidden,
$action,
$nlu,
$comment->getPost()->getId()
);
}
}