|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
5
|
|
|
* Date: 8/16/15 |
|
6
|
|
|
* Time: 4:43 AM. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace NilPortugues\Laravel5\JSendSerializer; |
|
12
|
|
|
|
|
13
|
|
|
use ErrorException; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
15
|
|
|
use NilPortugues\Api\JSend\JSendTransformer; |
|
16
|
|
|
use NilPortugues\Serializer\DeepCopySerializer; |
|
17
|
|
|
use ReflectionClass; |
|
18
|
|
|
use ReflectionMethod; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class JSendSerializer. |
|
22
|
|
|
*/ |
|
23
|
|
|
class JSendSerializer extends DeepCopySerializer |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param JSendTransformer $jSendTransformer |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(JSendTransformer $jSendTransformer) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($jSendTransformer); |
|
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
|
|
View Code Duplication |
if ($value instanceof \Illuminate\Database\Eloquent\Collection) { |
|
|
|
|
|
|
43
|
|
|
$items = []; |
|
44
|
|
|
foreach ($value as &$v) { |
|
45
|
|
|
$items[] = $this->serializeObject($v); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
if ($value instanceof \Illuminate\Contracts\Pagination\Paginator) { |
|
|
|
|
|
|
52
|
|
|
$items = []; |
|
53
|
|
|
foreach ($value->items() as &$v) { |
|
|
|
|
|
|
54
|
|
|
$items[] = $this->serializeObject($v); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (is_subclass_of($value, Model::class, true)) { |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$stdClass = (object) $value->getAttributes(); |
|
63
|
|
|
$data = $this->serializeData($stdClass); |
|
64
|
|
|
$data[self::CLASS_IDENTIFIER_KEY] = get_class($value); |
|
65
|
|
|
|
|
66
|
|
|
$methods = $this->getRelationshipMethodsAsPropertyName($value, get_class($value), new ReflectionClass($value)); |
|
67
|
|
|
|
|
68
|
|
|
if (!empty($methods)) { |
|
69
|
|
|
$data = array_merge($data, $methods); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $data; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return parent::serializeObject($value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $value |
|
80
|
|
|
* @param string $className |
|
81
|
|
|
* @param ReflectionClass $reflection |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getRelationshipMethodsAsPropertyName($value, $className, ReflectionClass $reflection) |
|
86
|
|
|
{ |
|
87
|
|
|
|
|
88
|
|
|
$methods = []; |
|
89
|
|
|
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
90
|
|
|
if (ltrim($method->class, "\\") === ltrim($className, "\\")) { |
|
91
|
|
|
|
|
92
|
|
|
$name = $method->name; |
|
93
|
|
|
$reflectionMethod = $reflection->getMethod($name); |
|
94
|
|
|
|
|
95
|
|
|
// Eloquent relations do not include parameters, so we'll be filtering based on this criteria. |
|
96
|
|
|
if (0 == $reflectionMethod->getNumberOfParameters()) { |
|
97
|
|
|
try { |
|
98
|
|
|
$returned = $reflectionMethod->invoke($value); |
|
99
|
|
|
//All operations (eg: boolean operations) are now filtered out. |
|
100
|
|
|
if (is_object($returned)) { |
|
101
|
|
|
|
|
102
|
|
|
// Only keep those methods as properties if these are returning Eloquent relations. |
|
103
|
|
|
// But do not run the operation as it is an expensive operation. |
|
104
|
|
|
if (false !== strpos(get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { |
|
105
|
|
|
|
|
106
|
|
|
$items = []; |
|
107
|
|
|
foreach ($returned->getResults() as $model) { |
|
108
|
|
|
|
|
109
|
|
|
if (is_object($model)) { |
|
110
|
|
|
$stdClass = (object) $model->getAttributes(); |
|
111
|
|
|
$data = $this->serializeData($stdClass); |
|
112
|
|
|
$data[self::CLASS_IDENTIFIER_KEY] = get_class($model); |
|
113
|
|
|
|
|
114
|
|
|
$items[] = $data; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
if (!empty($items)) { |
|
118
|
|
|
$methods[$name] = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} catch (ErrorException $e) {} |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $methods; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|