1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ghc\Rosetta\Messages; |
4
|
|
|
|
5
|
|
|
use Ghc\Rosetta\Configurable; |
6
|
|
|
use Ghc\Rosetta\Pipes\Pipeable; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
use Serializable; |
11
|
|
|
|
12
|
|
|
abstract class Message implements Arrayable, Serializable, JsonSerializable, Pipeable |
13
|
|
|
{ |
14
|
|
|
use Configurable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Message data. |
18
|
|
|
* |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $data; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Message constructor. |
25
|
|
|
* |
26
|
|
|
* @param null|mixed $data |
27
|
|
|
* @param array $config |
28
|
|
|
*/ |
29
|
|
|
public function __construct($data = null, $config = []) |
30
|
|
|
{ |
31
|
|
|
$this->setData($data ?: $this->newData()); |
|
|
|
|
32
|
|
|
$this->setConfig($config); |
33
|
|
|
|
34
|
|
|
$this->boot(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Boot Transformer. |
39
|
|
|
*/ |
40
|
|
|
protected function boot() |
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return null |
46
|
|
|
*/ |
47
|
|
|
public function newData() |
48
|
|
|
{ |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function getData() |
55
|
|
|
{ |
56
|
|
|
return $this->data; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $data |
61
|
|
|
* |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
|
|
public function setData($data) |
65
|
|
|
{ |
66
|
|
|
$this->data = $data; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function serialize() |
75
|
|
|
{ |
76
|
|
|
return serialize([ |
77
|
|
|
'data' => $this->toArray(), |
78
|
|
|
'config' => $this->getConfig(), |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $serialized |
84
|
|
|
*/ |
85
|
|
|
public function unserialize($serialized) |
86
|
|
|
{ |
87
|
|
|
$unserialized = unserialize($serialized); |
88
|
|
|
$this->fromArray($unserialized['data']); |
89
|
|
|
$this->setConfig($unserialized['config']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function jsonSerialize() |
96
|
|
|
{ |
97
|
|
|
return $this->toArray(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function __toString() |
104
|
|
|
{ |
105
|
|
|
return (string) $this->getData(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $options |
110
|
|
|
* |
111
|
|
|
* @return \Closure |
112
|
|
|
*/ |
113
|
|
|
public function pipe($options = []) |
114
|
|
|
{ |
115
|
|
|
return function ($inputData) use ($options) { |
116
|
|
|
$fromArray = Arr::get($options, 'fromArray', true); |
117
|
|
|
if ($fromArray) { |
118
|
|
|
$this->fromArray($inputData); |
119
|
|
|
} else { |
120
|
|
|
$this->setData($inputData); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->toArray(); |
124
|
|
|
}; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $data |
129
|
|
|
* |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
abstract public function fromArray($data); |
133
|
|
|
} |
134
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.