1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StephaneCoinon\SendGridActivity\Responses; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use StephaneCoinon\SendGridActivity\Support\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Data model for API response results. |
10
|
|
|
*/ |
11
|
|
|
class Response |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Name of the key containing the unique resource id. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $key = 'id'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Attributes to cast to a native type. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $casts = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Name of key under which multiple results are nested. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $dataKey = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Make a new Response instance. |
36
|
|
|
* |
37
|
|
|
* @param array $attributes |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $attributes = []) |
40
|
|
|
{ |
41
|
|
|
$this->fill($attributes); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Build a collection of Response models from an array. |
46
|
|
|
* |
47
|
|
|
* @param array $items raw array of results from API response |
48
|
|
|
* @param array $apiContext |
49
|
|
|
* @return static[]|Collection |
50
|
|
|
*/ |
51
|
|
|
public static function collection(array $items = [], array $apiContext = []) |
52
|
|
|
{ |
53
|
|
|
return Collection::make($items)->map(function ($item) use ($apiContext) { |
54
|
|
|
return (new static($item))->fill($apiContext); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Fill the response with an array of attributes. |
60
|
|
|
* |
61
|
|
|
* @param array $attributes |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
|
|
public function fill(array $attributes = []): self |
65
|
|
|
{ |
66
|
|
|
foreach ($attributes as $key => $value) { |
67
|
|
|
$this->setAttribute($key, $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the value of an attribute. |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function getAttribute(string $name) |
80
|
|
|
{ |
81
|
|
|
return $this->$name ?? null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the value of an attribute. |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* @param mixed $value |
89
|
|
|
* @return self |
90
|
|
|
*/ |
91
|
|
|
public function setAttribute(string $name, $value): self |
92
|
|
|
{ |
93
|
|
|
$this->$name = $this->castAttribute($name, $value); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the name of the resource unique identifier key. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getKeyName() |
104
|
|
|
{ |
105
|
|
|
return $this->key; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the value of the resource unique identifer. |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function getKey() |
114
|
|
|
{ |
115
|
|
|
return $this->getAttribute($this->getKeyName()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Cast an attribute to its native type. |
120
|
|
|
* |
121
|
|
|
* This method uses the types declared in static::$casts. |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* @param mixed $value |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
|
|
public function castAttribute($key, $value) |
128
|
|
|
{ |
129
|
|
|
switch ($this->casts[$key] ?? null) { |
130
|
|
|
case 'datetime': |
131
|
|
|
return Carbon::parse($value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Cast a JSON-decoded API response to Response instance(s) |
139
|
|
|
* |
140
|
|
|
* @param array $apiResponse |
141
|
|
|
* @param array $apiContext |
142
|
|
|
* @return static|static[] |
143
|
|
|
*/ |
144
|
|
|
public static function createFromApiResponse($apiResponse, array $apiContext = []) |
145
|
|
|
{ |
146
|
|
|
$dataKey = (new static)->dataKey; |
147
|
|
|
|
148
|
|
|
return isset($apiResponse[$dataKey]) |
149
|
|
|
// Collection of items nested under the data key |
150
|
|
|
? static::collection($apiResponse[$dataKey], $apiContext) |
151
|
|
|
// Single item |
152
|
|
|
: (new static($apiResponse))->fill($apiContext); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Fetch a fresh resource. |
157
|
|
|
* |
158
|
|
|
* @return self |
159
|
|
|
*/ |
160
|
|
|
public function fresh(): self |
161
|
|
|
{ |
162
|
|
|
return $this->api->request($this->request::find($this->getKey())); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: