|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bmatovu\LaravelXml\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Bmatovu\LaravelXml\Support\ArrayToXml; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Http\ResponseTrait; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
10
|
|
|
use SimpleXMLElement; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response as BaseResponse; |
|
12
|
|
|
|
|
13
|
|
|
class XmlResponse extends BaseResponse |
|
14
|
|
|
{ |
|
15
|
|
|
use Macroable, ResponseTrait { |
|
|
|
|
|
|
16
|
|
|
Macroable::__call as macroCall; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param mixed $data The response data |
|
23
|
|
|
* @param int $status The response status code |
|
24
|
|
|
* @param array $headers An array of response headers |
|
25
|
|
|
* @param array $options |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct($data = null, $status = 200, $headers = [], $options = []) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$headers = array_merge(config('xml.headers'), $headers); |
|
30
|
|
|
|
|
31
|
2 |
|
if ($data instanceof SimpleXmlElement) { |
|
32
|
1 |
|
parent::__construct($data->asXML(), $status, $headers); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
1 |
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
if ($data instanceof Model || $data instanceof Collection) { |
|
38
|
|
|
$data = $data->toArray(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
if (\is_array($data)) { |
|
42
|
1 |
|
$data = ArrayToXml::convert( |
|
43
|
1 |
|
$data, |
|
44
|
1 |
|
$options['root'] ?? config('xml.root'), |
|
45
|
1 |
|
$options['case'] ?? config('xml.case'), |
|
46
|
1 |
|
$options['declaration']['version'] ?? config('xml.declaration.version'), |
|
47
|
1 |
|
$options['declaration']['encoding'] ?? config('xml.declaration.encoding'), |
|
48
|
1 |
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
parent::__construct($data, $status, $headers); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|