Skip to content

Instantly share code, notes, and snippets.

@zoka123
Last active February 20, 2020 14:29
Show Gist options
  • Save zoka123/27e27cb2762ccddb8786f8c4a52ac076 to your computer and use it in GitHub Desktop.
Save zoka123/27e27cb2762ccddb8786f8c4a52ac076 to your computer and use it in GitHub Desktop.
Simple API models
<?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()
        );
    }
}
<?php

declare(strict_types=1);

namespace App\UI\HTTP\Web\v1\Model\Article;

use Symfony\Component\Validator\Constraints as Assert;
use Undabot\SymfonyJsonApi\Model\ApiModel;
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\Attribute;
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\ToMany;
use Undabot\SymfonyJsonApi\Service\Resource\Validation\Constraint\ResourceType;

/**
 * @ResourceType(type="article")
 * @psalm-immutable
 */
class ArticleReadModel implements ApiModel
{
    /** @var string */
    public $id;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank
     */
    public $title;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank
     */
    public $slug;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank(allowNull=false)
     * @Assert\Url
     */
    public $leadImage;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank
     */
    public $introText;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank
     */
    public $content;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank
     * @Assert\DateTime(format=DateTime::ATOM)
     */
    public $publishDate;

    /**
     * @var bool
     * @Attribute
     * @Assert\NotNull
     */
    public $published;

    /**
     * @var bool
     * @Attribute
     * @Assert\NotNull
     */
    public $featured;

    /**
     * @var string
     * @Attribute
     * @Assert\NotBlank
     */
    public $author;

    /**
     * @var string[]
     * @ToMany(name="categories", type="category")
     * @Assert\Type(type="array")
     */
    public $categoryIds;

    /** @param string[] $categoryIds */
    public function __construct(
        string $id,
        string $title,
        string $slug,
        string $leadImage,
        string $introText,
        string $content,
        string $publishDate,
        bool $published,
        bool $featured,
        string $author,
        array $categoryIds
    ) {
        $this->id = $id;
        $this->title = $title;
        $this->slug = $slug;
        $this->leadImage = $leadImage;
        $this->introText = $introText;
        $this->content = $content;
        $this->publishDate = $publishDate;
        $this->published = $published;
        $this->featured = $featured;
        $this->author = $author;
        $this->categoryIds = $categoryIds;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment