cocur/vale 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

cocur/vale

Composer 安装命令:

composer require cocur/vale

包简介

Vale is a helper utility that lets you get and set values in arbitrary nested arrays and objects.

README 文档

README

Vale helps you working with complex data structures. Easily get, set, unset and check the existence of values in deeply nested arrays and objects.

Build Status Windows Build status Scrutinizer Code Quality Code Coverage StyleCI

Developed by Florian Eckerstorfer in Vienna, Europe.

Features

  • Get, set, unset and check the existence of values in deeply nested arrays and objects
  • Works with arbitrary arrays and objects and any combination of them
  • Uses getters, setters, unsetters, hassers and issers in objects
$name = Vale::get($families, ['lannister', 'leader', 'children', 2, 'name']);

// This would be equal to the following
$name = null;
if (isset($families['lannister']) && $families['lannister']) {
    if ($families['lannister']->getLeader()) {
        if (isset($families['lannister']->getLeader()->children[2]) && $families['lannister']->getLeader()->children[2]) {
            $name = $families['lannister']->getLeader()->children[2]->name();
        }
    }
}

Installation

You can install Vale using Composer:

$ composer require cocur/vale

Usage

You can use either the static methods provided by Vale or create an instance of Vale.

use Cocur\Vale\Vale;

$data = ['name' => 'Tyrion'];
Vale::get($data, ['name']); // -> "Tyrion"
Vale::set($data, ['name'], 'Cersei'); // -> ["name" => "Cersei"]
Vale::has($data, ['name']); // -> true
Vale::remove($data, ['name']); // -> []

$vale = new Vale();
$vale->getValue($data, ['name']); // -> "Tyrion"
$vale->setValue($data, ['name'], 'Cersei'); // -> ["name" => "Cersei"]
$vale->hasValue($data, ['name']); // -> true
$vale->removeValue($data, ['name']); // -> []

For flat arrays and objects (that is, arrays and objects with only one level of depth) you can also use a string or integer as key. This works for the static as well as the instance methods.

Vale::get(['name' => 'Tyrion'], 'name'); // -> "Tyrion"
Vale::get(['Tyrion'], 0); // -> "Tyrion"

Get

::get() and ->getValue() return the value of a specified element.

mixed get(mixed $data, array|string|int $keys, mixed $default = null)
mixed getValue(mixed $data, array|string|int $keys, mixed $default = null)
  • $data is an arbitrary data structure
  • $keys is an array of keys to access the value. If the length is 1, $keys can be a string or int
  • $default is the default value that is returned if the value does not exist in $data

Returns the element at the given position or the original $data if $keys is empty.

Vale tries different ways to access the element specified in $keys. The following variants are tried in this order:

  1. $data[$key]
  2. $data->$key()
  3. $data->get$Key()
  4. $data->get($key)
  5. $data->has$Key()
  6. $data->has($key)
  7. $data->is$Key()
  8. $data->is($key)
  9. $data->$key

Set

::set() and ->setValue() set the value of an element at the given position.

mixed set(mixed $data, array|string|int $keys, mixed $value)
mixed setValue(mixed $data, array|string|int $keys, mixed $value)
  • $data is an arbitrary data structure
  • $keys is an array of keys to access the value. If the length is 1, $keys can be a string or int
  • $value is the value for the element

Returns the modified $data

Set utilizes the same means of navigating through nested data structures as Get and tries the following variants to set the value:

  1. $data[$key] = $value
  2. $data->$key($value)
  3. $data->set$Key($value)
  4. $data->set($key, $value)
  5. $data->$key = $value

Has

::has() and ->hasValue() returns if an element exists

bool has(mixed $data, array|string|int $keys)
bool hasValue(mixed $data, array|string|int $keys)
  • $data is an arbitrary data structure
  • $keys is an array of keys to access the value. If the length is 1, $keys can be a string or int

Returns true if the element exists, false otherwise.

Has utilizes the same means of navigating through nested data structures as Get and tries the following variants to check the existence of an element:

  1. isset($data[$key])
  2. isset($data->$key)
  3. $data->has$Key()
  4. $data->has($key)
  5. $data->is$Key()
  6. $data->is($key)
  7. $data->$key()
  8. $data->get$Key()

The variants involving a method call (such as has$Key() or has()) return true if the method returns true or a value that evaluates to true. If the method returns a value that evaluates to false (such as '', 0 or null) then has returns false.

Remove

::remove() and ->removeValue() remove an element from the given data structure

mixed remove(mixed $data, array|string|int $keys)
mixed removeValue(mixed $data, array|string|int $keys)
  • $data is an arbitrary data structure
  • $keys is an array of keys to access the value. If the length is 1, $keys can be a string or int

Returns the modified $data or null if $keys is empty

Remove utilizes the same means of navigating through nested data structures as Get and tries the following variants to remove the element from the data structure:

  1. unset($data[$key])
  2. unset($data->$key)
  3. $data->unset$Key()
  4. $data->remove$Key()
  5. $data->remove($key)

Please note that unset() is not used, because it is an reserved keyword in PHP.

Change Log

Version 0.2 (24 March 2015)

  • Add has() method to check if key exists
  • Add remove() method to remove key from item
  • Improved navigating through complex structures
  • Major refactoring, making the code more reusable and testable

Version 0.1 (15 March 2015)

  • Initial release

Motivation

Vale was largely motivated by the need for a simpler, but faster implementation of the Symfony PropertyAccess component. PropertyAccess is great when used in templates or config files, that is, code that is compiled and cached before being executed. However, the heavy use of string parsing and reflection make PropertyAccess not suitable for code that is not compiled. Another source of inspiration was the get-in library by Igor Wiedler for array traversal.

Name: I used A Song of Ice and Fire related strings for testing and due to having to write value quite often, I came up with Vale.

Author

Vale has been developed by Florian Eckerstorfer (Twitter) in Vienna, Europe.

Vale is a project of Cocur. You can contact us on Twitter: @cocurco

License

The MIT license applies to Vale. For the full copyright and license information, please view the LICENSE file distributed with this source code.

cocur/vale 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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