Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behaviour #41

Closed
XavRsl opened this issue Jan 30, 2017 · 2 comments
Closed

Weird behaviour #41

XavRsl opened this issue Jan 30, 2017 · 2 comments

Comments

@XavRsl
Copy link

XavRsl commented Jan 30, 2017

Hello,
I've been playing around with this package but can't make it behave the way I'm expecting to.
I'm using Laravel 5.4 and PHP 7.1 (this setup could be the reason of my problem...). I'm using the very simple code example :

Parser::json('{
            "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"}
                ]
            }
        }');
dd(Parser::has('post.title'));

This returns false....

$parser = new \Nathanmac\Utilities\Parser\Parser();
    $parser->json('{
            "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"}
                ]
            }
        }');
dd($parser->has('post.title'));

False again...

$parser = new \Nathanmac\Utilities\Parser\Parser();
    $parsed = $parser->json('{
            "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"}
                ]
            }
        }');
dd($parsed);

Here the output is what I'm expecting. Tried ->all(), ->get(), ... Even tried mask but the output is always empty.

I'm confused. Any idea ?

Thanks,

Xavier

@danhunsaker
Copy link
Contributor

So, in the first example, your first statement is parsing the JSON, but not storing the result anywhere, and then the second statement is checking the request body (pulled from STDIN) for 'post.title', which doesn't exist.

In your second example, the first statement creates a Parser object, but then your second and third statements do the same things as the two statements in your first example.

Your third example shows what's expected because you're actually saving the parsed output in the second statement.

Parser::json() parses a JSON payload (much like Parser::xml() parses an XML payload, and so forth), and returns its value, much like json_decode() might. Parser::has() (like the other helper methods of Parser) operates directly on the request body, which in this case is probably empty. In other words, you can't actually use Parser's helper methods on any payload you don't get from the request body.

But, since you're using Laravel 5, you should be able to use its Arr methods on the output of Parser::json($payload) instead. Indeed, Arr has quite a lot more methods at its disposal than Parser does in the first place, so you can do a number of fancier things with the parsed data.

@XavRsl
Copy link
Author

XavRsl commented Jan 30, 2017

OK. Thanks for your answer and your time. Maybe some more docs would help newcomers understand the package a bit more. Thanks for your work.

Xavier

@XavRsl XavRsl closed this as completed Jan 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants