2017 © Pedro Peláez
 

library object-dot-notation

Object dot notation for PHP with key existence validation

image

alrik11es/object-dot-notation

Object dot notation for PHP with key existence validation

  • Monday, April 16, 2018
  • by alrik11es
  • Repository
  • 1 Watchers
  • 3 Stars
  • 539 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 3 Open issues
  • 6 Versions
  • 16 % Grown

The README.md

alt tag Build Status, (*1)

The idea behind this library is to allow the access through
`config.port` dot notation to object data.

Why?

My main problem was when accessing API data., (*2)

{
    "hits":{
        "products": [
            {
                "name": "Shoe"
            }
        ]
    }
}

Imagine this is the result from an API. Usually to be sure that the data is what I want I'm gonna need to do:, (*3)

<?php
$result = r(); // imagine this is the result from an API with the json message abobe
if(is_object($result) && property_exists($result, 'hits')){
    if(is_object($result->hits) && property_exists($result->hits, 'products')){
        $whatiwant = $result->hits->products;
    }
}

This is really time consuming. I just needed a way to do something like:, (*4)

<?php
$d = \Alr\ObjectDotNotation\Data::load(r());
$whatiwant = $d->get('hits.products');

Note: In PHP7 you can just use $var = $something ?? $something2; but if you need to do this dynamically it becomes harder to do., (*5)

Demo

You can try a demo here, (*6)

Installation

Requires composer and PHP5.6+, (*7)

$ composer require alrik11es/object-dot-notation, (*8)

Usage

When the property is not found a null will be returned., (*9)

Take this object as example:, (*10)

{
    "config": {
        "port": "1234",
        "url": "testurl.com"
    }
}

Then:, (*11)

<?php
$d = \Alr\ObjectDotNotation\Data::load($mixed);
echo $d->get('config.port'); // 1234
echo $d->{'config.port'};
echo $d->config; // ['port'=>1234 ...]

For other kind of uses you're gonna need to get a position of an array or search and get the first value of array., (*12)

Take this object as example:, (*13)

{
    "config": [{
        "port": "80",
        "url": "aurl.com"
    },{
        "port": "90",
        "url": "burl.com"
    }]
}

You can use this way to access the information:, (*14)

<?php
$d = \Alr\ObjectDotNotation\Data::load($mixed);
echo $d->get('config[0].port'); // 80
echo $d->{'config[port=90|first].url'}; // burl.com

Filters

You can use filters for the array selection process., (*15)

[port=90|first], (*16)

IMPORTANT NOTE: Actually you can only use |first filter, use it always because it's going to be needed on future versions to avoid compatibility problems., (*17)

Advanced filters

TODO, (*18)

The Versions

16/04 2018

dev-master

9999999-dev

Object dot notation for PHP with key existence validation

  Sources   Download

MIT

The Development Requires

dot notation object

21/03 2018

1.0.4

1.0.4.0

Object dot notation for PHP with key existence validation

  Sources   Download

MIT

The Development Requires

dot notation object

25/07 2017

1.0.3

1.0.3.0

Object dot notation for PHP with key existence validation

  Sources   Download

MIT

The Development Requires

dot notation object

07/06 2017

1.0.2

1.0.2.0

Object dot notation for PHP with key existence validation

  Sources   Download

MIT

The Development Requires

dot notation object

06/06 2017

1.0.1

1.0.1.0

Object dot notation for PHP with key existence validation

  Sources   Download

MIT

The Development Requires

dot notation object

06/06 2017

1.0.0

1.0.0.0

Object dot notation for PHP with key existence validation

  Sources   Download

MIT

The Development Requires

dot notation object