├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── composer.json
├── phpunit.xml
├── src
├── Nathanmac
│ └── Parser
│ │ ├── Facades
│ │ └── Parser.php
│ │ ├── Parser.php
│ │ └── ParserServiceProvider.php
└── config
│ └── config.php
└── tests
└── .gitkeep
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | composer.phar
3 | composer.lock
4 | .DS_Store
5 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 5.4
5 | - 5.5
6 | - 5.6
7 | - hhvm
8 |
9 | before_script:
10 | - composer self-update
11 | - composer install --prefer-source --no-interaction --dev
12 |
13 | script: phpunit
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Nathan Macnamara
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | laravel-parser
2 | ==============
3 |
4 | [](https://travis-ci.org/nathanmac/laravel-parser)
5 | [](http://stillmaintained.com/nathanmac/laravel-parser)
6 |
7 | > Project no longer maintained see the [Parser](https://github.com/nathanmac/Parser) project for a replacement.
8 |
9 |
10 | Simple Format Parser For Laravel 4
11 |
12 | Installation
13 | ------------
14 |
15 | Begin by installing this package through Composer. Edit your project's `composer.json` file to require `Nathanmac/laravel-parser`.
16 |
17 | "require": {
18 | "nathanmac/laravel-parser": "dev-master"
19 | }
20 |
21 | Next, update Composer from the Terminal:
22 |
23 | composer update
24 |
25 | Once this operation completes, the final step is to add the service provider. Open `app/config/app.php`, and add a new item to the providers array.
26 |
27 | 'Nathanmac\Parser\ParserServiceProvider'
28 |
29 | ##### Parsing Functions
30 | ```php
31 | Parse::json($payload); // JSON > Array
32 | Parse::xml($payload); // XML > Array
33 | Parse::yaml($payload); // YAML > Array
34 | Parse::querystr($payload); // Query String > Array
35 | Parse::serialize($payload); // Serialized Object > Array
36 | ```
37 |
38 | ##### Parse Input/Payload (PUT/POST)
39 | ```php
40 | Parse::payload(); // Auto Detect Type - 'Content Type' HTTP Header
41 | Parse::payload('application/json'); // Specifiy the content type
42 | ```
43 |
44 | ##### Parse JSON
45 | ```php
46 | $parsed = Parse::json('
47 | {
48 | "message": {
49 | "to": "Jack Smith",
50 | "from": "Jane Doe",
51 | "subject": "Hello World",
52 | "body": "Hello, whats going on..."
53 | }
54 | }');
55 | ```
56 |
57 | ##### Parse XML
58 | ```php
59 | $parsed = Parse::xml('
60 |
61 |
62 |
63 | Jack Smith
64 | Jane Doe
65 | Hello World
66 | Hello, whats going on...
67 |
68 | ');
69 | ```
70 |
71 | ##### Parse Query String
72 | ```php
73 | $parsed = Parse::querystr('to=Jack Smith&from=Jane Doe&subject=Hello World&body=Hello, whats going on...');
74 | ```
75 |
76 | ##### Parse Serialized Object
77 | ```php
78 | $parsed = Parse::serialize('a:1:{s:7:"message";a:4:{s:2:"to";s:10:"Jack Smith";s:4:"from";s:8:"Jane Doe";s:7:"subject";s:11:"Hello World";s:4:"body";s:24:"Hello, whats going on...";}}');
79 | ```
80 |
81 | ##### Parse YAML
82 | ```php
83 | $parsed = Parse::yaml('
84 | ---
85 | message:
86 | to: "Jack Smith"
87 | from: "Jane Doe"
88 | subject: "Hello World"
89 | body: "Hello, whats going on..."
90 | ');
91 | ```
92 |
93 | ###### Supported Content-Types
94 | ```
95 | XML
96 | ---
97 | application/xml > XML
98 | text/xml > XML
99 |
100 | JSON
101 | ----
102 | application/json > JSON
103 | application/x-javascript > JSON
104 | text/javascript > JSON
105 | text/x-javascript > JSON
106 | text/x-json > JSON
107 |
108 | YAML
109 | ----
110 | text/yaml > YAML
111 | text/x-yaml > YAML
112 | application/yaml > YAML
113 | application/x-yaml > YAML
114 |
115 | MISC
116 | ----
117 | application/vnd.php.serialized > Serialized Object
118 | application/x-www-form-urlencoded' > Query String
119 | ```
120 |
121 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nathanmac/laravel-parser",
3 | "description": "Simple Format Parser For Laravel 4",
4 | "license": "MIT",
5 | "authors": [
6 | {
7 | "name": "nathanmac",
8 | "email": "nathan.macnamara@arrowecs.co.uk"
9 | }
10 | ],
11 | "require": {
12 | "php": ">=5.4.0",
13 | "symfony/yaml": "2.4.*",
14 | "illuminate/support": "4.1.*"
15 | },
16 | "autoload": {
17 | "psr-0": {
18 | "Nathanmac\\Parser": "src/"
19 | }
20 | },
21 | "minimum-stability": "stable"
22 | }
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 | ./tests/
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/Nathanmac/Parser/Facades/Parser.php:
--------------------------------------------------------------------------------
1 | {Config::get('parser::supported_formats')[$format]}($this->_payload());
16 | }
17 | throw new ParserException('Invalid Or Unsupported Format');
18 | }
19 | return $this->{$this->_format()}($this->_payload());
20 | }
21 |
22 | private function _format() {
23 | if (isset($_SERVER['HTTP_CONTENT_TYPE']))
24 | if (isset(Config::get('parser::supported_formats')[$_SERVER['HTTP_CONTENT_TYPE']]))
25 | return Config::get('parser::supported_formats')[$_SERVER['HTTP_CONTENT_TYPE']];
26 | return Config::get('parser::default');
27 | }
28 |
29 | private function _payload() {
30 | return file_get_contents('php://input');
31 | }
32 |
33 | public function xml($string) {
34 | if ($string) {
35 | $xml = @simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
36 | if(!$xml)
37 | throw new ParserException('Failed To Parse XML');
38 | return json_decode(json_encode((array) $xml), 1); // Work arround to accept xml input
39 | }
40 | return array();
41 | }
42 |
43 | public function json($string) {
44 | if ($string) {
45 | $json = json_decode(trim($string), true);
46 | if (!$json)
47 | throw new ParserException('Failed To Parse JSON');
48 | return $json;
49 | }
50 | return array();
51 | }
52 |
53 | public function serialize($string) {
54 | if ($string) {
55 | $serial = @unserialize(trim($string));
56 | if (!$serial)
57 | throw new ParserException('Failed To Parse Serialized Data');
58 | return $serial;
59 | }
60 | return array();
61 | }
62 |
63 | public function querystr($string) {
64 | if ($string) {
65 | @parse_str(trim($string), $querystr);
66 | if (!$querystr)
67 | throw new ParserException('Failed To Parse Query String');
68 | return $querystr;
69 | }
70 | return array();
71 | }
72 |
73 | public function yaml($string) {
74 | if ($string) {
75 | try {
76 | return Yaml::parse(trim(preg_replace('/\t+/', '', $string)));
77 | } catch (Exception $ex) {
78 | throw new ParserException('Failed To Parse YAML');
79 | }
80 | }
81 | return array();
82 | }
83 | }
--------------------------------------------------------------------------------
/src/Nathanmac/Parser/ParserServiceProvider.php:
--------------------------------------------------------------------------------
1 | package('nathanmac/parser');
22 | }
23 |
24 | /**
25 | * Register the service provider.
26 | *
27 | * @return void
28 | */
29 | public function register()
30 | {
31 | $this->app['parser'] = $this->app->share(function($app) {
32 | return new Parser;
33 | });
34 |
35 | $this->app->booting(function() {
36 | $loader = \Illuminate\Foundation\AliasLoader::getInstance();
37 | $loader->alias('Parse', 'Nathanmac\Parser\Facades\Parser');
38 | });
39 | }
40 |
41 | /**
42 | * Get the services provided by the provider.
43 | *
44 | * @return array
45 | */
46 | public function provides()
47 | {
48 | return array('parser');
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/config/config.php:
--------------------------------------------------------------------------------
1 | 'json',
6 |
7 | 'supported_formats' => array(
8 | // XML
9 | 'application/xml' => 'xml',
10 | 'text/xml' => 'xml',
11 | // JSON
12 | 'application/json' => 'json',
13 | 'application/x-javascript' => 'json',
14 | 'text/javascript' => 'json',
15 | 'text/x-javascript' => 'json',
16 | 'text/x-json' => 'json',
17 | // YAML
18 | 'text/yaml' => 'yaml',
19 | 'text/x-yaml' => 'yaml',
20 | 'application/yaml' => 'yaml',
21 | 'application/x-yaml' => 'yaml',
22 | // MISC
23 | 'application/vnd.php.serialized' => 'serialize',
24 | 'application/x-www-form-urlencoded' => 'querystr'
25 | )
26 | );
--------------------------------------------------------------------------------
/tests/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nathanmac/laravel-parser/0f5b21d12d00e71654038f686303ba506ff068e8/tests/.gitkeep
--------------------------------------------------------------------------------