result = array_merge( array_fill_keys(static::KEYS, null), [ "resultAvailable" => false, "present" => false, "available" => false, "null" => false, "valid" => false, "normalized" => false, ]); } function __get(string $name) { return $this->result[$name]; } function __set(string $name, $value): void { $this->result[$name] = $value; } protected static function replace_key(string &$message, ?string $key): void { if ($key) { $message = str_replace("{key}", $key, $message); } else { $message = str_replace("{key}: ", "", $message); $message = str_replace("cette valeur", "la valeur", $message); } } protected static function replace_orig(string &$message, $orig): void { $message = str_replace("{orig}", strval($orig), $message); } protected function getMessage(string $key, ScalarSchema $schema): string { $message = cl::get($schema->messages, $key); if ($message !== null) return $message; return cl::get(ref_schema::MESSAGES, $key); } function setMissing(ScalarSchema $schema): int { $this->resultAvailable = true; $this->present = false; $this->available = false; if (!$schema->required) { $this->null = false; $this->valid = true; $this->normalized = true; return ref_analyze::NORMALIZED; } else { $message = $this->getMessage("missing", $schema); self::replace_key($message, $schema->name); $this->message = $message; return ref_analyze::MISSING; } } function setUnavailable(ScalarSchema $schema): int { $this->resultAvailable = true; $this->present = true; $this->available = false; if (!$schema->required) { $this->null = false; $this->valid = true; $this->normalized = true; return ref_analyze::NORMALIZED; } else { $message = $this->getMessage("unavailable", $schema); self::replace_key($message, $schema->name); $this->message = $message; return ref_analyze::UNAVAILABLE; } } function setNull(ScalarSchema $schema): int { $this->resultAvailable = true; $this->present = true; $this->available = true; $this->null = true; if ($schema->nullable) { $this->valid = true; $this->normalized = true; return ref_analyze::NORMALIZED; } else { $message = $this->getMessage("null", $schema); self::replace_key($message, $schema->name); $this->message = $message; return ref_analyze::NULL; } } function setInvalid($value, ScalarSchema $schema): int { $this->resultAvailable = true; $this->present = true; $this->available = true; $this->null = false; $this->valid = false; $this->orig = $value; $message = $this->getMessage("invalid", $schema); self::replace_key($message, $schema->name); self::replace_orig($message, $schema->orig); $this->message = $message; return ref_analyze::INVALID; } function setValid(): int { $this->resultAvailable = true; $this->present = true; $this->available = true; $this->null = false; $this->valid = true; return ref_analyze::VALID; } function setNormalized(): int { $this->resultAvailable = true; $this->present = true; $this->available = true; $this->null = false; $this->valid = true; $this->normalized = true; return ref_analyze::NORMALIZED; } function throw(bool $throw): void { if ($throw) throw new ValueException($this->message); } }