1: <?php
2: /*
3: * The MIT License
4: *
5: * Copyright 2017 David Schoenbauer.
6: *
7: * Permission is hereby granted, free of charge, to any person obtaining a copy
8: * of this software and associated documentation files (the "Software"), to deal
9: * in the Software without restriction, including without limitation the rights
10: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11: * copies of the Software, and to permit persons to whom the Software is
12: * furnished to do so, subject to the following conditions:
13: *
14: * The above copyright notice and this permission notice shall be included in
15: * all copies or substantial portions of the Software.
16: *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23: * THE SOFTWARE.
24: */
25: namespace DSchoenbauer\Orm\Events;
26:
27: use DSchoenbauer\Orm\Model;
28: use DSchoenbauer\Orm\VisitorInterface;
29: use Zend\EventManager\Event;
30:
31: /**
32: * Allows for easier event attachment
33: *
34: * @author David Schoenbauer
35: */
36: abstract class AbstractEvent implements VisitorInterface
37: {
38:
39: private $events = [];
40:
41: public function __construct(array $events = [])
42: {
43: $this->setEvents($events);
44: }
45:
46:
47: /**
48: * provides an opportunity to extend Model's functionality
49: * @param Model $model model with which to be listened to
50: * @since v1.0.0
51: */
52: public function visitModel(Model $model)
53: {
54: foreach ($this->getEvents() as $event) {
55: $model->getEventManager()->attach($event, [$this, 'onExecute']);
56: }
57: }
58:
59: abstract public function onExecute(Event $event);
60:
61: /**
62: * returns a list of event names that this object should be executed
63: * @return array
64: * @since v1.0.0
65: **/
66: public function getEvents()
67: {
68: return $this->events;
69: }
70:
71: /**
72: * defines a list of event names this object will listen for to execute
73: * @param array $events array of event to be executed with
74: * @return $this
75: * @since v1.0.0
76: */
77: public function setEvents(array $events)
78: {
79: $this->events = $events;
80: return $this;
81: }
82: }
83: