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;
26:
27: use DSchoenbauer\Orm\Enum\ModelEvents;
28:
29: /**
30: * Extends core model adding a basic set of methods triggering key events for crud operations
31: *
32: * @author David Schoenbauer
33: */
34: class CrudModel extends Model
35: {
36:
37: /**
38: * Process the addition of a new record
39: * @param array $data
40: * @return array
41: * @since v1.0.0
42: */
43: public function create($data)
44: {
45: $this->setData($data);
46: $this->getEventManager()->trigger(ModelEvents::CREATE, $this);
47: $this->getEventManager()->trigger(ModelEvents::FETCH, $this);
48: return $this->getData();
49: }
50:
51: /**
52: * Process the return of a single record
53: * @param integer $id
54: * @return array
55: * @since v1.0.0
56: */
57: public function fetch($id)
58: {
59: $this->setId($id);
60: $this->getEventManager()->trigger(ModelEvents::FETCH, $this);
61: return $this->getData();
62: }
63:
64: /**
65: * Process the return of a collection of data
66: * @return array
67: * @since v1.0.0
68: */
69: public function fetchAll()
70: {
71: $this->getEventManager()->trigger(ModelEvents::FETCH_ALL, $this);
72: return $this->getData();
73: }
74:
75: /**
76: * Process the update of data for a given id
77: * @param integer $id primary id number of the record to be updated
78: * @param array $data an associative array of the data to be updated
79: * @return array
80: * @since v1.0.0
81: */
82: public function update($id, $data)
83: {
84: $this->setId($id)->setData($data);
85: $this->getEventManager()->trigger(ModelEvents::UPDATE, $this);
86: $this->getEventManager()->trigger(ModelEvents::FETCH, $this);
87: return $this->getData();
88: }
89:
90: /**
91: * Process the removal of a given record
92: * @param integer $id primary ID of a value to be removed
93: * @return boolean returns true on success
94: * @since v1.0.0
95: */
96: public function delete($id)
97: {
98: $this->setId($id);
99: $this->getEventManager()->trigger(ModelEvents::DELETE, $this);
100: return true;
101: }
102: }
103: