proteins/dictionary
Composer 安装命令:
composer require proteins/dictionary
包简介
Class to handle a repository of key-values data via dot notation key path
README 文档
README
Protein | Dictionary
Handle a repository of key-values data
Install
composer require proteins/dictionary
Require the class via :
use Proteins\Dictionary;
Building a Dictionary
Dictionary is a behaviour class, it must be extended by another class or the value repository will be shared.
class State extends Dictionary {}
Setting a value
You can set a value from a key path via the get method.
A valid key path is a arbitrary deep sequence of . separated strings.
Examples
testalpha.betapages.section.text_block.3
State::set('options.use_cache',false); State::set('users.whitelist',[ 'frank.ciccio', 'walter.submarine', 'pepen.spacca', ]);
Getting a value
You can get a value from a key path via the get method.
echo State::get('users.whitelist.1'); // walter.submarine
You can optionally pass a default value to be returned when the requested key is not found. If a callable is passed the returned value will be used.
print_r( State::get('a.test',['b'=>123]) ); // Array( [b] => 123 ) echo State::get('a.test.b'); // 123
Getting all values
You can get all key-values as an associative array via the all method.
$all_data = State::all();
Results :
Array ( [users] => Array ( [whitelist] => Array( [0] => frank.ciccio [1] => walter.submarine [2] => pepen.spacca ) ) )
Clearing the Map
You can clear all values from a Map via the clear method.
State::clear();
Merging data
The merge method extends the Map with values passed via an associative array. The second optional parameter will define the if merge data from right-to-left or backwise (default is false = left-to-right ).
Setting initial data
State::clear(); State::merge([ 'user' => [ 'name' => 'Simon', 'role' => 'Villain', ], ]);
Array ( [user] => Array ( [name] => Simon [role] => Villain ) )
Standard merge (left-to-right)
State::merge([ 'user' => [ 'name' => 'Frank', ], 'happy' => true, ]);
Array ( [user] => Array ( [name] => Frank [role] => Villain ) [happy] => 1 )
Back merge (right-to-left)
State::merge([ 'user' => [ 'name' => 'Frank', ], 'happy' => true, ],true);
Array ( [user] => Array ( [name] => Simon [role] => Villain ) [happy] => 1 )
Getting multiple values
You can retrieve multiple values, minimizing function calls by passing an associative array of type :
DESTINATION_KEY => Map_PATH
Example :
MyService::init([
'username' => State::get('aws.username'),
'password' => State::get('aws.password'),
'from' => State::get('user.email'),
'verbose' => State::get('app.global.debug'),
]);
Can be written with a single call to get as :
MyService::init( State::get([
'username' => 'aws.username',
'password' => 'aws.password',
'from' => 'user.email',
'verbose' => 'app.global.debug',
]));
proteins/dictionary 适用场景与选型建议
proteins/dictionary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 proteins/dictionary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 proteins/dictionary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-29
