observer_002.phpt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. --TEST--
  2. SPL: SplObjectStorage
  3. --FILE--
  4. <?php
  5. class MyObjectStorage extends SplObjectStorage
  6. {
  7. function rewind(): void
  8. {
  9. echo __METHOD__ . "()\n";
  10. parent::rewind();
  11. }
  12. function valid(): bool
  13. {
  14. echo __METHOD__ . "(" . (parent::valid() ? 1 : 0) . ")\n";
  15. return parent::valid();
  16. }
  17. function key(): int
  18. {
  19. echo __METHOD__ . "(" . parent::key() . ")\n";
  20. return parent::key();
  21. }
  22. function current(): object
  23. {
  24. echo __METHOD__ . "(" . parent::current()->getName() . ")\n";
  25. return parent::current();
  26. }
  27. function next(): void
  28. {
  29. echo __METHOD__ . "()\n";
  30. parent::next();
  31. }
  32. }
  33. class ObserverImpl implements SplObserver
  34. {
  35. protected $name = '';
  36. function __construct($name = 'obj')
  37. {
  38. $this->name = '$' . $name;
  39. }
  40. function update(SplSubject $subject): void
  41. {
  42. echo $this->name . '->' . __METHOD__ . '(' . $subject->getName() . ");\n";
  43. }
  44. function getName()
  45. {
  46. return $this->name;
  47. }
  48. }
  49. class SubjectImpl implements SplSubject
  50. {
  51. protected $name = '';
  52. protected $observers;
  53. function __construct($name = 'sub')
  54. {
  55. $this->observers = new MyObjectStorage;
  56. $this->name = '$' . $name;
  57. }
  58. function attach(SplObserver $observer): void
  59. {
  60. echo $this->name . '->' . __METHOD__ . '(' . $observer->getName() . ");\n";
  61. $this->observers->attach($observer);
  62. }
  63. function detach(SplObserver $observer): void
  64. {
  65. echo $this->name . '->' . __METHOD__ . '(' . $observer->getName() . ");\n";
  66. $this->observers->detach($observer);
  67. }
  68. function count(): int
  69. {
  70. return $this->observers->count();
  71. }
  72. function notify(): void
  73. {
  74. echo $this->name . '->' . __METHOD__ . "();\n";
  75. foreach($this->observers as $key => $observer)
  76. {
  77. $observer->update($this);
  78. }
  79. }
  80. function getName()
  81. {
  82. return $this->name;
  83. }
  84. function contains($obj)
  85. {
  86. return $this->observers->contains($obj);
  87. }
  88. }
  89. $sub = new SubjectImpl;
  90. $ob1 = new ObserverImpl("ob1");
  91. $ob2 = new ObserverImpl("ob2");
  92. $ob3 = new ObserverImpl("ob3");
  93. var_dump($sub->contains($ob1));
  94. $sub->attach($ob1);
  95. var_dump($sub->contains($ob1));
  96. $sub->attach($ob1);
  97. $sub->attach($ob2);
  98. $sub->attach($ob3);
  99. var_dump($sub->count());
  100. $sub->notify();
  101. $sub->detach($ob3);
  102. var_dump($sub->count());
  103. $sub->notify();
  104. $sub->detach($ob2);
  105. $sub->detach($ob1);
  106. var_dump($sub->count());
  107. $sub->notify();
  108. $sub->attach($ob3);
  109. var_dump($sub->count());
  110. $sub->notify();
  111. ?>
  112. --EXPECT--
  113. bool(false)
  114. $sub->SubjectImpl::attach($ob1);
  115. bool(true)
  116. $sub->SubjectImpl::attach($ob1);
  117. $sub->SubjectImpl::attach($ob2);
  118. $sub->SubjectImpl::attach($ob3);
  119. int(3)
  120. $sub->SubjectImpl::notify();
  121. MyObjectStorage::rewind()
  122. MyObjectStorage::valid(1)
  123. MyObjectStorage::current($ob1)
  124. MyObjectStorage::key(0)
  125. $ob1->ObserverImpl::update($sub);
  126. MyObjectStorage::next()
  127. MyObjectStorage::valid(1)
  128. MyObjectStorage::current($ob2)
  129. MyObjectStorage::key(1)
  130. $ob2->ObserverImpl::update($sub);
  131. MyObjectStorage::next()
  132. MyObjectStorage::valid(1)
  133. MyObjectStorage::current($ob3)
  134. MyObjectStorage::key(2)
  135. $ob3->ObserverImpl::update($sub);
  136. MyObjectStorage::next()
  137. MyObjectStorage::valid(0)
  138. $sub->SubjectImpl::detach($ob3);
  139. int(2)
  140. $sub->SubjectImpl::notify();
  141. MyObjectStorage::rewind()
  142. MyObjectStorage::valid(1)
  143. MyObjectStorage::current($ob1)
  144. MyObjectStorage::key(0)
  145. $ob1->ObserverImpl::update($sub);
  146. MyObjectStorage::next()
  147. MyObjectStorage::valid(1)
  148. MyObjectStorage::current($ob2)
  149. MyObjectStorage::key(1)
  150. $ob2->ObserverImpl::update($sub);
  151. MyObjectStorage::next()
  152. MyObjectStorage::valid(0)
  153. $sub->SubjectImpl::detach($ob2);
  154. $sub->SubjectImpl::detach($ob1);
  155. int(0)
  156. $sub->SubjectImpl::notify();
  157. MyObjectStorage::rewind()
  158. MyObjectStorage::valid(0)
  159. $sub->SubjectImpl::attach($ob3);
  160. int(1)
  161. $sub->SubjectImpl::notify();
  162. MyObjectStorage::rewind()
  163. MyObjectStorage::valid(1)
  164. MyObjectStorage::current($ob3)
  165. MyObjectStorage::key(0)
  166. $ob3->ObserverImpl::update($sub);
  167. MyObjectStorage::next()
  168. MyObjectStorage::valid(0)