asz/statemm

manage model state

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

This package is auto-updated.

Last update: 2024-05-17 01:16:40 UTC


README

68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f61686d61647a72657161742f73746174654d4d 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f61686d61647a72657161742f73746174654d4d 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f61686d61647a72657161742f73746174654d4d 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f77617463686572732f61686d61647a72657161742f73746174654d4d3f7374796c653d736f6369616c

Overview

  • This is a Laravel package to manage model state

Installation :

You can install asz/statemm via Composer by adding " asz/statemm": "^1.1" as requirement to your composer.json. OR :

composer require asz/statemm
  • Then :
composer dump-autoload

Service Provider:

go to your config/app.php file and add :

 statemm\StateServiceProvider::class ,

adding interface to model

class Product extends Model implements hasState

use command line to create a new state for your model

$ 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();
    }

Test Unit

    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);
    }
    
    ```