-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiltersRunner.php
More file actions
84 lines (72 loc) · 2.04 KB
/
Copy pathFiltersRunner.php
File metadata and controls
84 lines (72 loc) · 2.04 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
83
84
<?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\Sanitation;
use Altair\Middleware\Contracts\PayloadInterface;
use Altair\Sanitation\Contracts\FilterInterface;
use Altair\Sanitation\Contracts\FiltersRunnerInterface;
use Altair\Sanitation\Contracts\ResolverInterface;
use Altair\Structure\Queue;
use Override;
class FiltersRunner implements FiltersRunnerInterface
{
/**
*
* A resolver to convert queue entries to callables.
*
*/
protected ?ResolverInterface $resolver;
/**
*
* Constructor.
*
* @param Queue<mixed>|null $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() ? null : $this->queue->pop();
$middleware = $this->resolve($entry);
return $middleware($payload, $this);
}
/**
* @param array<int, mixed> $filters
*/
#[Override]
public function withFilters(array $filters): FiltersRunnerInterface
{
$this->queue = new Queue($filters);
return $this;
}
/**
* Converts a queue entry to a callable, using the resolver if present.
*
* @param mixed|callable|FilterInterface $entry the queue entry.
*
* @return callable|FilterInterface
*/
protected function resolve($entry)
{
if (!$entry) {
return static fn(PayloadInterface $payload): PayloadInterface => $payload;
}
if (!$this->resolver instanceof ResolverInterface) {
return $entry;
}
return \call_user_func($this->resolver, $entry);
}
}