-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCollectionWriter.php
More file actions
46 lines (37 loc) · 1.04 KB
/
CollectionWriter.php
File metadata and controls
46 lines (37 loc) · 1.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
<?php
declare(strict_types=1);
namespace CodeRhapsodie\DataflowBundle\DataflowType\Writer;
use CodeRhapsodie\DataflowBundle\Exceptions\UnsupportedItemTypeException;
/**
* Delegates the writing of each item in a collection to an embedded writer.
*/
class CollectionWriter implements DelegateWriterInterface
{
/**
* CollectionWriter constructor.
*/
public function __construct(private readonly WriterInterface $writer)
{
}
public function prepare(): void
{
$this->writer->prepare();
}
public function write($collection): void
{
if (!is_iterable($collection)) {
throw new UnsupportedItemTypeException(\sprintf('Item to write was expected to be an iterable, received %s.', get_debug_type($collection)));
}
foreach ($collection as $item) {
$this->writer->write($item);
}
}
public function finish(): void
{
$this->writer->finish();
}
public function supports($item): bool
{
return is_iterable($item);
}
}