<?php

namespace Application\Service\Extension\ValidatorV2\validators;

class MatchValidator extends BaseValidator
{
    public string $regex = '';

    public function regex(string $regex = ''): MatchValidator
    {
        $this->regex = $regex;
        return $this;
    }

    public function valid($value, $attribute)
    {
        if (!preg_match($this->regex, $value)) {
            $this->error = [$this->message ?: '{attribute} is invalid.', [
                'attribute' => $attribute
            ]];
            return null;
        }
    }
}