定制 avris/bag 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

avris/bag

Composer 安装命令:

composer require avris/bag

包简介

Nicer arrays for PHP

README 文档

README

Avris Bag is a set of helpers that make working with arrays in PHP way easier.

Instalation

composer require avris/bag

Bag

Bag class represents a key-value store. It can be used both as an array and as an object, leading to a shorter and nicer code:

$array = [
    'foo' => 'bar',
    'lorem' => [
    	'ipsum' => ['dolor', 'sit', 'amet']
    ],
];

$bag = new Bag($array);

// all return 'bar'
var_dump(isset($array['foo']) ? $array['foo'] : null);
var_dump($bag->get('foo')); 
var_dump($bag['foo']); 
var_dump($bag('foo')); 

// all return null
var_dump(isset($array['nonexistent']) ? $array['nonexistent'] : null);
var_dump($bag->get('nonexistent')); 
var_dump($bag['nonexistent']); 
var_dump($bag('nonexistent')); 

// all return 'default'
var_dump(isset($array['nonexistent']) ? $array['nonexistent'] : 'default');
var_dump($bag->get('nonexistent', 'default')); 
var_dump($bag['nonexistent'] ?: 'default'); 
var_dump($bag('nonexistent', 'default')); 

// all are equivalent
$array['x'] => 'y';
$bag->set('x', 'y');
$bag['x'] = 'y';

// all are equivalent
var_dump(count($array));
var_dump($bag->count());
var_dump(count($bag));

// all are equivalent
var_dump(count($array) === 0);
var_dump($bag->isEmpty());
var_dump(count($bag) === 0);

// all are equivalent
var_dump(array_keys($array));
var_dump($bag->keys());
var_dump(array_keys($bag));

// all are equivalent
var_dump(isset($array['foo']));
var_dump($bag->has('foo'));
var_dump(isset($bag['foo']));

// all are equivalent
unset($array['foo']);
$bag->delete('foo');
unset($bag['foo']);
	
// all are equivalent
$array = [];
$bag->clear();

Just like with a simple array, you can also iterate over a Bag and json_encode it.

Additional features include:

  • $bag->getDeep($key, $default = null) -- gets a value from a nested tree, using dot-separated string as a key, for instance $bag->getDeep('lorem.ipsum.1') will return 'sit';
  • $bag->add(array) -- merges the other array into the Bag, without overwriting the existing keys;
  • $bag->replace(array) -- merges the other array into the Bag, with overwriting the existing keys;
  • $bag->appendToElement($key, $value) -- treats $bag[$key] as a list and appends $value at its end;
  • $bag->prependToElement($key, $value) -- treats $bag[$key] as a list and prepends $value at its beginning;
  • $bag->map(function ($key, $value) {}) -- returns a new Bag, with values mapped by the callback function;
  • $bag->filter(function ($key, $value) {}) -- returns a new Bag, with only those values from the original Bag, for which the callback function returns true.
  • $bag->flatten() -- returns a new Bag, with keys flattened, for instance lorem.ipsum.

Set

Set is a similar structure, but it doesn't care about the keys and it makes sure that all the values are unique. Sometimes it's helpful to instantiate it with a callback:

$set = new Set(['post', 'get', 'GET'], 'strtoupper');
// internal values are: 'POST' and 'GET'

var_dump($set->has('post')); // true
var_dump($set->has('POST')); // true
var_dump($set->has('DELETE')); // false

Methods:

  • $set->add($value)
  • $set->addMultiple(array $values)
  • $set->has($value)
  • $set->delete($value)
  • $set->first()
  • $set->last()
  • $set->clear()

BagHelper

This class provides four static methods:

  • BagHelper::isArray($object) -- checks if $object is an array or array-like (\Traversable);
  • BagHelper::toArray($object) -- converts $object to an array;
  • BagHelper::magicGetter($object, $attr, $default = null) -- tries to get a value from object, using:
    • $object['key'],
    • $object->key(),
    • $object->getKey(),
    • $object->key
    • $object->get('key')
    • or fallbacks to $default.
  • BagHelper::magicSetter($object, $attr, $value) -- tries to set a value to object, using:
    • $object['key'] = $value,
    • $object->setKey($value),
    • $object->key = $value,
    • $object->set($key, $value)

Nested

This class provides two static methods for a nested access to arrays and objects:

  • Nested::get($object, array $keys, $default = null),
  • Nested::set(array &$array, array $keys, $value) (only arrays are supported).

For example:

Nested::get($container, ['github', 'webhooks', 'foo']);

could return a value of:

$container->get('github')->getWebhooks()['foo'];

unless any of the chain elements doesn't exist -- then $default is returned.

Similarly:

Nested::set($array, ['foo', 'bar', 'baz'], 8);

is in equivalent to:

$array['foo']['bar']['baz'] = 8;

but it creates all the arrays on the way, if they don't exist yet.

QueueBag

This class works like a normal queue, but lets you enqueue and dequeue a key-value pair and gives you standard Bag features.

$queue = new QueueBag();
$queue->enqueue('key', 'value')
$queue->enqueue('foo', 'bar')
list($key, $value) = $queue->dequeue()

StackBag

This class works like a normal stack, but lets you push and pop a key-value pair and gives you standard Bag features.

$stack = new StackBag();
$stack->push('key', 'value')
$stack->push('foo', 'bar')
list($key, $value) = $stack->pop()

Copyright

avris/bag 适用场景与选型建议

avris/bag 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.54k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 avris/bag 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 avris/bag 我们能提供哪些服务?
定制开发 / 二次开发

基于 avris/bag 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-03