Skip to content

Commit 253e604

Browse files
committed
Allow filter chains to be constructed with instances in the specification
Signed-off-by: George Steel <george@net-glue.co.uk>
1 parent 8ea14c1 commit 253e604

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/FilterChain.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Traversable;
1212

1313
use function count;
14+
use function is_callable;
1415

1516
/**
1617
* @psalm-type InstanceType = FilterInterface|(callable(mixed): mixed)
@@ -19,7 +20,7 @@
1920
* name: string|class-string<FilterInterface>,
2021
* options?: array<string, mixed>,
2122
* priority?: int,
22-
* }>,
23+
* }|InstanceType>,
2324
* callbacks?: list<array{
2425
* callback: FilterInterface|(callable(mixed): mixed),
2526
* priority?: int,
@@ -55,6 +56,11 @@ public function __construct(
5556

5657
$filters = $options['filters'] ?? [];
5758
foreach ($filters as $spec) {
59+
if (is_callable($spec) || $spec instanceof FilterInterface) {
60+
$this->attach($spec);
61+
continue;
62+
}
63+
5864
$this->attachByName(
5965
$spec['name'],
6066
$spec['options'] ?? [],

test/FilterChainTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,17 @@ public function testTheFilterChainIsInvokable(): void
225225

226226
self::assertSame('foo', $chain->__invoke('FOO'));
227227
}
228+
229+
public function testFilterChainSpecAcceptsFilterInstances(): void
230+
{
231+
$filter = new StringTrim();
232+
$chain = new FilterChain($this->plugins, [
233+
'filters' => [
234+
$filter,
235+
],
236+
]);
237+
238+
$filters = iterator_to_array($chain);
239+
self::assertSame([0 => $filter], $filters);
240+
}
228241
}

0 commit comments

Comments
 (0)