|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RichToms\LaravelJsend\Responses; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
class SuccessResponse extends Response |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $statusText = 'success'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function getResponseData() |
|
19
|
|
|
{ |
|
20
|
|
|
// If the data given is an associative array, assume the developer knows what they are sending |
|
21
|
|
|
if (is_array($this->data) && Arr::isAssoc($this->data)) { |
|
22
|
|
|
return $this->data; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// If the data given is a sequential array, attempt to determine the keys to group the response by |
|
26
|
|
|
if (is_array($this->data)) { |
|
27
|
|
|
return $this->mergeKeys( |
|
28
|
|
|
$this->determineKeys($this->data), |
|
29
|
|
|
$this->data |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (! is_object($this->data)) { |
|
34
|
|
|
return ['object' => $this->data]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// If the class name contains Resource |
|
38
|
|
|
if (stristr($className = class_basename($this->data), 'Resource') !== false) { |
|
39
|
|
|
return [ |
|
40
|
|
|
Str::plural( |
|
41
|
|
|
$this->toSlug(explode('Resource', $className)[0]), |
|
42
|
|
|
stristr($className, 'Collection') !== false ? 0 : 1 |
|
43
|
|
|
) => $this->data->toArray(), |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// If the data is not an object and is not a Laravel Resource, return the class name and the value |
|
48
|
|
|
return [ |
|
49
|
|
|
$this->toSlug($className) => $this->data, |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $keys |
|
55
|
|
|
* @param array $values |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function mergeKeys($keys = [], $values = []) |
|
59
|
|
|
{ |
|
60
|
|
|
// Group the values that come out with the slug that matches the key |
|
61
|
|
|
return array_combine($keys, array_map(function ($arr) { |
|
62
|
|
|
return array_values($arr); |
|
63
|
|
|
}, array_map(function ($key) use ($values) { |
|
64
|
|
|
return Arr::where($values, function ($value) use ($key) { |
|
65
|
|
|
return $this->determineSlug($value) === $key; |
|
66
|
|
|
}); |
|
67
|
|
|
}, $keys))); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $values |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function determineKeys($values) |
|
75
|
|
|
{ |
|
76
|
|
|
return array_unique(array_map(function ($value) { |
|
77
|
|
|
return $this->determineSlug($value); |
|
78
|
|
|
}, $values)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param mixed $value |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function determineSlug($value) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->toSlug(Str::plural((function ($value) { |
|
88
|
|
|
if (is_object($value)) { |
|
89
|
|
|
return class_basename($value); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $value['type'] ?? 'object'; |
|
93
|
|
|
})($value))); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function toSlug($string) |
|
97
|
|
|
{ |
|
98
|
|
|
return Str::snake($string); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|