1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Transform |
5
|
|
|
* @author Flipbox Factory |
6
|
|
|
* @copyright Copyright (c) 2017, Flipbox Digital |
7
|
|
|
* @link https://github.com/flipbox/transform/releases/latest |
8
|
|
|
* @license https://github.com/flipbox/transform/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace flipbox\transform\transformers; |
12
|
|
|
|
13
|
|
|
use flipbox\transform\resources\ResourceInterface; |
14
|
|
|
use flipbox\transform\Scope; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @package flipbox\transform\transformers |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractResourceTransformer extends AbstractTransformer implements ResourceTransformerInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
protected $data; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var callable|TransformerInterface |
31
|
|
|
*/ |
32
|
|
|
protected $transformer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Scope $scope |
36
|
|
|
* @return ResourceInterface |
37
|
|
|
*/ |
38
|
|
|
protected abstract function createResource(Scope $scope): ResourceInterface; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Scope $scope |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
protected function getData(Scope $scope) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
return $this->data; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Scope $scope |
51
|
|
|
* @param string|null $identifier |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function transform(Scope $scope, string $identifier = null) |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
$childScope = $scope->childScope($identifier); |
58
|
|
|
|
59
|
|
|
$resource = $this->createResource( |
60
|
|
|
$childScope |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return $resource->transform( |
64
|
|
|
$this->transformer, |
65
|
|
|
$this->getData( |
66
|
|
|
$childScope |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param callable $transformer |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setTransformer(callable $transformer) |
77
|
|
|
{ |
78
|
|
|
$this->transformer = $transformer; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed $data |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setData($data) |
87
|
|
|
{ |
88
|
|
|
$this->data = $data; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
|
|
public function __invoke($data, Scope $scope, string $identifier = null) |
96
|
|
|
{ |
97
|
|
|
return $this->transform($scope, $identifier); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |