PHP Finite-state Machine