EntityTaxonomyMapping.php

Code Hygiene Score: 89

Keine Issues gefunden.

Dependencies 1

Klassen 1

Funktionen 21

Verwendet von 3

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Domain\Entity;

// @responsibility: Mapping zwischen Entity und Taxonomie-Begriff

use Domain\ValueObject\Confidence;

class EntityTaxonomyMapping
{
    private ?int $id = null;
    private int $entityId;
    private int $taxonomyTermId;
    private Confidence $relevance;
    private bool $validated = false;
    private \DateTimeImmutable $createdAt;
    private ?\DateTimeImmutable $updatedAt = null;

    public function __construct()
    {
        $this->createdAt = new \DateTimeImmutable();
        $this->relevance = Confidence::medium();
    }

    // Getters
    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEntityId(): int
    {
        return $this->entityId;
    }

    public function getTaxonomyTermId(): int
    {
        return $this->taxonomyTermId;
    }

    public function getRelevance(): Confidence
    {
        return $this->relevance;
    }

    public function isValidated(): bool
    {
        return $this->validated;
    }

    public function getCreatedAt(): \DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): ?\DateTimeImmutable
    {
        return $this->updatedAt;
    }

    // Setters
    public function setId(int $id): self
    {
        $this->id = $id;

        return $this;
    }

    public function setEntityId(int $entityId): self
    {
        $this->entityId = $entityId;

        return $this;
    }

    public function setTaxonomyTermId(int $taxonomyTermId): self
    {
        $this->taxonomyTermId = $taxonomyTermId;

        return $this;
    }

    public function setRelevance(Confidence $relevance): self
    {
        $this->relevance = $relevance;
        $this->touch();

        return $this;
    }

    public function setRelevanceFromFloat(float $value): self
    {
        $this->relevance = Confidence::fromFloat($value);
        $this->touch();

        return $this;
    }

    public function setValidated(bool $validated): self
    {
        $this->validated = $validated;
        $this->touch();

        return $this;
    }

    public function setCreatedAt(\DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    // Domain Methods
    public function validate(): self
    {
        $this->validated = true;
        $this->touch();

        return $this;
    }

    public function invalidate(): self
    {
        $this->validated = false;
        $this->touch();

        return $this;
    }

    private function touch(): void
    {
        $this->updatedAt = new \DateTimeImmutable();
    }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'entity_id' => $this->entityId,
            'taxonomy_term_id' => $this->taxonomyTermId,
            'relevance' => $this->relevance->value(),
            'validated' => $this->validated ? 1 : 0,
            'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
            'updated_at' => $this->updatedAt?->format('Y-m-d H:i:s'),
        ];
    }

    public static function fromArray(array $data): self
    {
        $mapping = new self();

        if (isset($data['id'])) {
            $mapping->setId((int) $data['id']);
        }
        if (isset($data['entity_id'])) {
            $mapping->setEntityId((int) $data['entity_id']);
        }
        if (isset($data['taxonomy_term_id'])) {
            $mapping->setTaxonomyTermId((int) $data['taxonomy_term_id']);
        }
        if (isset($data['relevance'])) {
            $mapping->relevance = Confidence::fromFloat((float) $data['relevance']);
        }
        if (isset($data['validated'])) {
            $mapping->validated = (bool) $data['validated'];
        }
        if (isset($data['created_at'])) {
            $mapping->setCreatedAt(new \DateTimeImmutable($data['created_at']));
        }
        if (isset($data['updated_at'])) {
            $mapping->setUpdatedAt(new \DateTimeImmutable($data['updated_at']));
        }

        return $mapping;
    }
}
← Übersicht Graph