1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\Laravel\JavaScriptData; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
|
8
|
|
|
class ResponseFactory |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory |
12
|
|
|
*/ |
13
|
|
|
private $responseFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Swis\Laravel\JavaScriptData\Builder |
17
|
|
|
*/ |
18
|
|
|
private $builder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory |
22
|
|
|
* @param \Swis\Laravel\JavaScriptData\Builder $builder |
23
|
|
|
*/ |
24
|
30 |
|
public function __construct(ResponseFactoryContract $responseFactory, Builder $builder) |
25
|
|
|
{ |
26
|
30 |
|
$this->responseFactory = $responseFactory; |
27
|
30 |
|
$this->builder = $builder; |
28
|
30 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Return a new JavaScript data response from the application. |
32
|
|
|
* |
33
|
|
|
* @param string $name |
34
|
|
|
* @param mixed $data |
35
|
|
|
* @param int $status |
36
|
|
|
* @param array $headers |
37
|
|
|
* @param int $options |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Http\Response |
40
|
|
|
*/ |
41
|
30 |
|
public function make(string $name, $data = [], int $status = 200, array $headers = [], $options = 0): Response |
42
|
|
|
{ |
43
|
30 |
|
$name = ltrim(config('javascript-data-response.namespace', '').'.'.$name, '.'); |
44
|
|
|
|
45
|
30 |
|
if (empty($headers)) { |
46
|
27 |
|
$headers = (array)config('javascript-data-response.headers', []); |
47
|
|
|
} |
48
|
|
|
|
49
|
30 |
|
if ($options === 0) { |
50
|
27 |
|
$options = (int)config('javascript-data-response.json_encode-options', 0); |
51
|
|
|
} |
52
|
|
|
|
53
|
30 |
|
if (config('javascript-data-response.pretty-print', false)) { |
54
|
3 |
|
$options |= JSON_PRETTY_PRINT; |
55
|
|
|
} |
56
|
|
|
|
57
|
30 |
|
return $this->responseFactory->make( |
58
|
30 |
|
$this->builder->build($name, $data, $options), |
59
|
30 |
|
$status, |
60
|
30 |
|
$headers |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|