承接 softboxlab/php-array-utils 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

softboxlab/php-array-utils

Composer 安装命令:

composer require softboxlab/php-array-utils

包简介

Lib with many array utilitaries.

README 文档

README

Build Status codecov Latest Stable Version Total Downloads Latest Unstable Version composer.lock SensioLabsInsight

Helps to cast values of array of array. Util when it is necessary garantee the type of output value, por exemple, encoding of arrays in JSON. Beyond cast values, it is possible format too.

Installation

composer require tarcisiojr/php-cast

Usage

Example:

class Example {
    public function test() {
        $value = [
            "a" => [
                "b" => [
                    "c1" => "a",
                    "c2" => "2",
                    "c3" => "3",
                    "c4" => ["1", "2", "3"],
                    "c5" => [
                        ["d1" => "9"],
                        ["d1" => "8"],
                        ["d1" => "7"],
                    ]
                ]
            ]
        ];
    
        // 1. Create a instance of CastHelper
        // 2. Configure the rules to cast values
        // 3. Execute method 'cast' to cast array values
        
        $ret = (new CastHelper())
            ->addRule("a.b.c1", "string")
            ->addRule("a.b.c2", "int")
            ->addRule("a.b.c3", "float")
            ->addRule("a.b.c4.*", "int")
            ->addRule("a.b.c5.*.d1", "string|lpad:2,0")
            ->cast($value);

        echo "\n\n" . json_encode($ret, JSON_PRETTY_PRINT);
    }
}

Output:

{
    "a": {
        "b": {
            "c1": "a",
            "c2": 2,
            "c3": 3,
            "c4": [
                1,
                2,
                3
            ],
            "c5": [
                {
                    "d1": "09"
                },
                {
                    "d1": "08"
                },
                {
                    "d1": "07"
                }
            ]
        }
    }
}

Path Expression

  • a.b selects "value":
    [ 
        "a" => [
            "b" => "value"
        ]
    ]; 
  • a.2.b selects "two":
    [ 
        "a" => [
            [ "b" => "zero" ],
            [ "b" => "one" ],
            [ "b" => "two" ],
            [ "b" => "three" ]
        ]
    ]; 
  • a.*.b selects all values "zero, "one", "two" and "three":
    [ 
        "a" => [
            [ "b" => "zero" ],
            [ "b" => "one" ],
            [ "b" => "two" ],
            [ "b" => "three" ]
        ]
    ]; 

Rule Expression

Expression: cast_type|option_1:param_1,param_2,...param_n;option_2:param_1...;option_n...

Where cast_type is the identifier of cast rule, options are aditional configurations do be executed against the value, for example, trim, pad, etc.

Cast Types:

  • int: casts to a int value.
  • float: casts to a float point value.
  • bool: casts to a boolean value.
  • string: casts to a string value.
    • max_length:size truncate the value at max length.
    • rpad:size,char pad at right position with supplied char.
    • lpad:size,char pad at left position with supplied char.

Extending

It is possible to add new cast rules, for this it will be necessary create a class the extends CastRule interface and register it with PHP\Cast\CastRule\CastRuleFactory::registerCastRule method. You can use the PHP\Cast\CastRule\CastRuleBase to shorten the path. See example above:

class BooleanCastRule extends PHP\Cast\CastRule\CastRuleBase {
    public function getIdentifier() {
        return "bool";
    }

    public function cast($value) {
        return (boolean) $value;
    }
} 

...

PHP\Cast\CastRule\CastRuleFactory::registerCastRule(new BooleanCastRule());

...

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固