2017 © Pedro Peláez
 

library selector

Extract information from JSON, StdClass in a fashionable way

image

cabello/selector

Extract information from JSON, StdClass in a fashionable way

  • Friday, September 27, 2013
  • by danilo.cabello
  • Repository
  • 2 Watchers
  • 8 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Selector Build Status

Disclaimer

If you found this project but think it's incomplete or doesn't meet your needs, try Property Access., (*1)

Problem

Imagine that you have a StdClass (possibly created by a json_decode) instance full of attributes you want to retrieve. So you do something like this:, (*2)

$username = $photo->owner->username;

What happens if username is undefined and/or owner?, (*3)

$username = isset($photo) && isset($photo->owner) && isset($photo->owner->username)
          ? $photo->owner->username
          : 'anonymous';

Imagine this kind of logic spread all over your codebase. What a mess!, (*4)

Solution

Selector turns the horrible code above into this:, (*5)

$photoSelector = Selector($photo);
$username      = $photoSelector->getOne('owner.username', 'anonymous');

You never have to worry again about checking if the StdClass have the properties you need, and as a plus you receive several ways of retrieving data., (*6)

Features

Selector

Selector::getOne

Description

Parameters

Return Values

Examples

Example #1

The above example will output:, (*7)

Example #2

The above example will output:, (*8)

Selector::getAll

Description

Parameters

Return Values

Examples

Example #1

The above example will output:, (*9)

Example #2

The above example will output:, (*10)

Selector::findOne

Description

Parameters

Return Values

Examples

Example #1

The above example will output:, (*11)

Example #2

The above example will output:, (*12)

Selector::findAll

Description

Parameters

Return Values

Examples

Example #1

The above example will output:, (*13)

Example #2

The above example will output:, (*14)

Selector::getDictionary

Description

Parameters

Return Values

Examples

Example #1

The above example will output:, (*15)

Example #2

The above example will output:, (*16)

Developer

Assuming you have composer.phar installed, it's simple to contribute to Selector, fork, clone your repository and run:, (*17)

cd selector # your clone folder
composer.phar install --dev
vendor/bin/phpunit tests

And you are ready to write new tests, contributions and sending pull requests. :octocat:, (*18)

The Versions

27/09 2013

dev-master

9999999-dev http://jmsyst.com/libs/serializer

Extract information from JSON, StdClass in a fashionable way

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3

 

The Development Requires

by Danilo Cabello
by Rafael Martins

json extract stdclass