aedart / array-option
该包已废弃,不再维护。未建议替代包。
允许您定义一组选项(数组键值对),这些选项必须遵循一组预定义的规则(允许的选项)
1.1.0
2015-06-21 17:12 UTC
Requires
- php: >=5.5.9
- aedart/overload: 1.*
- aedart/util: 1.*
Requires (Dev)
- aedart/license: 1.*
- aedart/license-file-manager: 1.*
- codeception/codeception: 2.0.*
- mockery/mockery: 0.9.*
Suggests
- aedart/model: Collection of getter- / setter-interfaces and traits for various common properties
README
允许您定义一组选项(数组键值对),这些选项必须遵循一组预定义的规则(允许的选项)
官方网站 (https://bitbucket.org/aedart/array-option)
内容
[目录]
何时使用此工具
如果您的组件、类或方法接受以数组(键值对)形式存在的选项,并且您需要对这些选项进行某种基本验证,例如检查是否必需、数据类型检查,以及最终是否具有预定义的默认值。
警告 我建议您不要使用此组件,除非您确实无法以其他方式指定 方法参数,而不是以 关联数组 的形式。
如何安装
#!console
composer require aedart/array-option 1.*
该包使用 composer。如果您不知道这是什么或者它是如何工作的,我建议您在尝试使用此包之前先了解一下。
快速入门
以下示例说明了如何构建一个接受 3 个选项的组件,每个选项都有一个默认值和特定的数据类型。
#!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;
}
}
如需更多信息,请查看此包的接口以及单元测试。
许可证
BSD-3-Clause,请阅读此包中包含的 LICENSE 文件