<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class SafeName implements Rule
{

    public const CHARSET_DEFAULT = '/^[a-zA-Z0-9\s\#\(\)\[\]\-\_]+$/';
    public const CHARSET_NO_HASH = '/^[a-zA-Z0-9\s\(\)\[\]\-\_]+$/';
    public const CHARSET_SIMPLE  = '/^[a-zA-Z0-9_\-\s]+$/';

    private $charset = self::CHARSET_DEFAULT;

    public function __construct(string $_charset = self::CHARSET_DEFAULT)
    {
        $this->charset = $_charset;
    }

    public static function make(string $charset = self::CHARSET_DEFAULT)
    {
        return new static($charset);
    }


    public function passes($attribute, $value)
    {
        if (!is_string($value)) {
            return false;
        }

        return preg_match($this->charset, $value);
    }

    public function message()
    {
        return 'The :attribute must be a valid and safe name.';
    }
}
