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 | * API client. |
||
36 | * |
||
37 | * @var \StephaneCoinon\SendGridActivity\SendGrid |
||
38 | */ |
||
39 | protected $api = null; |
||
40 | |||
41 | /** |
||
42 | * Make a new Response instance. |
||
43 | * |
||
44 | * @param array $attributes |
||
45 | */ |
||
46 | public function __construct(array $attributes = []) |
||
47 | { |
||
48 | $this->fill($attributes); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Build a collection of Response models from an array. |
||
53 | * |
||
54 | * @param array $items raw array of results from API response |
||
55 | * @param array $apiContext |
||
56 | * @return static[]|Collection |
||
57 | */ |
||
58 | public static function collection(array $items = [], array $apiContext = []) |
||
59 | { |
||
60 | return Collection::make($items)->map(function ($item) use ($apiContext) { |
||
61 | return (new static($item))->fill($apiContext); |
||
62 | }); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Fill the response with an array of attributes. |
||
67 | * |
||
68 | * @param array $attributes |
||
69 | * @return self |
||
70 | */ |
||
71 | public function fill(array $attributes = []): self |
||
72 | { |
||
73 | foreach ($attributes as $key => $value) { |
||
74 | $this->setAttribute($key, $value); |
||
75 | } |
||
76 | |||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get the value of an attribute. |
||
82 | * |
||
83 | * @param string $name |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function getAttribute(string $name) |
||
87 | { |
||
88 | return $this->$name ?? null; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Set the value of an attribute. |
||
93 | * |
||
94 | * @param string $name |
||
95 | * @param mixed $value |
||
96 | * @return self |
||
97 | */ |
||
98 | public function setAttribute(string $name, $value): self |
||
99 | { |
||
100 | $this->$name = $this->castAttribute($name, $value); |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get the name of the resource unique identifier key. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getKeyName() |
||
111 | { |
||
112 | return $this->key; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the value of the resource unique identifer. |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function getKey() |
||
121 | { |
||
122 | return $this->getAttribute($this->getKeyName()); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Cast an attribute to its native type. |
||
127 | * |
||
128 | * This method uses the types declared in static::$casts. |
||
129 | * |
||
130 | * @param string $key |
||
131 | * @param mixed $value |
||
132 | * @return self |
||
133 | */ |
||
134 | public function castAttribute($key, $value) |
||
135 | { |
||
136 | switch ($this->casts[$key] ?? null) { |
||
137 | case 'datetime': |
||
138 | return Carbon::parse($value); |
||
139 | } |
||
140 | |||
141 | return $value; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Cast a JSON-decoded API response to Response instance(s) |
||
146 | * |
||
147 | * @param array $apiResponse |
||
148 | * @param array $apiContext |
||
149 | * @return static|static[] |
||
150 | */ |
||
151 | public static function createFromApiResponse($apiResponse, array $apiContext = []) |
||
152 | { |
||
153 | $dataKey = (new static)->dataKey; |
||
154 | |||
155 | return isset($apiResponse[$dataKey]) |
||
156 | // Collection of items nested under the data key |
||
157 | ? static::collection($apiResponse[$dataKey], $apiContext) |
||
158 | // Single item |
||
159 | : (new static($apiResponse))->fill($apiContext); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Fetch a fresh resource. |
||
164 | * |
||
165 | * @return self |
||
166 | */ |
||
167 | public function fresh() |
||
168 | { |
||
169 | return $this->api->request($this->request::find($this->getKey())); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
170 | } |
||
171 | } |
||
172 |