tags:

views:

17

answers:

1

I've created a simple function which allows me to determine which status options a change can have. For example a status of 'Pending' can only be changed to 'Active', 'Rejected' and 'Removed'.

In my class I store these as constants which reference the necessary record in a status column for purposes of comparison throughout the site.

My 'transition' array has the following:-

protected static $allowedTransitions=array(
 Booking::STATUS_ACTIVE=>array(Booking::STATUS_REVOKED),
     Booking::STATUS_PENDING=>array(Booking::STATUS_ACTIVE, Booking::STATUS_REJECTED, Booking::STATUS_REVOKED),
     Booking::STATUS_REJECTED=>array(Booking::STATUS_ACTIVE, Booking::STATUS_REVOKED),
     Booking::STATUS_REVOKED=>array()
);

What are the opinions on this approach? It doesn't seem like a very good idea to add constants in an array.

In order to check if a status request can be fulfilled, I pass the current status type Id to statusTransitions() which allows me to determine if the change is valid.

public static function statusTransitions($statusTypeId) {
 return self::$allowedTransitions[$statusTypeId];
}
+1  A: 

I'm not certain why someone elses opinion would matter more than yours. If this works and you are comfortable with it, go with it. There could be reasons unseen to us "outsiders" why you chose to do it like this. (I18N for example would be a good reason to do it like this)

My opinion is, do what works for you as long as you are not grossly doing something you shouldn't (like suppressing errors in your code so you don't have to look at them). I don't see anything wrong with your approach.

cdburgess
I see where your coming from, but from a technical point of view it seemed wring at the time of coding of creating an array of variables from class constants. In the end I created a table with the transitions in and simply queried for the relevant ID. This ensures it can easily be modified in the future.