<?php

namespace Application\Service\Extension\Validator;


use Laminas\Validator\Exception\InvalidArgumentException;
use Laminas\Validator\NotEmpty;
use Laminas\Validator\Regex;
use Laminas\Validator\StringLength;

class FormRules extends CustomValidator
{
    public array $validateRules  = [
        'required' => [
            'class' => NotEmpty::class,
            'message' => "{attribute}是必填的"
        ],
        'mobile' => [
            'class' => Regex::class,
            "message" => "{attribute}不是正确的手机号码",
            'options' => [
                'pattern' => '/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/'
            ]
        ],
        'maxLength' => [
            'class' => StringLength::class,
            'message' => "{attribute}的长度不能大于{compareValue}"
        ],
        'minLength' => [
            'class' => StringLength::class,
            'message' => "{attribute}的长度不能小于{compareValue}"
        ]
    ];

    protected array $formRules = [];

    protected $formId;

    public function __construct($options = null)
    {
        $this->formId = $options['id'];
        unset($options['id']);
        $this->setFormRules();
        parent::__construct($options);
    }

    public function getFormRules()
    {
        return $this->formRules;
    }

    public function setFormRules()
    {
        $this->formRules = [
            'mobile' => [
                'rules' => [
                    [
                        'name' => 'required',
                        'value' => 1,
                    ],
                    [
                        'name' => 'mobile',
                        'value' => 1,
                    ]
                ]
            ],
            'age' => [
                'rules' => [
                    [
                        'name' => 'required',
                        'value' => 1,
                    ]
                ],
            ],
            'name' => [
                'rules' => [
                    [
                        'name' => 'required',
                        'value' => 1,
                    ],
                    [
                        'name' => 'maxLength',
                        'value' => 6,
                    ],
                    [
                        'name' => 'minLength',
                        'value' => 2,
                    ]
                ],
            ]
        ];
    }


    public function isValid($value, $attribute = null)
    {
        $validateFormRules = $this->formRules[$attribute]['rules'] ?? [];
        foreach ($validateFormRules as $k => $rule) {
            $validateRules = $this->getValidateRulesByName($rule);
            $validator = new $validateRules['class']($validateRules['options'] ?? []);
            if (!$validator->isValid($value)) {
                throw new InvalidArgumentException(strtr($validateRules['message'], [
                    '{value}' => $value,
                    '{attribute}' => $attribute,
                    '{compareValue}' => $rule['value']
                ]));
            }

        }
        return true;
    }

    /**
     * @param array $rule
     * @return mixed
     * @throws \Exception
     */
    public function getValidateRulesByName(array $rule): array
    {
        $ruleName = $rule['name'];
        if (!isset($this->validateRules[$ruleName])) {
            throw new \Exception("找不到验证规则{$rule['name']}对应的验证类");
        }

        if ($ruleName == 'maxLength') {
            $this->validateRules[$ruleName]['options'] = [
                'max' => $rule['value']
            ];
        }elseif ($rule['name'] == 'minLength') {
            $this->validateRules[$ruleName]['options']  = [
                'min' => $rule['value']
            ];
        }

        return $this->validateRules[$rule['name']];
    }
}