From bd17db353762e1404695793c5b2d33886fc4d4fe Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 29 Jan 2022 21:39:01 +0400 Subject: [PATCH] refactor(core)!: `Subject` methods will return `self` instead of `void`. --- src/Observer/Dispatcher.php | 24 ++++++++++++++---------- src/Observer/Subject.php | 13 +++++++++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Observer/Dispatcher.php b/src/Observer/Dispatcher.php index 516f36c..ef6c7f4 100644 --- a/src/Observer/Dispatcher.php +++ b/src/Observer/Dispatcher.php @@ -19,32 +19,36 @@ public function __construct() { $this->reset(); } - /** - * @param Closure(TContext):void $observer - */ - public function attach(Closure $observer): void { + public function attach(Closure $observer): static { $this->observers->attach($observer); + + return $this; } - /** - * @param Closure(TContext):void $observer - */ - public function detach(Closure $observer): void { + public function detach(Closure $observer): static { $this->observers->detach($observer); + + return $this; } - public function reset(): void { + public function reset(): static { $this->observers = new SplObjectStorage(); + + return $this; } /** * @param TContext $context + * + * @return $this */ - public function notify(mixed $context = null): void { + public function notify(mixed $context = null): static { foreach ($this->observers as $observer) { /** @var Closure $observer */ $observer($context); } + + return $this; } /** diff --git a/src/Observer/Subject.php b/src/Observer/Subject.php index 9dc0e4d..de24a7a 100644 --- a/src/Observer/Subject.php +++ b/src/Observer/Subject.php @@ -10,11 +10,20 @@ interface Subject { /** * @param Closure(TContext):void $observer + * + * @return $this */ - public function attach(Closure $observer): void; + public function attach(Closure $observer): self; /** * @param Closure(TContext):void $observer + * + * @return $this */ - public function detach(Closure $observer): void; + public function detach(Closure $observer): self; + + /** + * @return $this + */ + public function reset(): self; }