StateMachine.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. //require 'Yay/State.php';
  15. //require 'Yay/SimpleState.php';
  16. //require 'Yay/StatePredicate.php';
  17. //require 'Yay/SimpleStatePredicate.php';
  18. /**
  19. * A basic state machine.
  20. * @author Chris Corbyn <chris@w3style.co.uk>
  21. * @package Yay
  22. */
  23. class Yay_StateMachine implements Yay_States
  24. {
  25. /**
  26. * The name of this state machine.
  27. * @var string
  28. * @access private
  29. */
  30. private $_name;
  31. /**
  32. * The current state.
  33. * @var string
  34. * @access private
  35. */
  36. private $_state;
  37. /**
  38. * Create a new State machine with $name.
  39. * @param string $name
  40. */
  41. public function __construct($name)
  42. {
  43. $this->_name = $name;
  44. }
  45. /**
  46. * Set the initial state of this state machine.
  47. * @param string $stateName
  48. * @return Yay_States
  49. */
  50. public function startsAs($stateName)
  51. {
  52. $this->become($stateName);
  53. return $this;
  54. }
  55. /**
  56. * Get the state which puts the state machine into the named state.
  57. * @param string $stateName
  58. * @return Yay_State
  59. */
  60. public function is($stateName)
  61. {
  62. return new Yay_SimpleState($this, $stateName);
  63. }
  64. /**
  65. * Get the predicate which indicates the state machine is NOT in the named state.
  66. * @param string $stateName
  67. * @return Yay_StatePredicate
  68. */
  69. public function isNot($stateName)
  70. {
  71. return new Yay_SimpleStatePredicate($this, $stateName, false);
  72. }
  73. /**
  74. * Become the named state.
  75. * @param string $stateName
  76. */
  77. public function become($stateName)
  78. {
  79. $this->_state = $stateName;
  80. }
  81. /**
  82. * Get the name of the current state.
  83. * @return string
  84. */
  85. public function getCurrentState()
  86. {
  87. return $this->_state;
  88. }
  89. /**
  90. * Write a description of this self describing object to Description.
  91. * @param Yay_Description $description
  92. */
  93. public function describeTo(Yay_Description $description)
  94. {
  95. $description->appendText(sprintf(' %s', $this->_name));
  96. }
  97. }