kind = $kind; } public static function length(int $found): self { $error = new self(self::LENGTH); $error->found = $found; return $error; } public static function invalidCharacter(string $char, int $index): self { $error = new self(self::INVALID_CHARACTER); $error->char = $char; $error->index = $index; return $error; } public static function segment(string $segment): self { $error = new self(self::SEGMENT); $error->segment = $segment; return $error; } public function __toString(): string { switch ($this->kind) { case self::LENGTH: return 'expected ' . Postcode::LENGTH . " letters and digits, found {$this->found}"; case self::INVALID_CHARACTER: return "invalid character '{$this->char}' at index {$this->index}"; default: return "invalid {$this->segment} segment"; } } }