2017 © Pedro Peláez
 

library parser

Simple PHP Parser Utility Library for API Development

image

nathanmac/parser

Simple PHP Parser Utility Library for API Development

  • Wednesday, April 18, 2018
  • by nathanmac
  • Repository
  • 14 Watchers
  • 180 Stars
  • 255,618 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 40 Forks
  • 14 Open issues
  • 29 Versions
  • 11 % Grown

The README.md

Parser

Latest Version on Packagist Software License Build Status Total Downloads, (*1)

Simple PHP Parser Library for API Development, parse a post http payload into a php array., (*2)

Also see the Responder library for handling output., (*3)

Installation

Begin by installing this package through Composer. From the Terminal:, (*4)

composer require nathanmac/parser

Laravel/Lumen Users

Laravel/Lumen Verison Supported Library Verison
Laravel/Lumen 5+ > 3.*
Laravel 4 2.*

Laravel Users (Adding the Service Provider)

If you are a Laravel user, then there is a service provider that you can make use of to automatically prepare the bindings and such., (*5)

Include the service provider within app/config/app.php., (*6)

'providers' => [
    ...,
    Nathanmac\Utilities\Parser\ParserServiceProvider::class
];

And, for convenience, add a facade alias to this same file at the bottom:, (*7)

'aliases' => [
    ...,
    'Parser' => Nathanmac\Utilities\Parser\Facades\Parser::class,
];

Lumen Users (Adding the Service Provider)

If you are a Lumen user, then there is a service provider that you can make use of to automatically prepare the binding and such., (*8)

// bootstrap/app.php

$app->register('Nathanmac\Utilities\Parser\ParserServiceProvider');

Lumen users can also add the facade alias., (*9)

// bootstrap/app.php

class_alias('Nathanmac\Utilities\Parser\Facades\Parser', 'Parser');

Using the Facade

public function index()
{
    Parser::payload('application/json');

    Parser::json($payload);         // JSON > Array
    Parser::xml($payload);          // XML > Array
    Parser::yaml($payload);         // YAML > Array
    Parser::querystr($payload);     // Query String > Array
    Parser::serialize($payload);    // Serialized Object > Array
    Parser::bson($payload);         // BSON > Array
    Parser::msgpack($payload);      // MSGPack > Array

    Parser::all();                         // Return all values
    Parser::has('key');                    // Does a key exist, with value.
    Parser::get('key', 'default value');   // Get value by key, set an optional default.
    Parser::only('id', 'name', 'email');   // Only return value from the selected keys.
    Parser::except('password');            // Don't return values from the selected keys.
    Parser::mask($mask);                   // Return masked values (see Mask Function, below).
}

All the examples below assume you aren't using Laravel (or Lumen), and therefore don't have access to the facade. As with any other facade, instead of:, (*10)

$parser = new Parser();

$parser->{$method}($payload);

just use:, (*11)

Parser::{$method}($payload);

Usage

Parsing Functions

$parser->json($payload);        // JSON > Array
$parser->xml($payload);         // XML > Array
$parser->yaml($payload);        // YAML > Array
$parser->querystr($payload);    // Query String > Array
$parser->serialize($payload);   // Serialized Object > Array
$parser->bson($payload);        // BSON > Array
$parser->msgpack($payload);     // MSGPack > Array

Parse Input/Payload (PUT/POST)

$parser = new Parser();
$parser->payload();                     // Auto Detect Type - 'Content Type' HTTP Header
$parser->payload('application/json');   // Specifiy the content type

Helper functions

$parser = new Parser();
$parser->all();                         // Return all values
$parser->has('key');                    // Does a key exist, with value.
$parser->get('key', 'default value');   // Get value by key, set an optional default.
$parser->only('id', 'name', 'email');   // Only return value from the selected keys.
$parser->except('password');            // Don't return values from the selected keys.
$parser->mask($mask);                   // Return masked values (see Mask Function, below).

Mask function

The mask function processes payload data using a configuration mask, thereby returning only a selected subset of the data. It works just like the only method but with the added benefit of allowing you to specify a mask in the form of an array, this means you can generate masks on-the-fly based on system and/or user defined conditions., (*12)

Demo
Mask

Defining the mask, masks consist of basic array structure, for this particular example we have some rules for the data to be returned they include: - the title of the post - all the body's for all the comments., (*13)

$mask = [
    'post' => [
        'title' => '*',
        'comments' => [
            'body' => '*'
        ]
    ]
];
Sample Payload
{
    "post": {
        "title": "Hello World",
        "author": "John Smith",
        "comments": [
            {"body": "This is a comment", "date": "2015-02-20"},
            {"body": "This is another comment", "date": "2015-05-09"}
        ]
    }
}
Applying the Mask
    $parser = new Parser();
    $output = $parser->mask($mask);
Output

This is the output generated as a result of applying the mask against the sample payload provided above., (*14)

$output = [
    'post' => [
        'title' => 'Hello World',
        'comments' => [
            ['body' => 'This is a comment'],
            ['body' => 'This is another comment']
        ]
    ]
];

Wildcards/Special Keys (*, %, :first, :last, :index[0], :item[0])

$parser = new Parser();
$parser->has('message.*');          // Does a key exist, with value. (Wildcard key returns first item found)
$parser->get('message.*');          // Get value by key. (Wildcard key returns first item found)
$parser->has('message.:first');     // Does a key exist, with value. (:first key returns first item found)
$parser->get('message.:first');     // Get value by key. (:first key returns first item found)
$parser->has('message.:last');      // Does a key exist, with value. (:last key returns last item found)
$parser->get('message.:last');      // Get value by key. (:last key returns last item found)
$parser->has('message.:index[0]');  // Does a key exist, with value. (:index[0] key returns item at index 0)
$parser->get('message.:index[0]');  // Get value by key. (:index[0] key returns item at index 0)
$parser->has('message.:item[0]');   // Does a key exist, with value. (:item[0] key returns item at index 0)
$parser->get('message.:item[0]');   // Get value by key. (:item[0] key returns item at index 0)

Parse JSON

$parser = new Parser();
$parsed = $parser->json('
    {
        "message": {
            "to": "Jack Smith",
            "from": "Jane Doe",
            "subject": "Hello World",
            "body": "Hello, whats going on..."
        }
    }');

Parse XML

$parser = new Parser();
$parsed = $parser->xml('
            <?xml version="1.0" encoding="UTF-8"?>
            <xml xmlns:ns="http://example.com/xmlns">
                <message status="sent">
                    <ns:meta hint="created">Created 5 minutes ago</ns:meta>
                    <to>Jack Smith</to>
                    <from>Jane Doe</from>
                    <subject>Hello World</subject>
                    <body>Hello, whats going on...</body>
                </message>
            </xml>');

Parse Query String

$parser = new Parser();
$parsed = $parser->querystr('to=Jack Smith&from=Jane Doe&subject=Hello World&body=Hello, whats going on...');

Parse Serialized Object

$parser = new Parser();
$parsed = $parser->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...";}}');

Parse YAML

$parser = new Parser();
$parsed = $parser->yaml('
                ---
                message:
                    to: "Jack Smith"
                    from: "Jane Doe"
                    subject: "Hello World"
                    body: "Hello, whats going on..."
                ');

Parse BSON

$parser = new Parser();
$parsed = $parser->bson('BSON DATA HERE');

Parse MSGPack

$parser = new Parser();
$parsed = $parser->msgpack('MSGPACK DATA HERE');

Custom Parsers/Formatters

You can make your own custom parsers/formatters by implementing FormatInterface, the below example demostrates the use of a custom parser/formatter., (*15)

use Nathanmac\Utilities\Parser\Formats\FormatInterface;

/**
 * Custom Formatter
 */

class CustomFormatter implements FormatInterface {
    /**
     * Parse Payload Data
     *
     * @param string $payload
     *
     * @return array
     *
     * @throws ParserException
     */
    public function parse($payload)
    {
        $payload; // Raw payload data

        $output = // Process raw payload data to array

        return $output; // return array parsed data
    }
}

Using the CustomFormatter

use Acme\Formatters\CustomFormatter;

$parser = new Parser();
$parsed = $parser->parse('RAW PAYLOAD DATA', new CustomFormatter());

Autodetecting the CustomFormatter

use Acme\Formatters\CustomFormatter;

$parser = new Parser();
$parser->registerFormat('application/x-custom-format', 'Acme\Formatters\CustomFormatter');
$parser->payload('application/x-custom-format');

Testing

To test the library itself, run the tests:, (*16)

composer test

Contributing

Please see CONTRIBUTING for details., (*17)

Credits

License

The MIT License (MIT). Please see License File for more information., (*18)

Appendix

Supported Content-Types
XML
---
application/xml > XML
text/xml > XML

JSON
----
application/json > JSON
application/x-javascript > JSON
text/javascript > JSON
text/x-javascript > JSON
text/x-json > JSON

YAML
----
text/yaml > YAML
text/x-yaml > YAML
application/yaml > YAML
application/x-yaml > YAML

BSON
----
application/bson > BSON

MSGPack
-------
application/msgpack > MSGPack
application/x-msgpack > MSGPack

MISC
----
application/vnd.php.serialized > Serialized Object
application/x-www-form-urlencoded' > Query String

The Versions

18/04 2018

dev-review-testing

dev-review-testing https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

12/06 2017

dev-master

9999999-dev https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

07/09 2016

v4.3

4.3.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

02/09 2016

v4.1

4.1.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

02/09 2016

v4.2

4.2.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

28/07 2016

v4.0

4.0.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

06/02 2016

v3.11

3.11.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

03/10 2015

v3.10

3.10.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

27/08 2015

v3.9

3.9.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

26/07 2015

v3.8

3.8.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

09/07 2015

v3.7

3.7.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

03/07 2015

v3.6

3.6.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

03/07 2015

v3.5

3.5.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

29/06 2015

v3.4

3.4.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

28/06 2015

v3.3

3.3.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

26/05 2015

v3.1

3.1.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

17/05 2015

v3.0

3.0.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

08/01 2015

v2.3

2.3.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

28/12 2014

v2.2

2.2.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

11/11 2014

v2.1

2.1.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

27/10 2014

v2.0

2.0.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api parser json xml

18/07 2014

v1.7

1.7.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

18/07 2014

v1.6

1.6.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

17/07 2014

v1.5

1.5.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

16/07 2014

v1.4

1.4.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

15/07 2014

v1.3

1.3.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

30/05 2014

v1.2

1.2.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

30/05 2014

v1.1

1.1.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml

29/05 2014

v1.0

1.0.0.0 https://github.com/nathanmac/Parser

Simple PHP Parser Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api parser json xml