Skip to content

Commit 44348d1

Browse files
committed
Use iterator when using the tagged service shortcut
1 parent 070c50c commit 44348d1

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/Transformer/Chain.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ class Chain implements ExceptionTransformerInterface
1212
/**
1313
* @var ExceptionTransformerInterface[]
1414
*/
15-
private $transformers;
15+
private $transformers = [];
1616

17-
public function __construct(ExceptionTransformerInterface ...$transformers)
17+
public function __construct(iterable $transformers)
1818
{
19-
$this->transformers = $transformers;
19+
foreach ($transformers as $transformer) {
20+
$this->addTransformer($transformer);
21+
}
2022
}
2123

2224
public function transform(\Throwable $exception): ApiProblemInterface
@@ -34,4 +36,9 @@ public function accepts(\Throwable $exception): bool
3436
{
3537
return true;
3638
}
39+
40+
private function addTransformer(ExceptionTransformerInterface $transformer): void
41+
{
42+
$this->transformers[] = $transformer;
43+
}
3744
}

test/Transformer/ChainTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ class ChainTest extends TestCase
2020
/** @test */
2121
public function it_is_an_exception_transformer(): void
2222
{
23-
$transformer = new Chain();
23+
$transformer = new Chain([]);
2424
$this->assertInstanceOf(ExceptionTransformerInterface::class, $transformer);
2525
}
2626

2727
/** @test */
2828
public function it_accepts_any_exception(): void
2929
{
30-
$transformer = new Chain();
30+
$transformer = new Chain([]);
3131
$this->assertTrue($transformer->accepts(new \Exception()));
3232
}
3333

3434
/** @test */
3535
public function it_transforms_with_first_acceptable_transformer(): void
3636
{
37-
$transformer = new Chain(
37+
$transformer = new Chain([
3838
$this->mockTransformer(false),
3939
$this->mockTransformer(true, $apiProblem1 = $this->prophesize(ApiProblemInterface::class)->reveal()),
40-
$this->mockTransformer(true, $apiProblem2 = $this->prophesize(ApiProblemInterface::class)->reveal())
41-
);
40+
$this->mockTransformer(true, $apiProblem2 = $this->prophesize(ApiProblemInterface::class)->reveal()),
41+
]);
4242

4343
$this->assertEquals($apiProblem1, $transformer->transform(new \Exception()));
4444
}
4545

4646
/** @test */
4747
public function it_transforms_to_basic_exception_problem_when_no_transformer_matches(): void
4848
{
49-
$transformer = new Chain($this->mockTransformer(false));
49+
$transformer = new Chain([$this->mockTransformer(false)]);
5050

5151
$this->assertInstanceOf(ExceptionApiProblem::class, $transformer->transform(new \Exception()));
5252
}

0 commit comments

Comments
 (0)