tags:

views:

18

answers:

1

I'm working on a status changing implementation for records. For example, when a record is created, it commences in a 'pending' state until a administrator changes its state to either 'active', 'rejected' or 'revoked'.

The thing is, if a status has been changed from pending to active, the status cannot be changed back to pending or rejected. A rejected status can be changed back to active.

A revoked status cannot changed to active, pending or rejected. At the moment I have a series of if/else statements to detect this but I wondered if there was a more logical and standard approach.

+5  A: 

You could have an array with all the allowed transitions:

$allowedTransitions = array(
    "pending" => array("active", "rejected", "revoked"),
    "active" => array("revoked"),
);

if (in_array($after, $allowedTransitions[$before])) { //...

You could do something a little more complicated that could detect that if can go from A to B and you can go from B to C, then you can go for A to C (if that's something you want). See reachability in the context of graph theory.

Artefacto