-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRulesRunner.php
More file actions
82 lines (69 loc) · 2 KB
/
Copy pathRulesRunner.php
File metadata and controls
82 lines (69 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
/*
* This file is part of the univeros/framework
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Altair\Validation;
use Altair\Middleware\Contracts\PayloadInterface;
use Altair\Structure\Queue;
use Altair\Validation\Contracts\ResolverInterface;
use Altair\Validation\Contracts\RuleInterface;
use Altair\Validation\Contracts\RulesRunnerInterface;
use Override;
class RulesRunner implements RulesRunnerInterface
{
/**
* A resolver to convert queue entries to callables.
*/
protected ?ResolverInterface $resolver;
/**
* Constructor.
*
* @param Queue<mixed> $queue The middleware queue.
*/
public function __construct(?ResolverInterface $resolver = null, protected ?Queue $queue = null)
{
$this->resolver = $resolver;
}
/**
* Calls the next entry in the queue.
*
*
*/
#[Override]
public function __invoke(PayloadInterface $payload): PayloadInterface
{
$entry = $this->queue instanceof Queue && !$this->queue->isEmpty() ? $this->queue->pop() : null;
$middleware = $this->resolve($entry);
return $middleware($payload, $this);
}
/**
* @param array<int, mixed> $rules
*/
#[Override]
public function withRules(array $rules): RulesRunnerInterface
{
$this->queue = new Queue($rules);
return $this;
}
/**
* Converts a queue entry to a callable, using the resolver if present.
*
* @param mixed|callable|RuleInterface $entry the queue entry.
*
* @return callable|RuleInterface
*/
protected function resolve($entry)
{
if (!$entry) {
return fn(PayloadInterface $payload, callable $next): PayloadInterface => $payload;
}
if (!$this->resolver instanceof ResolverInterface) {
return $entry;
}
return \call_user_func($this->resolver, $entry);
}
}