1
|
|
|
<?php |
2
|
|
|
namespace Solvire\API\Serializers; |
3
|
|
|
|
4
|
|
|
use Solvire\API\Serializers\DataFields\DataField; |
5
|
|
|
use Solvire\API\Serializers\DataFields\DataFieldCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* @see README.md |
10
|
|
|
* @author solvire <[email protected]> |
11
|
|
|
* @package Serializers |
12
|
|
|
* @namespace Solvire\API\Serializers |
13
|
|
|
*/ |
14
|
|
|
abstract class BaseSerializer implements \JsonSerializable |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* borrowed |
19
|
|
|
* |
20
|
|
|
* @var unknown |
21
|
|
|
*/ |
22
|
|
|
protected $arguments = [ |
23
|
|
|
'read_only', |
24
|
|
|
'write_only', |
25
|
|
|
'required', |
26
|
|
|
'default', |
27
|
|
|
'initial', |
28
|
|
|
'source', |
29
|
|
|
'label', |
30
|
|
|
'help_text', |
31
|
|
|
'style', |
32
|
|
|
'error_messages', |
33
|
|
|
'allow_empty', |
34
|
|
|
'instance', |
35
|
|
|
'data', |
36
|
|
|
'partial', |
37
|
|
|
'context', |
38
|
|
|
'allow_null' |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* available methods |
43
|
|
|
* based on the serializer type |
44
|
|
|
* should not be settable except from inside the serializer |
45
|
|
|
* |
46
|
|
|
* @var unknown |
47
|
|
|
*/ |
48
|
|
|
protected $methods = []; |
49
|
|
|
|
50
|
|
|
protected $fillable = []; |
51
|
|
|
|
52
|
|
|
protected $initialData = []; |
53
|
|
|
|
54
|
|
|
protected $data = []; |
55
|
|
|
|
56
|
|
|
protected $requiredOptions = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This is pretty important |
60
|
|
|
* It is going to hold a list of all the fields and their types. |
61
|
|
|
* Each one has to get loaded up by factory. |
62
|
|
|
* |
63
|
|
|
* Collection of DataFields mapped to field names |
64
|
|
|
*/ |
65
|
|
|
protected $dataFields = null; |
66
|
|
|
|
67
|
9 |
|
public function __construct($initialData = []) |
68
|
|
|
{ |
69
|
9 |
|
$this->dataFields = new DataFieldCollection(); |
70
|
9 |
|
$this->setInitialData($initialData); |
71
|
9 |
|
$this->initDataFields(); |
72
|
9 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* this is where your definitions will go. |
76
|
|
|
*/ |
77
|
|
|
abstract public function initDataFields(); |
78
|
|
|
|
79
|
9 |
|
public function setInitialData($initialData) |
80
|
|
|
{ |
81
|
9 |
|
$this->initialData = $initialData; |
82
|
9 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
public function addField($name, DataField $field) |
86
|
|
|
{ |
87
|
9 |
|
$field->setName($name); |
88
|
9 |
|
$this->dataFields->set($name, $field); |
89
|
9 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
public function getField($name) |
93
|
|
|
{ |
94
|
4 |
|
return $this->dataFields->get($name)->get(); |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
public function getDataFieldCollection() |
98
|
|
|
{ |
99
|
4 |
|
return $this->dataFields; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* not the same as fill data - it should prob be depricated |
104
|
|
|
* |
105
|
|
|
* @param unknown $data |
106
|
|
|
*/ |
107
|
|
|
abstract public function loadData($data); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @throws \RuntimeException |
112
|
|
|
*/ |
113
|
1 |
|
public function create() |
114
|
|
|
{ |
115
|
1 |
|
throw new \RuntimeException('not implemented'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* @throws \RuntimeException |
121
|
|
|
*/ |
122
|
1 |
|
public function update() |
123
|
|
|
{ |
124
|
1 |
|
throw new \RuntimeException('not implemented'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* should get implemented at the concrete levels |
129
|
|
|
*/ |
130
|
1 |
|
public function save() |
131
|
|
|
{ |
132
|
1 |
|
throw new \RuntimeException('not implemented'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* (non-PHPdoc) |
137
|
|
|
* |
138
|
|
|
* @see JsonSerializable::jsonSerialize() |
139
|
|
|
*/ |
140
|
|
|
abstract public function jsonSerialize(); |
141
|
|
|
} |
142
|
|
|
|