1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Flipbox Factory |
5
|
|
|
* @copyright Copyright (c) 2017, Flipbox Digital |
6
|
|
|
* @link https://github.com/flipbox/transform/releases/latest |
7
|
|
|
* @license https://github.com/flipbox/transform/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Flipbox\Transform\Transformers; |
11
|
|
|
|
12
|
|
|
use Flipbox\Transform\Helpers\ObjectHelper; |
13
|
|
|
use Flipbox\Transform\Helpers\TransformerHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractExplicitTransformer implements TransformerInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The normalized includes |
23
|
|
|
* |
24
|
|
|
* @var null|array |
25
|
|
|
*/ |
26
|
|
|
private $includes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $config = []) |
32
|
|
|
{ |
33
|
|
|
ObjectHelper::configure($this, $config); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
protected function defineIncludes(): array |
40
|
|
|
{ |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns an array of normalized includes. It is recommend |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getIncludes(): array |
49
|
|
|
{ |
50
|
|
|
if ($this->includes === null) { |
51
|
|
|
$this->includes = TransformerHelper::normalizeIncludes( |
52
|
|
|
$this->defineIncludes() |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->includes; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $data |
61
|
|
|
* @param TransformerInterface|callable $transformer |
62
|
|
|
* @return ExplicitItem |
63
|
|
|
*/ |
64
|
|
|
protected function item($data, $transformer): ExplicitItem |
65
|
|
|
{ |
66
|
|
|
return new ExplicitItem(['data' => $data, 'transformer' => $transformer]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $data |
71
|
|
|
* @param TransformerInterface|callable $transformer |
72
|
|
|
* @return ExplicitCollection |
73
|
|
|
*/ |
74
|
|
|
protected function collection($data, $transformer): ExplicitCollection |
75
|
|
|
{ |
76
|
|
|
return new ExplicitCollection(['data' => $data, 'transformer' => $transformer]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|