<?php
/**
 * Validation helpers. Returns true/false; never throws to avoid leaking info.
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Utils;

defined( 'ABSPATH' ) || exit;

final class Validator {

	public static function is_valid_email( string $value ): bool {
		return is_email( $value ) && strlen( $value ) <= 190;
	}

	public static function is_valid_url( string $value ): bool {
		return (bool) wp_http_validate_url( $value );
	}

	public static function is_in( $value, array $allowed ): bool {
		return in_array( $value, $allowed, true );
	}

	public static function is_in_range_int( $value, int $min, int $max ): bool {
		return is_numeric( $value ) && (int) $value >= $min && (int) $value <= $max;
	}

	public static function is_iso_date( string $value ): bool {
		return (bool) preg_match( '/^\d{4}-\d{2}-\d{2}$/', $value ) && false !== strtotime( $value );
	}

	public static function is_safe_callback_url( string $url ): bool {
		if ( ! self::is_valid_url( $url ) ) {
			return false;
		}
		$parsed = wp_parse_url( $url );
		if ( ! isset( $parsed['scheme'], $parsed['host'] ) ) {
			return false;
		}
		if ( ! in_array( $parsed['scheme'], array( 'http', 'https' ), true ) ) {
			return false;
		}
		$home  = wp_parse_url( home_url() );
		return isset( $home['host'] ) && strcasecmp( $parsed['host'], (string) $home['host'] ) === 0;
	}
}
