1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Incoming |
4
|
|
|
* |
5
|
|
|
* @author Trevor Suarez (Rican7) |
6
|
|
|
* @copyright (c) Trevor Suarez |
7
|
|
|
* @link https://github.com/Rican7/incoming |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Incoming\Transformer; |
14
|
|
|
|
15
|
|
|
use Incoming\Structure\RecursiveInputStructureFactory; |
16
|
|
|
use Incoming\Structure\Structure; |
17
|
|
|
use Incoming\Structure\StructureFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A transformer that takes an input data and returns a Structure |
21
|
|
|
* representation of the same data. |
22
|
|
|
* |
23
|
|
|
* Allows for turning loose input into a well-structured container for easier, |
24
|
|
|
* less error-prone, and more user-friendly data handling. |
25
|
|
|
*/ |
26
|
|
|
class StructureBuilderTransformer implements Transformer |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Properties |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The factory used to build the Structure instances. |
35
|
|
|
* |
36
|
|
|
* @var StructureFactory |
37
|
|
|
*/ |
38
|
|
|
private $structure_factory; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Methods |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param StructureFactory|null $structure_factory The structure factory. |
49
|
|
|
*/ |
50
|
60 |
|
public function __construct(StructureFactory $structure_factory = null) |
51
|
|
|
{ |
52
|
60 |
|
$this->structure_factory = $structure_factory ?: new RecursiveInputStructureFactory(); |
53
|
60 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the structure factory. |
57
|
|
|
* |
58
|
|
|
* @return StructureFactory The structure factory. |
59
|
|
|
*/ |
60
|
3 |
|
public function getStructureFactory(): StructureFactory |
61
|
|
|
{ |
62
|
3 |
|
return $this->structure_factory; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the structure factory. |
67
|
|
|
* |
68
|
|
|
* @param StructureFactory $structure_factory The structure factory. |
69
|
|
|
* @return $this This instance. |
70
|
|
|
*/ |
71
|
3 |
|
public function setStructureFactory(StructureFactory $structure_factory): self |
72
|
|
|
{ |
73
|
3 |
|
$this->structure_factory = $structure_factory; |
74
|
|
|
|
75
|
3 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @param mixed $input The data to transform. |
82
|
|
|
* @return Structure The transformed data. |
83
|
|
|
*/ |
84
|
42 |
|
public function transform($input): Structure |
85
|
|
|
{ |
86
|
42 |
|
return $this->structure_factory->build($input); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|