1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NilPortugues\Serializer\Drivers\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use NilPortugues\Serializer\Drivers\Eloquent\Helper\RelationshipPropertyExtractor; |
9
|
|
|
use NilPortugues\Serializer\Serializer; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
|
12
|
|
|
class Driver extends Serializer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param mixed $value |
23
|
|
|
* |
24
|
|
|
* @return mixed|string |
25
|
|
|
*/ |
26
|
|
|
public function serialize($value) |
27
|
|
|
{ |
28
|
|
|
$this->reset(); |
29
|
|
|
|
30
|
|
|
return $this->serializeObject($value); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Extract the data from an object. |
35
|
|
|
* |
36
|
|
|
* @param mixed $value |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
protected function serializeObject($value) |
41
|
|
|
{ |
42
|
|
|
if ($value instanceof Collection) { |
43
|
|
|
return $this->serializeEloquentCollection($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($value instanceof Paginator) { |
47
|
|
|
return $this->serializeEloquentPaginatedResource($value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (\is_subclass_of($value, Model::class, true)) { |
|
|
|
|
51
|
|
|
return $this->serializeEloquentModel($value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return parent::serializeObject($value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Collection $value |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
protected function serializeEloquentCollection(Collection $value) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$items = []; |
65
|
|
|
foreach ($value as $v) { |
66
|
|
|
$items[] = $this->serializeObject($v); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Paginator $value |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
protected function serializeEloquentPaginatedResource(Paginator $value) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$items = []; |
80
|
|
|
foreach ($value->items() as $v) { |
81
|
|
|
$items[] = $this->serializeObject($v); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Model $value |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function serializeEloquentModel(Model $value) |
93
|
|
|
{ |
94
|
|
|
$stdClass = (object) $value->attributesToArray(); |
95
|
|
|
$data = $this->serializeData($stdClass); |
96
|
|
|
$data[self::CLASS_IDENTIFIER_KEY] = get_class($value); |
97
|
|
|
|
98
|
|
|
$methods = RelationshipPropertyExtractor::getRelationshipAsPropertyName( |
99
|
|
|
$value, |
100
|
|
|
get_class($value), |
101
|
|
|
new ReflectionClass($value), |
102
|
|
|
$this |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if (!empty($methods)) { |
106
|
|
|
$data = array_merge($data, $methods); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $data; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.