定制 aedart/array-option 二次开发

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

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

aedart/array-option

最新稳定版本:1.1.0

Composer 安装命令:

composer require aedart/array-option

包简介

Allows you to defined a set of options (array key-value pair), which must follow a set of predefined rules (allowed options)

README 文档

README

Allows you to defined a set of options (array key-value pair), which must follow a set of predefined rules (allowed options)

Official website (https://bitbucket.org/aedart/array-option)

Contents

[TOC]

When to use this

If your component, class or method accepts options, in the form of an array (key-value pair) and you need to perform some kind of basic validation of those options, e.g. are required, data-type check and eventually also have a predefined default value.

Warning I advise you NOT to use this component, unless you really cannot specify method arguments in any other way, than in the form of an associative array.

How to install

#!console

composer require aedart/array-option 1.* 

This package uses composer. If you do not know what that is or how it works, I recommend that you read a little about, before attempting to use this package.

Quick start

The blow stated example show how to build a component, which accepts 3 options, each having a default value and a specific data-type.

#!php
<?php
use Aedart\ArrayOption\Traits\ArrayOptionsListTrait;
use Aedart\ArrayOption\AllowedOptionsList;
use Aedart\ArrayOption\Interfaces\IOptionType;

class MyComponent {

    use ArrayOptionsListTrait;
    
    public function __construct(){

        // Specify the allowed options
        // See Aedart\ArrayOption\Interfaces\IAllowedOption
        $allowedOptions = new AllowedOptionsList([
            [
                'name' => 'myOption',
                'type' => IOptionType::TYPE_STRING,
                'required' => true,
                'defaultValue'  => 'doIt',
                'description' => 'A single option with do-it'
            ],
            [
                'name' => 'myOtherOption',
                'type' => IOptionType::TYPE_INTEGER,
                'required' => false,
                'defaultValue'  => 27,
                'description' => 'Another custom option'
            ],
            [
                'name' => 'LastOption',
                'type' => IOptionType::TYPE_ARRAY,
                'required' => false,
                'defaultValue'  => [1, 2, 3],
                'description' => 'Special...'
            ],
        ]);

        // Set the allowed-options-list
        $this->getArrayOptionsList()->setAllowedOptionsList($allowedOptions);
    }

    public function doSomeThing(array $options){
        // Set the given options in the "options-list"
        $optionsList = $this->getArrayOptionsList();
        $optionsList->setArrayOptions($options); // Validates input, throws exceptions if something is wrong

        // Get the value OR default value for a given option
        $myOption = $optionsList->getValue('myOption');

        return $myOption;
    }

}

For additional information, please review this package’s interfaces as well as the unit tests.

License

BSD-3-Clause, Read the LICENSE file included in this package

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-03-22