mallardduck/extended-validator-laravel

View on GitHub
src/Rules/BaseRule.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace MallardDuck\ExtendedValidator\Rules;

use Illuminate\Support\Str;

abstract class BaseRule
{
    protected string $name;

    /**
     * @var callable
     */
    protected $callback;

    protected string $message;

    /**
     * @var callable|null
     */
    protected $replacer = null;

    public function __construct(callable $callback, string $message, ?callable $replacer = null)
    {
        $this->name = $this->getImplicitRuleName();
        $this->callback = $callback;
        $this->message = $message;
        $this->replacer = $replacer;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getCallback(): callable
    {
        return $this->callback;
    }

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

    public function hasReplacer(): bool
    {
        return is_callable($this->replacer);
    }

    public function getReplacer(): ?callable
    {
        return $this->replacer;
    }

    protected function getImplicitRuleName(): string
    {
        return (string) Str::of(static::class)
            ->after('Rules\\')
            ->snake();
    }
}