Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mtownsend5512/collection-xml
The missing XML support for Laravel's Collection class.
https://github.com/mtownsend5512/collection-xml
api array collections laravel laravel-package response-format soap toxml xml
Last synced: about 1 month ago
JSON representation
The missing XML support for Laravel's Collection class.
- Host: GitHub
- URL: https://github.com/mtownsend5512/collection-xml
- Owner: mtownsend5512
- License: mit
- Created: 2018-10-16T20:46:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-12T15:43:07.000Z (11 months ago)
- Last Synced: 2024-11-20T21:20:22.649Z (2 months ago)
- Topics: api, array, collections, laravel, laravel-package, response-format, soap, toxml, xml
- Language: PHP
- Size: 21.5 KB
- Stars: 43
- Watchers: 2
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
The missing XML support for Laravel's Collection class.
This package is designed to work with the [Laravel](https://laravel.com) framework.
## Installation
Install via composer:
```
composer require mtownsend/collection-xml
```### Registering the service provider
For Laravel 5.4 and lower, add the following line to your ``config/app.php``:
```php
/*
* Package Service Providers...
*/
Mtownsend\CollectionXml\Providers\CollectionXmlServiceProvider::class,
```For Laravel 5.5 and greater, the package will auto register the provider for you.
### Using Lumen
To register the service provider, add the following line to ``app/bootstrap/app.php``:
```php
$app->register(Mtownsend\CollectionXml\Providers\CollectionXmlServiceProvider::class);
```## Quick start
### Collection to xml
Convert data inside a Laravel Collection into valid XML:
```php
$collection = collect([
'carrier' => 'fedex',
'id' => 123,
'tracking_number' => '9205590164917312751089',
]);$xml = $collection->toXml();
// Returns
fedex
123
9205590164917312751089```
### Collection to soap xml
*Heads up, this method is the most temperamental part of this package. Please thoroughly check your SOAP endpoint and requirements for best usage.*
```php
$collection = collect([
'carrier' => 'fedex',
'id' => 123,
'tracking_number' => '9205590164917312751089',
]);$xml = $collection->toSoapXml('request', 'xmlBody', 'https://yourwebserver/service.asmx?wsdl');
// Returns
fedex
123
9205590164917312751089
```
**Please note:** the SoapFactory class will ping the ``$fullUrl`` to see if it is valid as it builds the SOAP xml. It will not trigger an api interaction, but you will experience an exception if your url is invalid.
### Array to xml
Convert an array into xml without using a collection:
```php
$array = [
'carrier' => 'fedex',
'id' => 123,
'tracking_number' => '9205590164917312751089',
];$xml = array_to_xml($array);
```## Helpers, methods, and arguments
**Helper**
``array_to_xml($array, $root = '')``
The ``$root`` argument allows you to customize the root xml element. Default is ````.
**Collection method**
``->toXml($root)``
See ``array_to_xml()`` above.
**Collection method**
``->toSoapXml($root = '', $soapRoot = '', $fullUrl, array $options = [])``
The ``$root`` argument allows you to customize the inner root xml element. Default is ````.
``$soapRoot`` is the outer xml root element. Default is ``xmlBody``.
``$fullUrl`` will be the fully qualified SOAP endpoint e.g. https://yourwebserver/service.asmx?wsdl. **Please note:** the SoapFactory class will ping the ``$fullUrl`` to see if it is valid as it builds the SOAP xml. It will not trigger an api interaction, but you will experience an exception if your url is invalid.
``$options`` will be an array of valid options for PHP's [SoapClient](http://php.net/manual/en/class.soapclient.php) class. By default ``['trace' => 1]`` is set.
## Purpose
Laravel has always favored json over xml with its api structure. Inevitably, developers will be required to interact with files or apis that require xml, and they are often left to figure it out for themselves.
This package aims to bring painless xml support to Laravel's Collection class, and bring a few useful helpers along.
## Other packages you may be interested in
- [mtownsend/request-xml](https://github.com/mtownsend5512/request-xml)
- [mtownsend/response-xml](https://github.com/mtownsend5512/response-xml)
- [mtownsend/xml-to-array](https://github.com/mtownsend5512/xml-to-array)## Credits
- Mark Townsend
- [Spatie](https://spatie.be/)
- [All Contributors](../../contributors)## Testing
You can run the tests with:
```bash
./vendor/bin/phpunit
```## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.