Skip to content

Commit 26456ba

Browse files
committed
added custom template
1 parent ff941d7 commit 26456ba

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ php artisan vendor:publish
2323

2424
### Example
2525
```php
26-
//default template from xml.php config file like: '<root></root>'
26+
// basic usage with default config from xml.php file
2727
Route::get('/', function () {
2828
return response()->xml(User::all());
2929
});
3030

3131

3232
// custom template like: '<result></result>'
3333
Route::get('/foo', function () {
34-
return response()->xml(['foo' => 1, 'bar' => 2, 'baz' => 3], '<result></result>);
34+
return response()->xml(['foo' => 1, 'bar' => 2, 'baz' => 3], [], '<result></result>);
3535
});
3636
```
3737

src/XmlResponse/XmlResponse.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ private function addAttribute($attribute = [], \SimpleXMLElement $xml)
100100
* @param $array
101101
* @param bool $xml
102102
* @param array $headerAttribute
103+
* @param string $template
104+
*
103105
* @return mixed
104106
* @throws XmlResponseException
105107
*/
106-
function array2xml($array, $xml = false, $headerAttribute = [])
108+
function array2xml($array, $xml = false, $headerAttribute = [], $template = null)
107109
{
108110
if (!$this->isType(gettype($array))){
109111
throw new XmlResponseException('It is not possible to convert the data');
@@ -113,8 +115,10 @@ function array2xml($array, $xml = false, $headerAttribute = [])
113115
$array = $array->toArray();
114116
}
115117

118+
$tmpl = empty($template) ? $this->template : $template;
119+
116120
if($xml === false){
117-
$xml = new \SimpleXMLElement($this->template);
121+
$xml = new \SimpleXMLElement($tmpl);
118122
}
119123

120124
$this->addAttribute($headerAttribute, $xml);

src/XmlResponse/XmlResponseServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function boot()
2525

2626
public function register()
2727
{
28-
Response::macro('xml', function ($value, $headerTemplate = []) {
29-
return (new XmlResponse())->array2xml($value, false, $headerTemplate);
28+
Response::macro('xml', function ($value, $headerTemplate = [], $template = null) {
29+
return (new XmlResponse())->array2xml($value, false, $headerTemplate, $template);
3030
});
3131
}
3232
}

0 commit comments

Comments
 (0)