2017 © Pedro Peláez
 

library matter

Proper object traversal

image

belt/matter

Proper object traversal

  • Saturday, October 25, 2014
  • by belt
  • Repository
  • 1 Watchers
  • 2 Stars
  • 221 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Belt.Matter

Latest Version Software License Build Status Coverage Status Quality Score, (*1)

Proper object traversal, (*2)

Belt.Matter is an utility library that makes working with JSON objects a bit (actually a lot) more pleasant. It converts JSON strings, objects or arrays to a proper tree structure that can be traversed without warnings or errors., (*3)

Installation

Via Composer., (*4)

$ composer require belt/matter

Usage

When we use JSON in PHP, we usually have to use the properties of the JSON object. This is really nice and all, but it doesn't work when a property does not exist., (*5)

$object = json_decode('{"foo":"bar","bar":[1, 2, 3, 4]","baz":"lorem ipsum"}');
$object->foo;       // "bar"
$object->something; // BOOM!

Well, that's not really nice, is it? We have to put guards around everytime we want to use a property., (*6)

This is where Matter comes in handy!, (*7)

// Before
$object = json_decode('{"foo":"bar"}');
$object->foo;       // "bar"
$object->something; // BOOM!

// After
use Belt\Matter;

$object = Matter::fromJson('{"foo":"bar"}');
$object->get('foo')->get();         // "bar"
$object->get('something')->get();   // null

What Matter does for you, is convert a given JSON string or value into a tree structure. This allows you to request nodes from the tree without being afraid of errors/warnings that are thrown., (*8)

You can always access any value in the JSON object., (*9)

$object = Matter::fromJson('{"foo":[1, 2, 3, 4],"bar":"lorem"}');
$object->get('foo')->get(0)->get(); // 1
$object->get('foo')->get(4)->get(); // null
$object->get('bar')->get();         // "lorem"

$object = Matter::fromJson('[{"name":"Alice"},{"name":"Bob"}]');
$object->get(0)->get('name')->get(); // "Alice"
$object->get(1)->get('name')->get(); // "Bob"
$object->get(2)->get('name')->get(); // null

Each Matter node also implements ArrayAccess and Iterator, which allows you to work even faster!, (*10)

$users = Matter::fromJson('[{"name":"Alice"},{"name":"Bob"}]');
$users[0]['name']->get(); // "Alice"
$users[1]['name']->get(); // "Bob"
$users[2]['name']->get(); // null

foreach ($users as $user) {
    $name = $user['name']->get();
}

Additionally, you can also safely access properties of the object without having to use get to retrieve the property node., (*11)

$object = Matter::fromJson('{"foo":{"bar":{"baz":42}}}');
$object->foo->bar->baz->get(); // 42
$object->baz->bar->foo->get(); // null

Contributing

Please see CONTRIBUTING., (*12)

License

Please see LICENSE., (*13)

The Versions

25/10 2014

dev-master

9999999-dev https://github.com/beltphp/matter

Proper object traversal

  Sources   Download

BSD-2-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

json tree toolkit

25/10 2014

v0.1

0.1.0.0 https://github.com/beltphp/matter

Proper object traversal

  Sources   Download

BSD-2-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

json tree toolkit