1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lneicelis\Transformer; |
4
|
|
|
|
5
|
|
|
use Lneicelis\Transformer\Exception\TransformerNotFoundException; |
6
|
|
|
use Lneicelis\Transformer\Helper\Arr; |
7
|
|
|
use Lneicelis\Transformer\ValueObject\Context; |
8
|
|
|
use Lneicelis\Transformer\ValueObject\Deferred; |
9
|
|
|
use Lneicelis\Transformer\ValueObject\Path; |
10
|
|
|
|
11
|
|
|
class DeferredAwareTransformer extends Transformer |
12
|
|
|
{ |
13
|
|
|
/** @var array[] */ |
14
|
|
|
private $deferredTree = []; |
15
|
|
|
|
16
|
|
|
/** @var LoaderRegistry */ |
17
|
|
|
private $loaderRegistry; |
18
|
|
|
|
19
|
|
|
public function __construct(array $pipes, LoaderRegistry $loaderRegistry) |
20
|
2 |
|
{ |
21
|
|
|
parent::__construct($pipes); |
22
|
2 |
|
|
23
|
2 |
|
$this->loaderRegistry = $loaderRegistry; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed $resource |
28
|
|
|
* @param Context|null $context |
29
|
|
|
* @return string|int|float|array |
30
|
|
|
* @throws TransformerNotFoundException |
31
|
2 |
|
*/ |
32
|
|
|
public function transform($resource, Context $context = null) |
33
|
2 |
|
{ |
34
|
|
|
$context = $context ?: new Context(); |
35
|
2 |
|
|
36
|
2 |
|
$result = parent::transform($resource, $context); |
37
|
|
|
$result = $this->loadDeferred($result, $context); |
|
|
|
|
38
|
2 |
|
|
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $deferred |
44
|
|
|
* @param Context $context |
45
|
|
|
* @param Path $path |
46
|
|
|
* @return array|float|int|string |
47
|
|
|
* @throws TransformerNotFoundException |
48
|
|
|
*/ |
49
|
2 |
|
protected function transformAny($deferred, Context $context, Path $path) |
50
|
|
|
{ |
51
|
2 |
|
if (! $deferred instanceof Deferred) { |
52
|
2 |
|
return parent::transformAny($deferred, $context, $path); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$resource = $deferred->getResource(); |
56
|
2 |
|
$property = $deferred->getProperty(); |
57
|
2 |
|
$resourceClass = get_class($resource); |
58
|
|
|
|
59
|
2 |
|
$this->deferredTree = Arr::setValue($this->deferredTree, [$resourceClass, $property, (string)$path], $deferred); |
60
|
|
|
|
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $result |
66
|
|
|
* @param Context $context |
67
|
|
|
* @return array |
68
|
2 |
|
* @throws TransformerNotFoundException |
69
|
|
|
*/ |
70
|
2 |
|
protected function loadDeferred(array $result, Context $context): array |
71
|
2 |
|
{ |
72
|
2 |
|
foreach ($this->deferredTree as $resourceClass => $properties) { |
73
|
|
|
foreach ($properties as $property => $deferredByPath) { |
74
|
2 |
|
$loader = $this->loaderRegistry->getLoader($resourceClass, $property); |
75
|
|
|
$paths = array_keys($deferredByPath); |
76
|
2 |
|
$deferreds = array_values($deferredByPath); |
77
|
|
|
|
78
|
2 |
|
unset($this->deferredTree[$resourceClass][$property]); |
79
|
|
|
|
80
|
2 |
|
$loadedValues = $loader->load($deferreds); |
81
|
|
|
$transformedValues = $this->transform($loadedValues, $context); |
82
|
2 |
|
|
83
|
2 |
|
foreach ($paths as $index => $path) { |
84
|
2 |
|
/** @var Path $path */ |
85
|
2 |
|
$result = Arr::setValue( |
86
|
|
|
$result, |
87
|
|
|
explode('.', $path), |
88
|
|
|
$transformedValues[$index] |
89
|
|
|
); |
90
|
2 |
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
2 |
|
|
94
|
|
|
return $result; |
95
|
2 |
|
} |
96
|
|
|
} |
97
|
|
|
|