PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / action / observable / adapter.php
vikappointments / site / helpers / libraries / action / observable Last commit date
adapter.php 3 days ago index.html 3 days ago
adapter.php
103 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 * Observable action adapter.
16 *
17 * @since 1.7.3
18 */
19 class VAPActionObservableAdapter implements VAPActionObservable
20 {
21 /**
22 * An array of subscribers.
23 *
24 * @var VAPActionObserver[]
25 */
26 private $listeners = [];
27
28 /**
29 * Class constructor.
30 *
31 * @param array $listeners
32 */
33 public function __construct(array $listeners = [])
34 {
35 foreach ($listeners as $l)
36 {
37 $this->attach($l);
38 }
39 }
40
41 /**
42 * Attaches the specified observer to this entity.
43 *
44 * @param VAPActionObserver $observer
45 *
46 * @return void
47 */
48 public function attach(VAPActionObserver $observer)
49 {
50 $this->listeners[] = $observer;
51 }
52
53 /**
54 * Detaches the specified observer from this entity.
55 *
56 * @param VAPActionObserver $observer
57 *
58 * @return boolean True in case of success.
59 */
60 public function detach(VAPActionObserver $observer)
61 {
62 // search the specified observer
63 $index = array_search($observer, $this->listeners);
64
65 if ($index !== false)
66 {
67 // remove the observer from the array
68 array_splice($this->listeners, $index, 1);
69 return true;
70 }
71
72 return false;
73 }
74
75 /**
76 * Notifies the subscribers every time the internal state changes.
77 *
78 * @param VAPActionState $state
79 *
80 * @return VAPActionResult A list of results returned by the observers.
81 */
82 public function notify(VAPActionState $state)
83 {
84 $results = [];
85
86 // iterate subscribers
87 foreach ($this->listeners as $l)
88 {
89 // trigger state change
90 $results[] = $l->trigger($state);
91
92 if ($state->isPropagationStopped())
93 {
94 // break the cycle in case the last subscriber
95 // stopped the action propagation
96 break;
97 }
98 }
99
100 return new VAPActionResult($results);
101 }
102 }
103