SimpleStatePredicate.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/StatePredicate.php';
  15. //require 'Yay/States.php';
  16. /**
  17. * An expectation about what State a state machine is in.
  18. * @author Chris Corbyn <chris@w3style.co.uk>
  19. * @package Yay
  20. */
  21. class Yay_SimpleStatePredicate implements Yay_StatePredicate
  22. {
  23. /**
  24. * The state machine which this predicate checks.
  25. * @var Yay_States
  26. * @access private
  27. */
  28. protected $_stateMachine;
  29. /**
  30. * The state name to check for in the state machine.
  31. * @var string
  32. * @access private
  33. */
  34. protected $_stateName;
  35. /**
  36. * True if the state is wanted, false otherwise.
  37. * @var boolean
  38. * @access private
  39. */
  40. private $_is = true;
  41. /**
  42. * Create a new StatePredicate.
  43. * @param Yay_States $stateMachine
  44. * @param string $stateName to expect
  45. * @param boolean $is (negation point)
  46. */
  47. public function __construct(Yay_States $stateMachine, $stateName, $is = true)
  48. {
  49. $this->_stateMachine = $stateMachine;
  50. $this->_stateName = $stateName;
  51. $this->_is = $is;
  52. }
  53. /**
  54. * Return true if the state machine is in this state.
  55. * @return boolean
  56. */
  57. public function isActive()
  58. {
  59. return (($this->_is && $this->_stateMachine->getCurrentState() == $this->_stateName)
  60. || (!$this->_is && $this->_stateMachine->getCurrentState() != $this->_stateName));
  61. }
  62. /**
  63. * Write a description of this self describing object to Description.
  64. * @param Yay_Description $description
  65. */
  66. public function describeTo(Yay_Description $description)
  67. {
  68. $this->_stateMachine->describeTo($description);
  69. $description->appendText(sprintf(
  70. ' %s %s;',
  71. ($this->_is ? 'is' : 'is not'),
  72. $this->_stateName
  73. ));
  74. }
  75. }