1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lneicelis\Transformer\Pipe; |
4
|
|
|
|
5
|
|
|
use Lneicelis\Transformer\Contract\CanPipe; |
6
|
|
|
use Lneicelis\Transformer\Contract\HasLazyProperties; |
7
|
|
|
use Lneicelis\Transformer\Contract\HasSchema; |
8
|
|
|
use Lneicelis\Transformer\Exception\TransformerNotFoundException; |
9
|
|
|
use Lneicelis\Transformer\TransformerRegistry; |
10
|
|
|
use Lneicelis\Transformer\ValueObject\Context; |
11
|
|
|
use Lneicelis\Transformer\ValueObject\Path; |
12
|
|
|
|
13
|
|
|
class LazyPropertiesPipe implements CanPipe |
14
|
|
|
{ |
15
|
|
|
/** @var TransformerRegistry */ |
16
|
|
|
protected $transformerRepository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param TransformerRegistry $transformerRepository |
20
|
|
|
*/ |
21
|
11 |
|
public function __construct(TransformerRegistry $transformerRepository) |
22
|
|
|
{ |
23
|
11 |
|
$this->transformerRepository = $transformerRepository; |
24
|
11 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param object $resource |
28
|
|
|
* @param Context $context |
29
|
|
|
* @param Path $path |
30
|
|
|
* @param $data |
31
|
|
|
* @return array |
32
|
|
|
* @throws TransformerNotFoundException |
33
|
|
|
*/ |
34
|
6 |
|
public function pipe($resource, Context $context, Path $path, $data) |
35
|
|
|
{ |
36
|
6 |
|
if (! $context instanceof HasSchema) { |
|
|
|
|
37
|
|
|
return $data; |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
$transformer = $this->transformerRepository->getTransformer($resource); |
41
|
|
|
|
42
|
6 |
|
if (! $transformer instanceof HasLazyProperties) { |
43
|
4 |
|
return $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
2 |
|
$schema = $this->getPathSchema($path, $context->getSchema()); |
48
|
2 |
|
$properties = $this->getProperties($schema); |
49
|
|
|
|
50
|
2 |
|
return array_reduce( |
51
|
2 |
|
$properties, |
52
|
2 |
|
function (array $data, string $key) use ($resource, $transformer) { |
53
|
2 |
|
$data[$key] = $data[$key] ?? $transformer->{$key}($resource); |
54
|
|
|
|
55
|
2 |
|
return $data; |
56
|
2 |
|
}, |
57
|
2 |
|
$data |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getProperties(array $schema): array |
62
|
|
|
{ |
63
|
2 |
|
return array_map(function ($value, $key) { |
64
|
2 |
|
return is_string($value) ? $value : $key; |
65
|
2 |
|
}, $schema, array_keys($schema)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getPathSchema(Path $path, array $schema): array |
69
|
|
|
{ |
70
|
2 |
|
$segments = array_filter($path->getSegments(), function ($segment) { |
71
|
1 |
|
return ! is_numeric($segment); |
72
|
2 |
|
}); |
73
|
|
|
|
74
|
2 |
|
return $this->arrayGet($schema, $segments, []); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
private function arrayGet(array $array, array $path, $default): array |
78
|
|
|
{ |
79
|
2 |
|
foreach ($path as $key) { |
80
|
1 |
|
if (! array_key_exists($key, $array)) { |
81
|
1 |
|
return $default; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$array = $array[$key]; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $array; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|