logikostech/class-options
Composer 安装命令:
composer require logikostech/class-options
包简介
Class Options Manager
README 文档
README
Logikos\ClassOptions
Usage
Add it to any class like so:
<?php use Logikos\ClassOptions\ConfigurableInterface; use Logikos\ClassOptions\ConfigurableTrait; use Logikos\ClassOptions\OptionDefinition; class Configurable implements ConfigurableInterface { use ConfigurableTrait; const OPTION_FOO = 'foo'; const OPTION_BAR = 'bar'; const OPTION_REQUIRED = 'required'; const OPTION_NO_DASH = 'noDashes'; const OPTION_INT_ONLY = 'intOnly'; const OPTION_DATE = 'date'; public function __construct() { $this->addOption(self::OPTION_FOO); $this->addOption(self::OPTION_BAR); $this->defineOption($this->requiredOption()); $this->defineOption($this->noDashesOption()); $this->defineOption($this->intOnlyOption()); $this->defineOption($this->dateOption()); } public function execute() { if (!$this->validateOptions()) throw new \Exception('All required options must be set!'); // code to execute } private function requiredOption() { $o = new OptionDefinition(self::OPTION_REQUIRED); $o->makeRequired(); return $o; } private function noDashesOption() { $o = new OptionDefinition(self::OPTION_NO_DASH); $o->setValuePattern('/[^-]+/'); return $o; } private function intOnlyOption() { $o = new OptionDefinition(self::OPTION_INT_ONLY); $o->setValidationHook('is_int'); return $o; } private function dateOption() { $o = new OptionDefinition(self::OPTION_DATE); $o->setValidationHook(function($value) { $date = date_parse($value); return $date["error_count"] === 0; }); return $o; } }
Defining Options
As you can see the user of the trait can define an option with defineOption(OptionDefinitionInterface $option) or with the addOption(string 'name') method.
OptionDefinition
Create a new named option definition: $o = new OptionDefinition('foo');
That is all you need to create an option. However if you want validate what kind of values can be set to the option there are several helper methods.
$o->setValuePattern('/[0-1]+/');- use regex to validate as values are set$o->setValidationHook('is_int');- pass any callable and the value will be passed to it as the first arg. If the callable returns false an exception will be thrown.$o->setValidationHook(function($value){return (is_int($value) && $value>5);});- another callable example.
These validations will be checked anytime setValue is called and if they fail an exception is thrown.
You can also require the option to be set using makeRequired() then at any time you can call isValid() and it does a full validation testing against the pattern or callable above and checking to see if it has been set if it is required. Though isValid() does not throw an exception it just returns a bool. Even though it does check the pattern and callable hooks it technically should only return false for required options that are not set beings the other validators are checked when trying to set the value and beings you can not add a validator after the value is set. You can not really get an invalid value set so that isValid will return false.
logikostech/class-options 适用场景与选型建议
logikostech/class-options 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 166.89k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 06 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 logikostech/class-options 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 logikostech/class-options 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 166.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-15