<?php

namespace Application\Service\Extension\Validator\customer;

use Application\Service\Extension\Validator\BaseCustomValidator;
use Application\Service\Extension\Validator\CustomerValidatorInterface;
use Laminas\Validator\NotEmpty;

class RequiredValidator extends BaseCustomValidator implements CustomerValidatorInterface
{
    /**
     * 严格模式
     *  - 严格模式中, 传值了就算非空
     *  - 非严格模式中, 验证器将会验证属性的值是否为false
     * @var bool
     */
    protected bool $strict = false;

    /**
     * 定义required的值, 在requiredValue值内的将视为空.
     * @var array
     */
    protected array $requiredValue = [];

    public array $skipRequiredValue = [];

    protected $message;

    public function __construct(array $config = [])
    {
        parent::__construct($config);
    }

    public function isValid($value, $attribute)
    {
        if ($this->skipRequiredValue !== [] && in_array($value, $this->skipRequiredValue, $this->strict)) {
            return null;
        }elseif ($this->requiredValue === []) {
            if ($this->strict && $value !== null || !$this->strict && !$this->isEmpty(is_string($value) ? trim($value) : $value)) {
                return null;
            }
        } elseif (!in_array($value, $this->requiredValue, $this->strict)) {
            return null;
        }

        return $this->formatMessage('{attribute} 不能为空.', [
            'attribute' => $this->getAttributeLabel($attribute)
        ]);
    }

    public function isEmpty($value)
    {
        return $value === null || $value === [] || $value === '';
    }
}