<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Password implements Rule
{
    private $length;

    private $message;

    public function __construct($length = 8)
    {
        $this->length = $length;
    }

    public function passes($attribute, $value)
    {
        $rules = [
            '#.{'.$this->length.',}#' => 'boilerplate::password.rules.length',
            '#[a-z]+#' => 'boilerplate::password.rules.letter',
            '#[A-Z]+#' => 'boilerplate::password.rules.capital',
            '#[0-9]+#' => 'boilerplate::password.rules.number',
            '#[^A-Za-z0-9]+#' => 'boilerplate::password.rules.special',
        ];

        foreach ($rules as $rule => $msg) {
            if (! preg_match($rule, $value)) {
                $this->message = __($msg, ['min' => $this->length]);

                return false;
            }
        }

        return true;
    }

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