定制 graste/params 二次开发

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

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

graste/params

Composer 安装命令:

composer require graste/params

包简介

Array wrapper that eases the retrieval of values. Has parameters, options and settings and traits for inclusion in other libraries.

README 文档

README

Latest-Stable-Version License Latest Unstable Version Build Status Stories in Ready Total Composer Downloads

Purpose

Array wrapper object that eases the retrieval of values. It provides a get method that can return a default value if the given key is missing. In addition to the usage as a normal array and the various get, set, has etc. methods Parameters defines a getValues($expression) method that allows the retrieval of values by providing more or less complex expressions. You can easily retrieve values from nested arrays or get several values from different nesting levels with one call. There are traits for convenient creation of configurable classes.

Requirements and installation

  • PHP ^7.0

Install the library via Composer:

./composer.phar require graste/params [optional version]

Adding it manually as a vendor library requirement to the composer.json file of your project works as well:

{
    "require": {
        "graste/params": "^3.0.0"
    }
}

Alternatively, you can download a release archive from the releases.

Documentation and usage

$data = array(
    'str' => 'some string',
    'first_level' => 'first level',
    'nested' => array(
        'str' => 'some nested string',
        '2nd level' => 'second level',
    ),
    'more' => array(
        'str' => 'other nested string',
    )
);

// base object of the aliased versions:
$arrayobj = new \Params\ConfigurableArrayObject($array);

// create mutable parameters instance:
$params = new \Params\Parameters($data);
// or initialize an immutable instance that prevents further modifications after construction
$params = new \Params\Immutable\ImmutableParameters($data);

// do you like options instead of parameters in your classes?
$params = new \Params\Options($data);
$params = new \Params\Immutable\ImmutableOptions($data);

// do you like settings instead of parameters in your classes?
$params = new \Params\Settings($data);
$params = new \Params\Immutable\ImmutableSettings($data);

// use it as a recursive object:

$params->has("foo")                           // returns true now
$params->get("str")                           // gives "some string"
$params->get("nested")                        // gives array stored under "nested" key
$params->get("nested")->get('str')            // gives "some nested string"
$params->get("non-existant", "default value") // gives "default value" as given key is non existant
$params->get("nested")->set("foo", "bar")     // sets key "foo" to value "bar" on the "nested" array
$params->getKeys()                            // returns all first level keys
$params->toArray()                            // returns internal array

// or use the mutable methods
$params->set("foo", "bar")                    // sets key "foo" to value "bar"
$params->add(array|ArrayAccess)               // add array or other ArrayAccess implementing object to current instance
$params->clear()                              // empty internal array
$params->map(function($key, $value) { … })    // modifies each value to the value returned by the callback

// retrieve values using expressions

$params->getValues("foo")                               // gives "bar"
$params->getValues("nested.str")                        // gives "some nested string"
$params->getValues('*.str')                             // gives array("some nested string", "other nested string")
$params->getValues('[str, nested.str]')                 // gives array("some string", "some nested string")
$params->getValues('nested."2nd level" || first_level') // gives "second level" as that key exists; other expression not evaluated
$params->getValues('first_level || nested."2nd level"') // gives "first level" as that key exists; other expression not evaluated

// use it as an array:

$params["str"]    // gives "some string"
$params["nested"] // gives the array under the "nested" key
$params[1]        // gives "first level"

// use it as an object with properties

$params->foo = 'bar'          // sets key 'foo' to value 'bar'
$params->filter->bool = 'yes' // sets $params['filter']['bool'] to value 'yes'

The expression syntax used is provided by Michael Dowling's JMESPath.

Traits

There are Traits may be used for own configurable classes:

  • ImmutableParametersTrait wraps parameters
  • ParametersTrait wraps parameters
  • ImmutableOptionsTrait wraps options
  • OptionsTrait wraps options
  • ImmutableSettingsTrait wraps settings
  • SettingsTrait wraps settings

For fluent API support the methods add, set, remove and clear return the class instance they're mixed into.

ElasticSearch queries

The syntax sugar Parameters provides is not only nice to define configurable classes, but also eases the creation and modification of ElasticSearch queries:

$params->filter->bool->must[1]->term->live = false;
$params->get('filter')->set('bool', …);
$params->filter->bool->must[] = array(…);

Contribution

Please contribute by forking and sending a pull request. More information can be found in the CONTRIBUTING.md file.

To develop on this repository clone your fork and use the Makefile targets:

  • make install installs composer and all necessary vendor libraries
  • make tests runs the phpunit tests
  • make code-sniffer-cli runs the code sniffer on console

The code tries to adhere to the following PHP-FIG standards: PSR-4, PSR-1 and PSR-2. The current license is MIT.

The authors and contributors are mentioned in the github contributors graph of this repository. The license file must be left intact for attribution and sharing of this work.

Changelog

See CHANGELOG.md for more information about changes.

graste/params 适用场景与选型建议

graste/params 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.99k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2014 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「library」 「search」 「options」 「array」 「Settings」 「parameters」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-06-25