1
0
mirror of https://github.com/flarum/core.git synced 2025-08-23 00:23:25 +02:00

Validator extender (#2102)

Added validator extender, integration tests, and deprecated related Validating event
This commit is contained in:
Alexander Skvortsov
2020-11-01 11:31:16 -05:00
committed by GitHub
parent b311512502
commit 5842dd1200
4 changed files with 173 additions and 0 deletions

58
src/Extend/Validator.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
class Validator implements ExtenderInterface
{
private $configurationCallbacks = [];
private $validator;
/**
* @param string $validatorClass: The ::class attribute of the validator you are modifying.
* The validator should inherit from \Flarum\Foundation\AbstractValidator.
*/
public function __construct($validatorClass)
{
$this->validator = $validatorClass;
}
/**
* Configure the validator. This is often used to adjust validation rules, but can be
* used to make other changes to the validator as well.
*
* @param callable $callable
*
* The callable can be a closure or invokable class, and should accept:
* - \Flarum\Foundation\AbstractValidator $flarumValidator: The Flarum validator wrapper
* - \Illuminate\Validation\Validator $validator: The Laravel validator instance
*/
public function configure($callback)
{
$this->configurationCallbacks[] = $callback;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$container->resolving($this->validator, function ($validator, $container) {
foreach ($this->configurationCallbacks as $callback) {
if (is_string($callback)) {
$callback = $container->make($callback);
}
$validator->addConfiguration($callback);
}
});
}
}