1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YucaDoo\PolymorphicFractal; |
6
|
|
|
|
7
|
|
|
use Mouf\AliasContainer\AliasContainer; |
8
|
|
|
use League\Fractal\TransformerAbstract; |
9
|
|
|
use League\Fractal\Scope; |
10
|
|
|
|
11
|
|
|
class Transformer extends TransformerAbstract |
12
|
|
|
{ |
13
|
|
|
/** @var AliasContainer */ |
14
|
|
|
private $registry; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tranformer based on current data. |
18
|
|
|
* @var TransformerAbstract |
19
|
|
|
*/ |
20
|
|
|
private $currentTransformer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* @param AliasContainer $registry Registry providing specific transformers. |
25
|
|
|
*/ |
26
|
39 |
|
public function __construct(AliasContainer $registry) |
27
|
|
|
{ |
28
|
39 |
|
$this->registry = $registry; |
29
|
39 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Getter for registry. |
33
|
|
|
* @return AliasContainer |
34
|
|
|
*/ |
35
|
39 |
|
public function getRegistry(): AliasContainer |
36
|
|
|
{ |
37
|
39 |
|
return $this->registry; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Getter for currentTransformer. |
42
|
|
|
* @return TransformerAbstract Current transformer. |
43
|
|
|
*/ |
44
|
39 |
|
public function getCurrentTransformer() |
45
|
|
|
{ |
46
|
39 |
|
return $this->currentTransformer; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets currentTransformer based on transformation data. |
51
|
|
|
* @param mixed $data Transformation data based on which current transformer is set. |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
39 |
|
protected function setCurrentTransformer($data) |
55
|
|
|
{ |
56
|
|
|
// Transformer to be used |
57
|
39 |
|
$registryKey = $this->getRegistryKey($data); |
58
|
39 |
|
$this->currentTransformer = $this->registry->get($registryKey); |
59
|
|
|
// Apply current scope to current tranformer as well. |
60
|
39 |
|
$this->currentTransformer->setCurrentScope($this->getCurrentScope()); |
61
|
39 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get registry key from data. |
66
|
|
|
* @param mixed $data Data for which transformer should be got. |
67
|
|
|
* @return mixed Registry key for transformer. |
68
|
|
|
*/ |
69
|
30 |
|
protected function getRegistryKey($data) |
70
|
|
|
{ |
71
|
30 |
|
return get_class($data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
39 |
|
public function getAvailableIncludes() |
78
|
|
|
{ |
79
|
39 |
|
return array_merge( |
80
|
26 |
|
parent::getAvailableIncludes(), |
81
|
39 |
|
$this->getCurrentTransformer()->getAvailableIncludes() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
39 |
|
public function getDefaultIncludes() |
89
|
|
|
{ |
90
|
39 |
|
return array_merge( |
91
|
26 |
|
parent::getDefaultIncludes(), |
92
|
39 |
|
$this->getCurrentTransformer()->getDefaultIncludes() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
39 |
|
public function transform($data) |
100
|
|
|
{ |
101
|
39 |
|
$this->setCurrentTransformer($data); |
102
|
|
|
// The typehint and variable below prevent a PHP Stan linting error. |
103
|
|
|
/** @var mixed */ |
104
|
39 |
|
$currentTransformer = $this->getCurrentTransformer(); |
105
|
39 |
|
return $currentTransformer->transform($data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
21 |
|
protected function callIncludeMethod(Scope $scope, $includeName, $data) |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
// Try to invoke include from this transformer |
115
|
21 |
|
return parent::callIncludeMethod(...func_get_args()); |
116
|
21 |
|
} catch (\Throwable $e) { |
117
|
|
|
// Include method doesn't exist in this class, forward include to current transformer |
118
|
21 |
|
return $this->getCurrentTransformer()->callIncludeMethod(...func_get_args()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|