承接 webdevvie/nestis 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webdevvie/nestis

最新稳定版本:1.2.0

Composer 安装命令:

composer require webdevvie/nestis

包简介

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

README 文档

README

A simple class to get a nested property or array key from an array or nested property.

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:

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

instead of :

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

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

Also works great with json objects.

Using it in your project

First add it to your project using composer

./composer require webdevvie/nestis

In your project use the class.

use Webdevvie\Nestis;

then try it out!


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:

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

统计信息

  • 总下载量: 9.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2016-06-29