承接 belt/matter 相关项目开发

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

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

belt/matter

最新稳定版本:v0.1

Composer 安装命令:

composer require belt/matter

包简介

Proper object traversal

README 文档

README

Latest Version Software License Build Status Coverage Status Quality Score

Proper object traversal

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.

Installation

Via Composer.

$ 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.

$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.

This is where Matter comes in handy!

// 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.

You can always access any value in the JSON object.

$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!

$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.

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

Contributing

Please see CONTRIBUTING.

License

Please see LICENSE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2014-10-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固