# give/4.15.1/vendor/vendor-prefixed/stellarwp/validation/src/ValidationRulesRegistrar.php

GiveWP – Donation Plugin and Fundraising Platform, version 4.15.1. 75 lines.

- Page: https://pluginprobe.com/plugins/give/4.15.1/code/vendor/vendor-prefixed/stellarwp/validation/src/ValidationRulesRegistrar.php
- Raw: https://pluginprobe.com/plugins/give/4.15.1/raw/vendor/vendor-prefixed/stellarwp/validation/src/ValidationRulesRegistrar.php
- Modified: 2025-07-09T13:09:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/give/4.15.1/code/vendor/vendor-prefixed/stellarwp/validation/src/ValidationRulesRegistrar.php#L10-L20`.

```php
<?php

declare(strict_types=1);

namespace Give\Vendors\StellarWP\Validation;

use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule;

/**
 * @since 1.0.0
 */
class ValidationRulesRegistrar
{
    /**
     * @var array<string, class-string<ValidationRule>>
     */
    protected array $rules = [];

    /**
     * Register one or many validation rules.
     *
     * @since 1.0.0
     */
    public function register(string ...$rules): self
    {
        foreach ($rules as $rule) {
            $this->registerClass($rule);
        }

        return $this;
    }

    /**
     * Register a validation rule.
     *
     * @since 1.2.1 switch to throwing InvalidArgumentException from Config
     * @since 1.0.0
     */
    private function registerClass(string $class): self
    {
        if (!is_subclass_of($class, ValidationRule::class)) {
            Config::throwInvalidArgumentException(
                sprintf(
                    'Validation rule must implement %s',
                    ValidationRule::class
                )
            );
        }

        $classId = $class::id();

        if (isset($this->rules[$classId])) {
            Config::throwInvalidArgumentException(
                "A validation rule with the id $classId has already been registered."
            );
        }

        $this->rules[$classId] = $class;

        return $this;
    }

    /**
     * Get a validation rule.
     *
     * @return string|null
     * @since 2.12.0
     *
     */
    public function getRule(string $ruleId)
    {
        return $this->rules[$ruleId] ?? null;
    }
}

```
