GithubHelp home page GithubHelp logo

ibnuhalimm / laravel-xml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mtvbrianking/laravel-xml

0.0 1.0 0.0 992 KB

Laravel XML support; middle-ware, response, and utilities.

License: MIT License

PHP 100.00%

laravel-xml's Introduction

Laravel XML Support Package

Code Quality Code Style Total Downloads Latest Stable Version License Documentation

This package comes with the much desired xml support for you Laravel project including middleware to accept only xml requests, http response in xml, and more utilities for xml conversions as well as validation.

Supports: Laravel versions v5.3 and above

Installation

$ composer require bmatovu/laravel-xml

Requests

Get the request content (body).

$request->xml();

* Returns Bmatovu\LaravelXml\Support\XMLElement object.

Determine if the request content type is XML.

$request->isXml();

Determine if the current request is accepting XML.

$request->wantsXml();

Validate XML content

Xml::is_valid($request->xml());

Validation - Against XML Schema Definition

$errors = Xml::validate($request->xml(), 'path_to/sample.xsd');

if ($errors) {
    return response()->xml(['error' => $errors], 422);
}

Responses

Expects an array, convent you're objects to arrays prior...

Route::get('/users', function () {
    $users = App\User::all();
    return response()->xml(['users' => $users->toArray()]);
});

Sample response from above snippet

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <users>
        <id>1</id>
        <name>John Doe</name>
        <email>[email protected]</email>
        <created_at>2018-07-12 17:06:13</created_at>
        <updated_at>2018-07-12 18:00:05</updated_at>
    </users>
    <users>
        <id>2</id>
        <name>Gary Plant</name>
        <email>[email protected]</email>
        <created_at>2018-07-12 18:02:26</created_at>
        <updated_at>2018-07-13 11:22:44</updated_at>
    </users>
</document>

And will automatically set the content type to xml

Content-Type โ†’ text/xml; charset=UTF-8

Middleware

First register the middleware in app\Http\Kernel.php

protected $routeMiddleware = [
    // ...
    'xml' => \Bmatovu\LaravelXml\Http\Middleware\RequireXml::class,
];

Then use the middleware on your routes, or in the controllers.

Route::post('/user/store', function (Request, $request) {
    // do something...
})->middleware('xml');

In case of the request content-type is not xml, the response will be;

[415 - Unsupported Media Type]

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <error>Only accepting xml content</error>
</document>

Utilities

Encode: Array to Xml

Xml::encode(['key' => 'value']);

Or

xml_encode(['key' => 'value']);

Decode: Xml to Array

Xml::decode('<?xml version="1.0" encoding="UTF-8"?><document><key>value</key></document>');

Or

xml_decode('<?xml version="1.0" encoding="UTF-8"?><document><key>value</key></document>');

Credits

Under the hood, I'm using;

Spatie's array to XML convernsion

Hakre's XML to JSON conversion

Akande's XML validation

Reporting bugs

If you've stumbled across a bug, please help us by leaving as much information about the bug as possible, e.g.

  • Steps to reproduce
  • Expected result
  • Actual result

This will help us to fix the bug as quickly as possible, and if you do wish to fix it yourself; feel free to fork the package on GitHub and submit a pull request!

laravel-xml's People

Contributors

mtvbrianking avatar poket-jony avatar trollfalgar avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.