|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 11/21/15 |
|
5
|
|
|
* Time: 3:44 PM. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Serializer\Drivers\Eloquent; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
16
|
|
|
use NilPortugues\Serializer\Drivers\Eloquent\Helper\RelationshipPropertyExtractor; |
|
17
|
|
|
use NilPortugues\Serializer\Serializer; |
|
18
|
|
|
use ReflectionClass; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Driver. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Driver extends Serializer |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param mixed $value |
|
34
|
|
|
* |
|
35
|
|
|
* @return mixed|string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function serialize($value) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->reset(); |
|
40
|
|
|
|
|
41
|
|
|
return $this->serializeObject($value); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Extract the data from an object. |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function serializeObject($value) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($value instanceof Collection) { |
|
54
|
|
|
return $this->serializeEloquentCollection($value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($value instanceof Paginator) { |
|
58
|
|
|
return $this->serializeEloquentPaginatedResource($value); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (\is_subclass_of($value, Model::class, true)) { |
|
|
|
|
|
|
62
|
|
|
return $this->serializeEloquentModel($value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return parent::serializeObject($value); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Collection $value |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
protected function serializeEloquentCollection(Collection $value) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$items = []; |
|
76
|
|
|
foreach ($value as $v) { |
|
77
|
|
|
$items[] = $this->serializeObject($v); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Paginator $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
protected function serializeEloquentPaginatedResource(Paginator $value) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$items = []; |
|
91
|
|
|
foreach ($value->items() as $v) { |
|
92
|
|
|
$items[] = $this->serializeObject($v); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Model $value |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function serializeEloquentModel(Model $value) |
|
104
|
|
|
{ |
|
105
|
|
|
$stdClass = (object) $value->attributesToArray(); |
|
106
|
|
|
$data = $this->serializeData($stdClass); |
|
107
|
|
|
$data[self::CLASS_IDENTIFIER_KEY] = get_class($value); |
|
108
|
|
|
|
|
109
|
|
|
$methods = RelationshipPropertyExtractor::getRelationshipAsPropertyName( |
|
110
|
|
|
$value, |
|
111
|
|
|
get_class($value), |
|
112
|
|
|
new ReflectionClass($value), |
|
113
|
|
|
$this |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
if (!empty($methods)) { |
|
117
|
|
|
$data = array_merge($data, $methods); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $data; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.