MessageMetadata.php

Code Hygiene Score: 92

Keine Issues gefunden.

Klassen 1

Funktionen 14

Verwendet von 3

Code

<?php

declare(strict_types=1);

namespace Domain\ValueObject;

// @responsibility: Immutable Value Object for message metadata (sources, chunks, collections)

final class MessageMetadata
{
    /** @var array<mixed>|null */
    private ?array $sources;

    /** @var array<string>|null */
    private ?array $collections;

    /** @var array<mixed>|null */
    private ?array $chunksUsed;

    private ?int $contextLimit;

    /**
     * @param array<mixed>|null $sources
     * @param array<string>|null $collections
     * @param array<mixed>|null $chunksUsed
     */
    private function __construct(
        ?array $sources,
        ?array $collections,
        ?array $chunksUsed,
        ?int $contextLimit
    ) {
        $this->sources = $sources;
        $this->collections = $collections;
        $this->chunksUsed = $chunksUsed;
        $this->contextLimit = $contextLimit;
    }

    /**
     * @param array<mixed>|null $sources
     * @param array<string>|null $collections
     * @param array<mixed>|null $chunksUsed
     */
    public static function create(
        ?array $sources = null,
        ?array $collections = null,
        ?array $chunksUsed = null,
        ?int $contextLimit = null
    ): self {
        return new self($sources, $collections, $chunksUsed, $contextLimit);
    }

    public static function empty(): self
    {
        return new self(null, null, null, null);
    }

    /** @return array<mixed>|null */
    public function sources(): ?array
    {
        return $this->sources;
    }

    /** @return array<string>|null */
    public function collections(): ?array
    {
        return $this->collections;
    }

    /** @return array<mixed>|null */
    public function chunksUsed(): ?array
    {
        return $this->chunksUsed;
    }

    public function contextLimit(): ?int
    {
        return $this->contextLimit;
    }

    public function hasSources(): bool
    {
        return $this->sources !== null && count($this->sources) > 0;
    }

    public function hasCollections(): bool
    {
        return $this->collections !== null && count($this->collections) > 0;
    }

    public function hasChunks(): bool
    {
        return $this->chunksUsed !== null && count($this->chunksUsed) > 0;
    }

    /**
     * @param array<mixed> $sources
     */
    public function withSources(array $sources): self
    {
        return new self($sources, $this->collections, $this->chunksUsed, $this->contextLimit);
    }

    /**
     * @param array<string> $collections
     */
    public function withCollections(array $collections): self
    {
        return new self($this->sources, $collections, $this->chunksUsed, $this->contextLimit);
    }

    /**
     * @param array<mixed> $chunksUsed
     */
    public function withChunksUsed(array $chunksUsed): self
    {
        return new self($this->sources, $this->collections, $chunksUsed, $this->contextLimit);
    }

    public function withContextLimit(int $contextLimit): self
    {
        return new self($this->sources, $this->collections, $this->chunksUsed, $contextLimit);
    }

    public function equals(self $other): bool
    {
        return $this->sources === $other->sources
            && $this->collections === $other->collections
            && $this->chunksUsed === $other->chunksUsed
            && $this->contextLimit === $other->contextLimit;
    }
}
← Übersicht Graph