self::STATE_DRAFT, self::STATE_PUBLISHED_ID => self::STATE_PUBLISHED, ]; private int $id; private string $name; public static function fromInt(int $statusId) { self::ensureIsValidId($statusId); return new self($statusId, self::$validStates[$statusId]); } public static function fromString(string $status) { self::ensureIsValidName($status); $state = array_search($status, self::$validStates); if ($state === false) { throw new InvalidArgumentException('Invalid state given!'); } return new self($state, $status); } private function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } public function toInt(): int { return $this->id; } /** * there is a reason that I avoid using __toString() as it operates outside of the stack in PHP * and is therefor not able to operate well with exceptions */ public function toString(): string { return $this->name; } private static function ensureIsValidId(int $status) { if (!in_array($status, array_keys(self::$validStates), true)) { throw new InvalidArgumentException('Invalid status id given'); } } private static function ensureIsValidName(string $status) { if (!in_array($status, self::$validStates, true)) { throw new InvalidArgumentException('Invalid status name given'); } } }