$raw Input. * @param string $key Key to read. * @param string[] $allowed Allowed values. * @param string|null $fallback Value to use when nothing was sent; null when the field is required. * @return string|null */ public static function pick( array $raw, string $key, array $allowed, ?string $fallback ): ?string { $value = $raw[ $key ] ?? null; if ( null === $value || '' === $value ) { return $fallback; } $value = is_scalar( $value ) ? (string) $value : ''; return in_array( $value, $allowed, true ) ? $value : null; } /** * A valid Y-m-d date, or an empty string. * * @since 0.1.0 * * @param mixed $value Raw value. * @return string */ public static function date( $value ): string { $value = is_string( $value ) ? trim( $value ) : ''; return \HostKhata\Support\Date::is_valid_date( $value ) ? $value : ''; } /** * Hex colour such as #4f46e5, or an empty string. * * @since 0.1.0 * * @param mixed $value Raw value. * @return string */ public static function hex_color( $value ): string { $value = is_string( $value ) ? trim( $value ) : ''; return 1 === preg_match( '/^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i', $value ) ? strtolower( $value ) : ''; } /** * Lower case domain name without scheme, path or trailing dot, or an empty string when invalid. * * @since 0.1.0 * * @param mixed $value Raw value. * @return string */ public static function domain_name( $value ): string { $value = strtolower( trim( self::to_string( $value ) ) ); $value = (string) preg_replace( '#^[a-z][a-z0-9+.-]*://#', '', $value ); $value = explode( '/', $value )[0]; $value = rtrim( $value, '.' ); return Validator::is_domain( $value ) ? $value : ''; } /** * A plain non-negative decimal such as "12.50", or an empty string. * * @since 0.1.0 * * @param mixed $value Raw value. * @return string */ public static function decimal( $value ): string { $value = trim( self::to_string( $value ) ); return 1 === preg_match( '/^\d+(?:\.\d{1,4})?$/', $value ) ? $value : ''; } /** * Casts scalars to string and anything else to an empty string. * * @param mixed $value Raw value. * @return string */ private static function to_string( $value ): string { return is_scalar( $value ) ? (string) $value : ''; } }