2017 © Pedro Peláez
 

library state-machine

State Machine library for PHP projects

image

esampaio/state-machine

State Machine library for PHP projects

  • Monday, January 27, 2014
  • by esampaio
  • Repository
  • 1 Watchers
  • 5 Stars
  • 27 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

State Machine

A dead simple State Machine PHP package., (*1)

Installation

To install, just add this to your composer.json:, (*2)

"require": {
    "esampaio/state-machine":"master"
}

Usage

PHP version 5.3

If you are running PHP 5.3 you need to inherit the StateMachine class:, (*3)

<?php

use Esampaio\StateMachine\StateMachine;

class MyClass extends StateMachine
{

PHP version 5.4

If you are running PHP version 5.4, just use the Trait:, (*4)

use Esampaio\StateMachine\Traits\StateMachine;

class MyClass
{
    use StateMachine;

Annotations

To add your possible states and transitions, use the following annotations:, (*5)

use Esampaio\StateMachine\Traits\StateMachine;
use Esampaio\StateMachine\States;
use Esampaio\StateMachine\Transition;

/**
 * @States(states = {"requested", "approved", "rejected"})
 * @Transition(state = "requested", transitions = {"approved", "rejected"})
 */
class MyClass
{
    use StateMachine;

Magic!

The following methods will be created for each state:, (*6)

$foo->isRequested(); // Check if foo's current state equals "requested"
$foo->canApproved(); // Check if foo's current state allows transition to "approved"
$foo->transitionRejected(); // Transitions current foo's state to "rejected"

Two methods are triggered when a transition happens, you may implement them if you want:, (*7)

$foo->beforeRejected(); // Triggered before foo's state is changed into "rejected"
$foo->afterRejected(); // Triggered after foo's state is changed into "rejected"

Check the source code and/or tests to understand it better., (*8)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

The Versions

27/01 2014

dev-master

9999999-dev http://esampaio.github.io/state-machine

State Machine library for PHP projects

  Sources   Download

MIT

The Requires

 

by Eduardo Sampaio

annotations statemachine state machine state-machine