forked from flow-php/etl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectToArrayTransformer.php
More file actions
64 lines (53 loc) · 1.68 KB
/
Copy pathObjectToArrayTransformer.php
File metadata and controls
64 lines (53 loc) · 1.68 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
<?php
declare(strict_types=1);
namespace Flow\ETL\Transformer;
use Flow\ETL\Exception\RuntimeException;
use Flow\ETL\Row;
use Flow\ETL\Rows;
use Flow\ETL\Transformer;
use Laminas\Hydrator\HydratorInterface;
/**
* @implements Transformer<array{object_entry_name: string, hydrator: HydratorInterface}>
* @psalm-immutable
*/
final class ObjectToArrayTransformer implements Transformer
{
public function __construct(
private readonly HydratorInterface $hydrator,
private readonly string $objectEntryName
) {
}
public function __serialize() : array
{
return [
'object_entry_name' => $this->objectEntryName,
'hydrator' => $this->hydrator,
];
}
public function __unserialize(array $data) : void
{
$this->objectEntryName = $data['object_entry_name'];
$this->hydrator = $data['hydrator'];
}
public function transform(Rows $rows) : Rows
{
/** @psalm-var pure-callable(Row) : Row $transformer */
$transformer = function (Row $row) : Row {
$entry = $row->entries()->get($this->objectEntryName);
if (!$entry instanceof Row\Entry\ObjectEntry) {
throw new RuntimeException("\"{$this->objectEntryName}\" is not ObjectEntry");
}
$entries = $row->entries()
->set(
new Row\Entry\ArrayEntry(
$this->objectEntryName,
$this->hydrator->extract(
$entry->value()
)
)
);
return new Row($entries);
};
return $rows->map($transformer);
}
}