<?php

namespace ModulesGarden\Servers\OpenXchange\App\Validators;


use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Validators\BaseValidator;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Validators\boolen;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 10.10.19
 * Time: 11:20
 * Class PasswordsValidator
 */
class PasswordsValidator extends BaseValidator
{


    /**
     * return true if data is valid, false if not,
     * add error messages to $errorsList
     *
     * @param $data mixed
     * @param $additionalData mixed
     * @return boolen
     */
    protected function validate($data, $additionalData = null)
    {
        if ($data == '')
        {
            return true;
        }
        else
        {
            if(strlen($data) < 8)
            {
                $this->addValidationError('passwordCharsLengthError');
                return false;
            }
            elseif(!preg_match('/^(?=.*\d)/', $data))
            {
                $this->addValidationError('passwordCharsLengthError');
                return false;
            }
            elseif(!preg_match("#[A-Z]+#", $data))
            {
                $this->addValidationError('passwordCharsLengthError');
                return false;
            }
        }

        return true;
    }
}
