minor; } catch ( \InvalidArgumentException ) { return null; } return $minor > 99999999999 ? null : $minor; } /** "3.49" (percent) -> 349 basis points; null when invalid or outside 0..30%. */ public static function percentToBps( mixed $raw ): ?int { if ( ! is_scalar( $raw ) ) { return null; } $s = rtrim( trim( str_replace( ',', '.', (string) $raw ) ), '%' ); if ( ! preg_match( '/^\d{1,2}(\.\d{1,2})?$/', $s ) ) { return null; } [ $w, $f ] = array_pad( explode( '.', $s, 2 ), 2, '' ); $bps = (int) $w * 100 + (int) str_pad( $f, 2, '0' ); return $bps <= 3000 ? $bps : null; } /** Whole number within [min,max], else null. Empty string -> null. */ public static function int( mixed $raw, int $min, int $max ): ?int { if ( ! is_scalar( $raw ) || ! preg_match( '/^\s*\d{1,12}\s*$/', (string) $raw ) ) { return null; } $n = (int) trim( (string) $raw ); return ( $n >= $min && $n <= $max ) ? $n : null; } /** Normalise a "one word per line" textarea: trimmed, lowercase, deduplicated, bounded. */ public static function words( string $text ): string { $out = array(); foreach ( preg_split( '/\r\n|\r|\n|,/', $text ) ?: array() as $w ) { $w = preg_replace( '/[\x00-\x1F\x7F<>]/u', '', trim( $w ) ) ?? ''; $w = function_exists( 'mb_strtolower' ) ? mb_strtolower( $w ) : strtolower( $w ); if ( '' === $w || ( function_exists( 'mb_strlen' ) ? mb_strlen( $w ) : strlen( $w ) ) > 40 ) { continue; } $out[ $w ] = true; if ( count( $out ) >= 500 ) { break; } } return implode( "\n", array_keys( $out ) ); } /** Event name slug: [a-z0-9_-], max 40, falling back to $default. */ public static function slug( mixed $raw, string $default = 'big_donation' ): string { $s = is_scalar( $raw ) ? strtolower( trim( (string) $raw ) ) : ''; $s = preg_replace( '/[^a-z0-9_-]+/', '_', $s ) ?? ''; $s = substr( trim( $s, '_-' ), 0, 40 ); return '' === $s ? $default : $s; } /** Plain single-line text with control chars and tags removed, bounded in length. */ public static function text( mixed $raw, int $max ): string { $s = is_scalar( $raw ) ? strip_tags( (string) $raw ) : ''; $s = preg_replace( '/[\x00-\x1F\x7F]/u', '', $s ) ?? ''; $s = trim( $s ); return function_exists( 'mb_substr' ) ? mb_substr( $s, 0, $max ) : substr( $s, 0, $max ); } /** Mask for showing that a secret exists without revealing it. */ public static function maskSecret( string $secret ): string { return '' === $secret ? '' : str_repeat( "\u{2022}", 12 ); } /** True when the submitted value is the untouched placeholder or empty (=> keep the stored secret). */ public static function secretUnchanged( string $submitted ): bool { $t = trim( $submitted ); return '' === $t || 1 === preg_match( '/^[\x{2022}*]+$/u', $t ); } }