Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tucker-Eric committed Dec 1, 2016
1 parent 2bd58dd commit a7800b4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "tucker-eric/laravel-xml-middleware",
"description": "A Laravel Middleware to accept XML requests",
"keywords": [
"Laravel",
"xml-middleware",
"xml",
"middleware",
"xml request",
"request"
],
"require": {
"illuminate/http": "~5.0",
"illuminate/support": "~5.0"
},
"license": "MIT",
"authors": [
{
"name": "Eric Tucker",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"XmlMiddleware\\": "src/"
}
},
"minimum-stability": "dev"
}
22 changes: 22 additions & 0 deletions src/XmlRequestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace XmlMiddleware;

use Closure;

class XmlRequestMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$request->merge($request->xml());

return $next($request);
}
}
33 changes: 33 additions & 0 deletions src/XmlRequestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace XmlMiddleware;

use Illuminate\Support\ServiceProvider;
use Request;

class XmlRequestServiceProvider extends ServiceProvider
{

/**
* Register the application services.
*
* @return void
*/
public function register()
{
Request::macro('isXml', function () {
return strtolower($this->getContentType()) === 'xml';
});

Request::macro('xml', function ($assoc = true) {
if (!$this->isXml()) {
return [];
}
// Returns the xml input from a request
$xml = simplexml_load_string($this->getContent());
$json = json_encode($xml);

return json_decode($json, $assoc);
});
}
}

0 comments on commit a7800b4

Please sign in to comment.