<?php

class EditFormValidator extends FormValidator
{
    public function validate(EditForm $form)
    {
        $errors = [];
        $errors = array_merge(parent::validate($form), $errors);
        $errors["accessPass"] = $this->checkAccessPass($form->getAccessPass());
        $errors["delPass"] = $this->checkDelPass($form->getDelPass());
        $errors = parent::filterErrors($errors);
        return $errors;
    }

    public function checkAccessPass($accessPass)
    {
        return $this->checkPass($accessPass);
    }

    public function checkDelPass($delPass)
    {
        return $this->checkPass($delPass);
    }

    public function getHtml5RegexForAccessPass()
    {
        return $this->getHtml5RegexForPass();
    }

    public function getHtml5RegexForDelPass()
    {
        return $this->getHtml5RegexForPass();
    }

    public function getHintForAccessPass()
    {
        return $this->getHintForPass();
    }

    public function getHintForDelPass()
    {
        return $this->getHintForPass();
    }

    private function getHintForPass()
    {
        $hint = "Пароль должен состоять из цифр и латинских букв. " .
            "Длина не должна быть меньше 6 или превышать 20 символов.";
        return $hint;
    }

    private function getHtml5RegexForPass()
    {
        return "^([a-zA-Z\\d]){6,20}$";
    }

    private function checkPass($pass)
    {
        if (is_null($pass) ||
            preg_match("/^([a-z\\d]){6,20}$/ui", $pass)
        ) {
            $status = true;
        } else {
            $status = $this->getHintForPass();
        }
        return $status;
    }
}