asz/statemm

管理模型状态

v1.2 2021-07-16 18:59 UTC

This package is auto-updated.

Last update: 2024-09-17 02:15:40 UTC


README

概览

  • 这是一个用于管理模型状态的Laravel包

安装

您可以通过Composer安装 asz/statemm,在您的composer.json文件中添加"asz/statemm": "^1.1"作为需求。或者

composer require asz/statemm
  • 然后
composer dump-autoload

服务提供者

前往您的config/app.php文件,并添加

 statemm\StateServiceProvider::class ,

将接口添加到模型中

class Product extends Model implements hasState

使用命令行为您的模型创建新状态

$ php artisan state:make activated --dir=productStateContainer
add method stubs in your model:
 public function initialState()
    {
         //set your initial state after generated 
        // TODO: Implement initialState() method.
        
        return new ActivatedState();
    }

单元测试

    public function testCanDeactivateProduct()
    {

        $product = new product();
       
       // the product is active and the initial state will check to 
       // $this->transitionTo( new DeactivatedState()); 
       
        $context = new Context($product->where(['id' => 4])->first()); 
       
       // if true the proceed function will go to next state 
       // which is deactivatedState and execute the query or what 
       // you want todo
       // the deactivated has query to deactivate the given product
       
       $context->proceed();
       self::assertEquals(StateEnum::DEACTIVATED_STATE, $context->getModel()->state);
    }
    
    ```