2017 © Pedro Peláez
 

library nestis

A simple php class that allows for the retrieval of nested object without null checks

image

webdevvie/nestis

A simple php class that allows for the retrieval of nested object without null checks

  • Monday, February 19, 2018
  • by webdevvie
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,565 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 22 % Grown

The README.md

Nestis

A simple class to get a nested property or array key from an array or nested property., (*1)

Why

Having gone through several weeks of is_null($object) checks and not being able to reliably depend on the api's output I was using to always have the objects I decided this method needed to be made. Now I can do the following without the fear of running into a null or other random object that the api responds with:, (*2)

public function getThatThingIWant($apiResponse)
{
    return $nestis->getNestedItem('someObject/someOtherProperty/itemIWant',$apiResponse);
}

instead of :, (*3)

public function getThatThingIWant($apiResponse)
{
   $someObject = $apiResponse->getSomeObject();

   if(!is_null($someObject))
   {
        $someOtherProperty = $someObject->getSomeOtherProperty();
        if(!is_null($someOtherProperty))
        {
            return $someOtherProperty->getItemIWant();
        }
   }

   return null;
}

It works on arrays,objects,public properties, public methods,get{{propertyname}} and is{{propertyname}} and public static properties, (*4)

Separator is / For static properties use ::{yourvarname} (e.g. testItem/::someStaticVar), (*5)

Also works great with json objects., (*6)

Using it in your project

First add it to your project using composer, (*7)

./composer require webdevvie/nestis

In your project use the class., (*8)

use Webdevvie\Nestis;

then try it out!, (*9)


use Webdevvie\Nestis\Nestis; $nested = (object)["test"=>(object)["layer1"=>['layer2'=>(object)['layer3'=>'downtherabbithole']]]]; print_r($nested); $nestis = new Nestis(); $item = $nestis->getNestedItem('test/layer1/layer2',$nested,null); print_r($item);

This will output:, (*10)

stdClass Object
(
    [test] => stdClass Object
        (
            [layer1] => Array
                (
                    [layer2] => stdClass Object
                        (
                            [layer3] => downtherabbithole
                        )

                )

        )

)
stdClass Object
(
    [layer3] => downtherabbithole
)

Author

If you like this library. Find me on twitter @webdevvie or my personal site johnbakker.name and say hello, (*11)

The Versions

19/02 2018

dev-master

9999999-dev

A simple php class that allows for the retrieval of nested object without null checks

  Sources   Download

Apache-2.0 Apache 2.0

The Requires

  • php >=5.6.0

 

The Development Requires

object nested bystring

01/07 2016

1.0.3

1.0.3.0

A simple php class that allows for the retrieval of nested object without null checks

  Sources   Download

Apache 2.0

The Requires

  • php >=5.6.0

 

The Development Requires

object nested bystring

29/06 2016

1.0.2

1.0.2.0

A simple php class that allows for the retrieval of nested object without null checks

  Sources   Download

Apache 2.0

The Requires

  • php >=5.6.0

 

The Development Requires

object nested bystring

29/06 2016

1.0.1

1.0.1.0

A simple php class that allows for the retrieval of nested object without null checks

  Sources   Download

Apache 2.0

The Requires

  • php >=5.4.0

 

The Development Requires

object nested bystring

29/06 2016

1.0

1.0.0.0

A php class that allows for

  Sources   Download

Apache 2.0

The Requires

  • php >=5.4.0

 

The Development Requires

object nested bystring