map.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Action container map implementor. |
| 16 | * |
| 17 | * @since 1.7.3 |
| 18 | */ |
| 19 | class VAPActionContainerMap implements VAPActionContainer |
| 20 | { |
| 21 | /** |
| 22 | * An event-subscribers lookup. |
| 23 | * |
| 24 | * @var VAPActionObservable[] |
| 25 | */ |
| 26 | protected $map = []; |
| 27 | |
| 28 | /** |
| 29 | * Attaches the specified observer to the given event. |
| 30 | * |
| 31 | * @param string $event The event to observe. |
| 32 | * @param VAPActionObserver $observer The subsriber instance. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function attach($event, VAPActionObserver $observer) |
| 37 | { |
| 38 | if (!isset($this->map[$event])) |
| 39 | { |
| 40 | // create new observale instance |
| 41 | $this->map[$event] = new VAPActionObservableAdapter(); |
| 42 | } |
| 43 | |
| 44 | // attach subscriber to the given event |
| 45 | $this->map[$event]->attach($observer); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Detaches the specified observer from the given event. |
| 50 | * |
| 51 | * @param string $event The observed event. |
| 52 | * @param VAPActionObserver $observer The subsriber instance. |
| 53 | * |
| 54 | * @return boolean True in case of success. |
| 55 | */ |
| 56 | public function detach($event, VAPActionObserver $observer) |
| 57 | { |
| 58 | if (!isset($this->map[$event])) |
| 59 | { |
| 60 | // event not yet registered |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // try to detach the subscriber from the given event |
| 65 | return $this->map[$event]->detach($observer); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Detaches all the same instances of the specified observer from the |
| 70 | * given event. In case the observer instance is omitted, the method |
| 71 | * will clear all the subscribers attached to the given event. |
| 72 | * |
| 73 | * @param string $event The observed event. |
| 74 | * @param VAPActionObserver $observer The subsriber instance. |
| 75 | * |
| 76 | * @return boolean True in case of success. |
| 77 | */ |
| 78 | public function detachAll($event, ?VAPActionObserver $observer = null) |
| 79 | { |
| 80 | if (!isset($this->map[$event])) |
| 81 | { |
| 82 | // event not yet registered |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (!$observer) |
| 87 | { |
| 88 | // clear all subscribed elements |
| 89 | unset($this->map[$event]); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | // iterate as long as the given instance is attached |
| 94 | // to the observable event |
| 95 | while ($this->map[$event]->detach($observer)); |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Notifies the subscribers every time the internal state of the |
| 103 | * given event changes. |
| 104 | * |
| 105 | * @param string $event The involved event. |
| 106 | * @param VAPActionState $data The state wrapper. |
| 107 | * |
| 108 | * @return VAPActionResult A list of results returned by the observers. |
| 109 | */ |
| 110 | public function notify($event, VAPActionState $state) |
| 111 | { |
| 112 | if (!isset($this->map[$event])) |
| 113 | { |
| 114 | // event not yet registered |
| 115 | return new VAPActionResult(); |
| 116 | } |
| 117 | |
| 118 | // notifies all the listeners attached to the given event |
| 119 | return $this->map[$event]->notify($state); |
| 120 | } |
| 121 | } |
| 122 |