ActiveStatus.php

Code Hygiene Score: 100

Keine Issues gefunden.

Klassen 1

Funktionen 8

Code

<?php

declare(strict_types=1);

namespace Domain\ValueObject;

// @responsibility: Active-Status-Enum als Ersatz für Boolean is_active Felder

enum ActiveStatus: string
{
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';

    /**
     * Check if status is active.
     */
    public function isActive(): bool
    {
        return $this === self::ACTIVE;
    }

    /**
     * Get toggled status.
     */
    public function toggle(): self
    {
        return $this === self::ACTIVE ? self::INACTIVE : self::ACTIVE;
    }

    /**
     * Create from boolean value.
     */
    public static function fromBool(bool $active): self
    {
        return $active ? self::ACTIVE : self::INACTIVE;
    }

    /**
     * Create from database tinyint (0/1).
     */
    public static function fromInt(int $value): self
    {
        return $value === 1 ? self::ACTIVE : self::INACTIVE;
    }

    /**
     * Convert to boolean.
     */
    public function toBool(): bool
    {
        return $this === self::ACTIVE;
    }

    /**
     * Convert to database tinyint (0/1).
     */
    public function toInt(): int
    {
        return $this === self::ACTIVE ? 1 : 0;
    }

    /**
     * Get human-readable label.
     */
    public function label(): string
    {
        return match ($this) {
            self::ACTIVE => 'Aktiv',
            self::INACTIVE => 'Inaktiv',
        };
    }

    /**
     * Get CSS class for badge styling.
     */
    public function badgeClass(): string
    {
        return match ($this) {
            self::ACTIVE => 'completed',
            self::INACTIVE => 'secondary',
        };
    }
}
← Übersicht Graph