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)