SimpleSequence.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /**
  15. * Provides a means for Expectations to verify they are called in the correct order.
  16. * This allows Invocations to be forced in a particular order.
  17. * @author Chris Corbyn <chris@w3style.co.uk>
  18. * @package Yay
  19. */
  20. class Yay_SimpleSequence implements Yay_Sequence
  21. {
  22. /**
  23. * The name of this sequence.
  24. * @var string
  25. * @access private
  26. */
  27. private $_name;
  28. /**
  29. * The list of sequence IDs expected.
  30. * @var array
  31. * @access private
  32. */
  33. private $_sequenceIds = array();
  34. /**
  35. * An internal sequence counter.
  36. * @var int
  37. * @access private
  38. */
  39. private $_counter = 0;
  40. /**
  41. * The current position in the sequence.
  42. * @var int
  43. * @access private
  44. */
  45. private $_currentId = null;
  46. /**
  47. * Create a new Sequence with $name.
  48. * @param string $name
  49. */
  50. public function __construct($name)
  51. {
  52. $this->_name = $name;
  53. }
  54. /**
  55. * Ask for a new Sequence Id and register the new sequence.
  56. * @return int $id
  57. */
  58. public function requestSequenceId()
  59. {
  60. $id = $this->_counter++;
  61. $this->_sequenceIds[] = $id;
  62. return $id;
  63. }
  64. /**
  65. * Check if the sequence has progressed far enough for this sequence ID to be used.
  66. * @param int $id
  67. * @return boolean
  68. */
  69. public function isInSequence($sequenceId)
  70. {
  71. if ($this->_currentId === $sequenceId)
  72. {
  73. $inSequence = true;
  74. }
  75. elseif (current($this->_sequenceIds) === $sequenceId)
  76. {
  77. $this->_currentId = array_shift($this->_sequenceIds);
  78. $inSequence = true;
  79. }
  80. else
  81. {
  82. $inSequence = false;
  83. }
  84. return $inSequence;
  85. }
  86. /**
  87. * Write a description of this self describing object to Description.
  88. * @param Yay_Description $description
  89. */
  90. public function describeTo(Yay_Description $description)
  91. {
  92. $description->appendText(sprintf(' sequence %s;', $this->_name));
  93. }
  94. }