1
|
|
|
<?php |
2
|
|
|
namespace Solvire\API\Serializers; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
use Solvire\API\Serializers\DataFields\SplitPointField; |
6
|
|
|
use Solvire\API\Serializers\DataFields\SerializerField; |
7
|
|
|
use Solvire\API\Serializers\DataFields\TransformerField; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Map a Laravel 5.x model to a serializer |
11
|
|
|
* |
12
|
|
|
* @author solvire <[email protected]> |
13
|
|
|
* @package Serializers |
14
|
|
|
* @namespace Solvire\API\Serializers |
15
|
|
|
*/ |
16
|
|
|
abstract class LaravelModelSerializer extends BaseSerializer |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @var Model |
22
|
|
|
*/ |
23
|
|
|
protected $model = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @param Model $model |
28
|
|
|
* @return \Solvire\API\Serializers\LaravelModelSerializer |
29
|
|
|
*/ |
30
|
1 |
|
public function setModel(Model $model) |
31
|
|
|
{ |
32
|
1 |
|
$this->model = $model; |
33
|
1 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @return Model |
39
|
|
|
*/ |
40
|
1 |
|
public function getModel() |
41
|
|
|
{ |
42
|
1 |
|
return $this->model; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function jsonSerialize() |
46
|
|
|
{ |
47
|
1 |
|
return $this->toArray(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* is update = when updating the database |
52
|
|
|
* |
53
|
|
|
* @param string $isUpdate |
54
|
|
|
* @return multitype:NULL |
|
|
|
|
55
|
|
|
*/ |
56
|
1 |
|
public function toArray($isUpdate = false) |
|
|
|
|
57
|
|
|
{ |
58
|
1 |
|
$ret = []; |
59
|
1 |
|
$dfc = $this->getDataFieldCollection(); |
60
|
1 |
|
foreach ($dfc as $name => $dataField) { |
61
|
|
|
|
62
|
|
|
// write only fields should not be displayed |
63
|
1 |
|
if($dataField->writeOnly()) |
64
|
1 |
|
continue; |
65
|
|
|
|
66
|
1 |
|
$ret[$name] = $dataField->getData(); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
return $ret; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @param array $data |
|
|
|
|
75
|
|
|
*/ |
76
|
1 |
|
public function loadData($model) |
77
|
|
|
{ |
78
|
|
|
|
79
|
|
|
// make sure that this is a laravel model |
80
|
1 |
|
if (! $model instanceof Model) |
81
|
1 |
|
throw new \RuntimeException("the object must be of type Illuminate\Database\Eloquent\Model "); |
82
|
|
|
|
83
|
|
|
// loop through all the set fields in the collection |
84
|
|
|
// if they have a matching name then load them up from the model |
85
|
|
|
// if they have a matching columnName then find the value and load that up instead |
86
|
1 |
|
$dfc = $this->getDataFieldCollection(); |
87
|
1 |
|
foreach ($dfc as $name => $dataField) { |
88
|
1 |
|
$col = $dataField->columnName(); |
89
|
|
|
|
90
|
|
|
// i don't think laravel has a point type data field |
91
|
|
|
// we need to check to see if it's a point field split and combine it |
92
|
1 |
|
if ($dataField instanceof SplitPointField) { |
93
|
1 |
|
$this->loadSplitPoint($dataField, $model); |
94
|
1 |
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
// if this is a child serializer object then pass it down |
99
|
|
|
// we assume that since we are in laravel environment that we will have subs of the same |
100
|
|
|
// also assuming that the name of the column is the function name |
101
|
1 |
|
if($dataField instanceof SerializerField) { |
102
|
|
|
|
103
|
|
|
// this might be better as a callback instead |
104
|
|
|
// get the field name to call |
105
|
1 |
|
$dataField->setData($model); |
106
|
1 |
|
continue; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// if this is a child transformer object then pass it down |
111
|
|
|
// I need more use cases to see where htis will break |
112
|
1 |
|
if($dataField instanceof TransformerField) { |
113
|
|
|
$dataField->setData($model); |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
// if we have a column name then use that first |
119
|
1 |
|
$data = ($col) ? $model->$col : $model->$name; |
120
|
|
|
|
121
|
1 |
|
if (($data === null) && ! $dataField->allowNull()) |
122
|
1 |
|
continue; |
123
|
|
|
|
124
|
1 |
|
if (($data === '') && ! $dataField->allowEmpty()) |
125
|
1 |
|
continue; |
126
|
|
|
|
127
|
1 |
|
$dataField->setData($data); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
public function loadSplitPoint($dataField, $model) |
134
|
|
|
{ |
135
|
1 |
|
$lat = $dataField->getLatitudeColumn(); |
136
|
1 |
|
$lon = $dataField->getLongitudeColumn(); |
137
|
1 |
|
$dataField->setData([ |
138
|
1 |
|
'latitude' => $model->$lat, |
139
|
|
|
'longitude' => $model->$lon |
140
|
1 |
|
]); |
141
|
1 |
|
} |
142
|
|
|
} |
143
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.