"; } elseif (is_array($value)) { $values = $value; $parts = []; foreach ($values as $value) { $parts[] = self::value($value); } return "[" . implode(", ", $parts) . "]"; } elseif (is_string($value)) { return $value; } else { return var_export($value, true); } } private static function message($value, ?string $message, ?string $kind, ?string $prefix, ?string $suffix): string { if ($kind === null) $kind = "value"; if ($message === null) $message = "$kind$suffix"; if ($value !== null) { $value = self::value($value); if ($prefix) $prefix = "$prefix: $value"; else $prefix = $value; } if ($prefix) $prefix = "$prefix: "; return $prefix.$message; } static final function null(?string $kind=null, ?string $prefix=null, ?string $message=null): self { return new static(self::message(null, $message, $kind, $prefix, " is null")); } static final function invalid($value=null, ?string $kind=null, ?string $prefix=null, ?string $message=null): self { return new static(self::message($value, $message, $kind, $prefix, " is invalid")); } static final function unexpected_class($class, string $expected_class) { if (is_object($class)) $class = get_class($class); return new static(self::message($class, null, "class", null, " is invalid, expected $expected_class")); } static final function forbidden($value=null, ?string $kind=null, ?string $prefix=null, ?string $message=null): self { return new static(self::message($value, $message, $kind, $prefix, " is forbidden")); } }