pifeifei / hyperf-options
v0.1.1
2020-08-24 12:58 UTC
Requires
- php: >=7.2
- hyperf/constants: ~2.0.0
- hyperf/db-connection: ~2.0.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- hyperf/testing: ~2.0.0
- hyperf/translation: ^2.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^0.12.5
- swoft/swoole-ide-helper: dev-master
This package is auto-updated.
Last update: 2024-10-25 11:27:54 UTC
README
composer require pifeifei/hyperf-options
使用
// app/Constants/AbstractConstants.php namespace App\Constants; class AbstractConstants extends \Hyperf\Constants\AbstractConstants { use \Pff\HyperfOptions\Concerns\OptionsConstants; } // app/Constants/Status.php class Status extends AbstractConstants { /** * @Message("ok") * @Info("content 1") * @Desc("sequential_array") * @GoodsInfoDesc("abcde") */ const SERVER_OK = 1; /** * @Message("delete") * @Info("content -1") * @lang("validation.sequential_array") */ const SERVER_DELETE = -1; /** * @Message("forbid") * @Info("content 0") */ const SERVER_FORBID = 0; } // app/Model/Model.php namespace App\Model; abstract class Model extends \Hyperf\DbConnection\Model\Model { use \Pff\HyperfOptions\Concerns\Options; } namespace App\Model; class Test extends Model { /* @var array */ public $options = [ // 'status' => [Status::class, 'Message'], // 'status' => Status::class, // default: Message 'status' => [\App\Constants\Status::class, 'info'] ]; /** * @return mixed */ public function getStatusOptionAttribute() { return $this->getOption('status'); } public function getStatusOptionsAttribute($default = null) { return $this->getStatusOptions($default); } } // use $test = new \App\Model\Test(); $result = $test::query()->orderByDesc('id')->first(); // {"id":1,"status":1} var_dump($result->status_option); // return:'content 1' var_dump($result->status_options); // return: [1=>'content 1', 0=>'content 0', -1 => 'content -1']