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\Entity;
26:
27: /**
28: * Provides common functionality all entities could use
29: *
30: * @author David Schoenbauer <dschoenbauer@gmail.com>
31: */
32: abstract class AbstractEntity implements EntityInterface
33: {
34:
35: private $idField;
36: private $table;
37: private $name;
38:
39: /**
40: * provides field with primary key
41: * @return string
42: * @since v1.0.0
43: */
44: public function getIdField()
45: {
46: return $this->idField;
47: }
48:
49: /**
50: * provides which table the entities data is stored in
51: * @return string
52: * @since v1.0.0
53: */
54: public function getTable()
55: {
56: return $this->table;
57: }
58:
59: /**
60: * provides a name that can be used to reference the entity
61: * @return string
62: * @since v1.0.0
63: */
64: public function getName()
65: {
66: return $this->name;
67: }
68:
69: /**
70: * sets field with primary key
71: * @param string $idField
72: * @return $this
73: * @since v1.0.0
74: */
75: public function setIdField($idField)
76: {
77: $this->idField = $idField;
78: return $this;
79: }
80:
81: /**
82: * sets which table the entities data is stored in
83: * @param string $table
84: * @return $this
85: * @since v1.0.0
86: */
87: public function setTable($table)
88: {
89: $this->table = $table;
90: return $this;
91: }
92:
93: /**
94: * sets a name that can be used to reference the entity
95: * @param string $name
96: * @return $this
97: * @since v1.0.0
98: */
99: public function setName($name)
100: {
101: $this->name = $name;
102: return $this;
103: }
104:
105: /**
106: * provides an array of all fields the entity has
107: * @return array
108: * @since v1.0.0
109: */
110: public function getAllFields()
111: {
112: $fields = [];
113: if ($this instanceof HasBoolFieldsInterface) {
114: $fields = array_merge($fields, $this->getBoolFields());
115: }
116: if ($this instanceof HasDateFieldsInterface) {
117: $fields = array_merge($fields, $this->getDateFields());
118: }
119: if ($this instanceof HasNumericFieldsInterface) {
120: $fields = array_merge($fields, $this->getNumericFields());
121: }
122: if ($this instanceof HasStringFieldsInterface) {
123: $fields = array_merge($fields, $this->getStringFields());
124: }
125: return $fields;
126: }
127: }
128: