<?php
/**
 * General purpose sanitization helpers.
 *
 * @package MailDoctor
 */

namespace MailDoctor\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Provides sanitization helpers for primitive values.
 */
class Sanitize {

	/**
	 * Sanitize a boolean value.
	 *
	 * @param mixed $value Raw value.
	 * @return bool
	 */
	public static function bool( $value ): bool {
		if ( is_bool( $value ) ) {
			return $value;
		}

		if ( is_string( $value ) ) {
			$value = strtolower( trim( $value ) );
			return in_array( $value, array( '1', 'true', 'yes', 'on' ), true );
		}

		return (bool) $value;
	}

	/**
	 * Sanitize integer value.
	 *
	 * @param mixed $value Raw value.
	 * @param int   $default Default fallback.
	 * @return int
	 */
	public static function int( $value, int $default = 0 ): int {
		if ( is_numeric( $value ) ) {
			return (int) $value;
		}

		return $default;
	}
}
