MOON
Server: Apache
System: Linux server.netcommlabs.in 3.10.0-1160.83.1.el7.x86_64 #1 SMP Wed Jan 25 16:41:43 UTC 2023 x86_64
User: dermova (1051)
PHP: 5.4.45
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/dermova/www/india/wp-content/plugins/mailchimp-for-wp/includes/class-validator.php
<?php

/**
 * Class MC4WP_Validator
 *
 * @ignore
 * @access private
 */
class MC4WP_Validator {

	/**
	 * @var array Array of fields to be validated.
	 */
	public $fields;

	/**
	 * @var array Array of validation rules.
	 */
	public $rules = array();

	/**
	 * @var array Array of error codes
	 */
	public $errors = array();

	/**
	 * @param array $fields
	 * @param array $rules
	 */
	public function __construct( array $fields, array $rules = array() ) {
		$this->fields = $fields;
		$this->rules = $rules;
	}

	/**
	 * @param string $field
	 * @param string|callable $rule
	 * @param string $error_code Reference to the error
	 * @param array $config
	 */
	public function add_rule( $field, $rule, $error_code = 'error', $config = array() ) {
		$this->rules[] = array(
			'field' => $field,
			'rule' => $rule,
			'error_code' => $error_code,
			'config' => $config
		);
	}

	/**
	 * @return bool
	 * @throws Exception
	 */
	public function validate() {

		foreach( $this->rules as $rule ) {

			$value = isset( $this->fields[ $rule['field'] ] ) ? $this->fields[ $rule['field'] ] : '';

			$method = 'is_' . $rule['rule'];

			if( ! method_exists( $this, $method ) ) {
				throw new Exception( sprintf( 'No validation rule "%s" exists.', $rule['rule'] ) );
			}

			$valid = call_user_func( array( $this, $method ), $value, $rule['config'] );

			if( ! $valid ) {
				if( ! in_array( $rule['error_code'], $this->errors ) ) {
					$this->errors[] = $rule['error_code'];
				}
			}
		}

		return count( $this->errors ) === 0;
	}

	/**
	 * @return array
	 */
	public function get_errors() {
		return $this->errors;
	}

	/**
	 * @param $value
	 *
	 * @return bool
	 */
	public function is_empty( $value ) {
		return empty( $value );
	}

	/**
	 * @param $value
	 *
	 * @return bool
	 */
	public function is_not_empty( $value ) {
		return ! $this->is_empty( $value );
	}

	/**
	 * @param $value
	 * @param $config
	 *
	 * @return false|int
	 */
	public function is_valid_nonce( $value, $config ) {

		// when using caching, assume valid nonce
		if( defined( 'WP_CACHE' ) && WP_CACHE ) {
			return true;
		}

		return wp_verify_nonce( $value, $config['action'] ) !== false;
	}

	/**
	 * @param $value
	 * @param $config
	 *
	 * @return bool
	 */
	public function is_range( $value, $config ) {
		$value = intval( $value );

		if( isset( $config['min'] ) && $value < $config['min'] ) {
			return false;
		}

		if( isset( $config['max'] ) && $value > $config['max'] ) {
			return false;
		}

		return true;
	}

	/**
	 * @param $value
	 *
	 * @return bool
	 */
	public function is_email( $value ) {
		return is_string( $value ) && is_email( $value );
	}

}