revinate/php-getter-setter
Composer 安装命令:
composer require revinate/php-getter-setter
包简介
PHP library to simplify getting and setting values in arrays and objects.
README 文档
README
PHP Library for simplifying getting and setting values in arrays or objects
Summary
At its core, this library is composed of 3 function pairs:
get/set-- functions to get and set nested values in an array, object, or class given a field name using dot notationgetValue/setValue-- functions to get and set values in an array, object, or class.getValueByArrayPath/setValueByArrayPath-- used bygetandsetto access the nested values by specifying the path in an array.
Instalation
Use Composer:
"require": { "revinate/php-getter-setter": "~0.2" },
Usage
To make things easier, include the follow use statement at the top of your files:
use Revinate\GetterSetter as gs;
get and set
Getting a Value
$name = gs\get($data, 'name');
Getting a Value with a default
$name = gs\get($data, 'count', 0);
Setting a Value
$updatedData = gs\set($data, 'count', 42);
Here is an example unit test to give a bit more context.
public function testExampleJson() { $json = '{"name":"example","type":"json","value":22}'; $data = json_decode($json); $name = gs\get($data, 'name'); $missing = gs\get($data, 'missing'); $this->assertEquals('example', $name); $this->assertNull($missing); }
Differences between objects and arrays
With setValue, an object will get updated when a field value is set. But it is different with arrays because they are immutable. Only the array returned from setValue will have the updated fields. Watch out for ArrayObjects, they will get updated just like normal objects.
Example Unit Test showing difference between objects and arrays:
public function testExampleArrayVsObject() { $json = '{"name":"example","type":"json","value":22}'; $object = json_decode($json); $array = (array)$object; // The object gets updated as well $newObject = gs\set($object, 'type', 'Object'); // Only the new array contains the update. $newArray = gs\set($array, 'type', 'Array'); $this->assertEquals($object, $newObject); $this->assertNotEquals($array, $newArray); }
Getting and Setting Nested Values
getPathValue and setPathValue provide an easy shortcut for getting and setting nested values.
{
"first_name" : "Joe",
"last_name" : "Rock",
"address" : {
"street":"1 Main St.",
"city":"Little Rock",
"state":"Arkansas"
},
"profession":"Stone cutter"
}
Example accessing:
$json = $this->getEmployeeJsonData(); $data = json_decode($json); $this->assertEquals('Arkansas', gs\get($data, 'address.state')); $this->assertNull(gs\get($data, 'address.zip'));
The notation is longer than $data->address->state, but it will not blow up where this will: $data->address->zip.
Support for Getters, Setters, Has'ers, and Is'ers.
These functions support getters, setters and magic methods use by many ORM systems like Doctrine.
Support for Magic methods like __get and __set
Example Data Class
class MagicAccessTestClass { protected $values = array(); function __get($name) { return $this->values[$name]; } function __isset($name) { return isset($this->values[$name]); } function __set($name, $value) { $this->values[$name] = $value; } }
Sample Usage:
public function testMagicMethods() { $data = new MagicAccessTestClass(); $data->first_name = 'Joe'; $data->last_name = 'Rock'; gs\set($data, 'profession', 'Stone cutter'); gs\set($data, 'address', new MagicAccessTestClass()); gs\setPathValue($data, 'address.city', 'Little Rock'); $this->assertEquals('Joe', gs\get($data, 'first_name')); $this->assertEquals('Rock', gs\get($data, 'last_name')); $this->assertEquals('Little Rock', gs\get($data, 'address.city')); }
revinate/php-getter-setter 适用场景与选型建议
revinate/php-getter-setter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.05k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 09 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 revinate/php-getter-setter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 revinate/php-getter-setter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 32.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-15