<?php

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

class wpforms_persian_sms_helpers {

	public static function validateMobile( $mobile ) {
		$mobile = self::modifyMobile( $mobile );

		return preg_match( '/9\d{9,}?$/', trim( $mobile ) );
		//return is_numeric( $mobile );
	}

	public static function modifyMobile( $mobile ) {
		if ( is_array( $mobile ) ) {
			return array_map( array( self::class, __FUNCTION__ ), $mobile );
		}

		$mobile   = self::EnglishNumberMobile( $mobile );
		$modified = preg_replace( '/\D/is', '', (string) $mobile );

		if ( substr( $mobile, 0, 1 ) == '+' ) {
			return '+' . $modified;
		} elseif ( substr( $modified, 0, 2 ) == '00' ) {
			return '+' . substr( $modified, 2 );
		} elseif ( substr( $modified, 0, 1 ) == '0' ) {
			return $modified;
		} elseif ( ! empty( $modified ) ) {
			$modified = '0' . $modified;
		}

		return $modified;
	}

	public static function EnglishNumberMobile( $mobile ) {
		if ( is_array( $mobile ) ) {
			return array_map( array( self::class, __FUNCTION__ ), $mobile );
		} else {
			$mobile = sanitize_text_field( $mobile );
			$mobile = str_ireplace( array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ), array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ), $mobile ); //farsi
			$mobile = str_ireplace( array( '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩' ), array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ), $mobile ); //arabi

			return $mobile;
		}
	}

	public static function nusoap() {
		if ( ! class_exists( 'nusoap_client' ) ) {
			require_once 'lib/nusoap.php';
		}
	}

}