1: <?php
2:
3: namespace Deimos\ORM;
4:
5: use Deimos\Database\Database;
6: use Doctrine\Common\Inflector\Inflector;
7:
8: class Entity
9: {
10:
11: 12: 13:
14: private $database;
15:
16: 17: 18:
19: protected $primaryKey = 'id';
20:
21: 22: 23:
24: protected $isLoad;
25:
26: 27: 28:
29: protected $isNew;
30:
31: 32: 33:
34: protected $table;
35:
36: 37: 38:
39: protected $config;
40:
41: 42: 43:
44: protected $origin = [];
45:
46: 47: 48:
49: protected $modify = [];
50:
51: 52: 53: 54: 55: 56: 57:
58: public function __construct($database, $isNew = true, $table = null)
59: {
60: $this->table = $table;
61: $this->database = $database;
62: $this->isNew = $isNew;
63:
64: if ($this->isNew)
65: {
66: $this->isLoad = false;
67: }
68: }
69:
70: 71: 72:
73: public function id()
74: {
75: return $this->get($this->primaryKey);
76: }
77:
78: 79: 80:
81: public function tableName()
82: {
83: if (!$this->table && self::class !== static::class)
84: {
85: $ref = new \ReflectionClass(static::class);
86: $table = $ref->getName();
87: $table = lcfirst($table);
88:
89: return Inflector::pluralize($table);
90: }
91:
92: return $this->table;
93: }
94:
95: 96: 97:
98: protected function database()
99: {
100: return $this->database;
101: }
102:
103: 104: 105: 106: 107:
108: public function get($name)
109: {
110: if (isset($this->modify[$name]))
111: {
112: return $this->modify[$name];
113: }
114:
115: return $this->origin[$name];
116: }
117:
118: 119: 120: 121: 122:
123: public function __get($name)
124: {
125: return $this->get($name);
126: }
127:
128: 129: 130: 131:
132: public function __set($name, $value)
133: {
134: $this->set($name, $value);
135: }
136:
137: 138: 139: 140:
141: public function set($name, $value)
142: {
143: $this->modify[$name] = $value;
144: }
145:
146: 147: 148:
149: protected function toLoad()
150: {
151: $this->origin = array_merge($this->origin, $this->modify);
152:
153: $this->modify = [];
154: $this->isNew = false;
155: $this->isLoad = true;
156: }
157:
158: 159: 160: 161: 162:
163: public function save(array $storage = [])
164: {
165: foreach ($storage as $key => $value)
166: {
167: $this->set($key, $value);
168: }
169:
170: if ($this->isNew)
171: {
172: $insertId = $this->database()->insert()
173: ->from($this->table)
174: ->values($this->modify)
175: ->insert();
176:
177: if ($insertId)
178: {
179: $this->set($this->primaryKey, $insertId);
180: $this();
181: }
182:
183: return (bool)$insertId;
184: }
185:
186: if (!$this->isLoad)
187: {
188: throw new \InvalidArgumentException(__CLASS__);
189: }
190:
191: $update = (bool)$this->database()->update()
192: ->from($this->table)
193: ->values($this->modify)
194: ->where($this->primaryKey, $this->id())
195: ->updateOne();
196:
197: if ($update)
198: {
199: $this();
200: }
201:
202: return $update;
203: }
204:
205: 206: 207:
208: public function asArray()
209: {
210: return array_merge($this->origin, $this->modify);
211: }
212:
213: 214: 215: 216: 217:
218: public function __isset($name)
219: {
220: return isset($this->origin[$name]);
221: }
222:
223: 224: 225:
226: public function __invoke()
227: {
228: $this->toLoad();
229: }
230:
231: }