1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Http\Responses; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Flugg\Responder\Contracts\ResponseFactory; |
7
|
|
|
use Flugg\Responder\TransformBuilder; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use League\Fractal\Pagination\Cursor; |
10
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
11
|
|
|
use League\Fractal\Serializer\SerializerAbstract; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A builder class for building success responses. |
15
|
|
|
* |
16
|
|
|
* @package flugger/laravel-responder |
17
|
|
|
* @author Alexander Tømmerås <[email protected]> |
18
|
|
|
* @license The MIT License |
19
|
|
|
* |
20
|
|
|
* @method $this meta(array $meta) |
21
|
|
|
* @method $this with(array | string $relations) |
22
|
|
|
* @method $this without(array | string $relations) |
23
|
|
|
* @method $this serializer(SerializerAbstract | string $serializer) |
24
|
|
|
* @method $this paginator(IlluminatePaginatorAdapter $paginator) |
25
|
|
|
* @method $this cursor(Cursor $cursor) |
26
|
|
|
*/ |
27
|
|
|
class SuccessResponseBuilder extends ResponseBuilder |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* A builder for building transformed arrays. |
31
|
|
|
* |
32
|
|
|
* @var \Flugg\Responder\TransformBuilder |
33
|
|
|
*/ |
34
|
|
|
protected $transformBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A HTTP status code for the response. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $status = 200; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Construct the builder class. |
45
|
|
|
* |
46
|
|
|
* @param \Flugg\Responder\Contracts\ResponseFactory $responseFactory |
47
|
|
|
* @param \Flugg\Responder\TransformBuilder $transformBuilder |
48
|
|
|
*/ |
49
|
69 |
|
public function __construct(ResponseFactory $responseFactory, TransformBuilder $transformBuilder) |
50
|
|
|
{ |
51
|
69 |
|
$this->transformBuilder = $transformBuilder; |
52
|
|
|
|
53
|
69 |
|
parent::__construct($responseFactory); |
54
|
69 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set resource data for the transformation. |
58
|
|
|
* |
59
|
|
|
* @param mixed $data |
60
|
|
|
* @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
61
|
|
|
* @param string|null $resourceKey |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
56 |
|
public function transform($data = null, $transformer = null, string $resourceKey = null): SuccessResponseBuilder |
65
|
|
|
{ |
66
|
56 |
|
$this->transformBuilder->resource($data, $transformer, $resourceKey); |
67
|
|
|
|
68
|
56 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Dynamically send calls to the transform builder. |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param array $arguments |
76
|
|
|
* @return self|void |
77
|
|
|
*/ |
78
|
33 |
|
public function __call($name, $arguments) |
79
|
|
|
{ |
80
|
33 |
|
if (in_array($name, ['cursor', 'paginator', 'meta', 'with', 'without', 'only', 'serializer'])) { |
81
|
32 |
|
$this->transformBuilder->$name(...$arguments); |
82
|
|
|
|
83
|
32 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
throw new BadMethodCallException; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the serialized response output. |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
60 |
|
protected function getOutput(): array |
95
|
|
|
{ |
96
|
60 |
|
return $this->transformBuilder->transform(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validate the HTTP status code for the response. |
101
|
|
|
* |
102
|
|
|
* @param int $status |
103
|
|
|
* @return void |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
*/ |
106
|
3 |
|
protected function validateStatusCode(int $status) |
107
|
|
|
{ |
108
|
3 |
|
if ($status < 100 || $status >= 400) { |
109
|
1 |
|
throw new InvalidArgumentException("{$status} is not a valid success HTTP status code."); |
110
|
|
|
} |
111
|
2 |
|
} |
112
|
|
|
} |
113
|
|
|
|