<?php
/**
 * Regex Rule File
 *
 * @package NovaPostForWoocommerce\Rakit\Validation\Rules
 */

namespace NovaPostForWoocommerce\Rakit\Validation\Rules;

use NovaPostForWoocommerce\Rakit\Validation\Rule;

/**
 * Regex Rule Class
 *
 * @package NovaPostForWoocommerce\Rakit\Validation\Rules
 */
class Regex extends Rule {

    /**
     * The rule error message
     *
     * @var string
     */
    protected $message = 'The :attribute is not valid format';

    /**
     * The fillable parameters
     *
     * @var array
     */
    protected $fillableParams = array( 'regex' );

    /**
     * Check the $value is valid
     *
     * @param mixed $value The value to check.
     * @return bool
     */
    public function check( $value ): bool {
        $this->requireParameters( $this->fillableParams );
        $regex = $this->parameter( 'regex' );
        return preg_match( $regex, $value ) > 0;
    }
}
