Overview

Namespaces

  • Deimos
    • ORM
      • Queries

Classes

  • Deimos\ORM\Entity
  • Deimos\ORM\ORM
  • Deimos\ORM\Queries\Query
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  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:      * @var Database
 13:      */
 14:     private $database;
 15: 
 16:     /**
 17:      * @var string
 18:      */
 19:     protected $primaryKey = 'id';
 20: 
 21:     /**
 22:      * @var bool
 23:      */
 24:     protected $isLoad;
 25: 
 26:     /**
 27:      * @var bool
 28:      */
 29:     protected $isNew;
 30: 
 31:     /**
 32:      * @var string
 33:      */
 34:     protected $table;
 35: 
 36:     /**
 37:      * @var array
 38:      */
 39:     protected $config;
 40: 
 41:     /**
 42:      * @var array
 43:      */
 44:     protected $origin = [];
 45: 
 46:     /**
 47:      * @var array
 48:      */
 49:     protected $modify = [];
 50: 
 51:     /**
 52:      * User constructor.
 53:      *
 54:      * @param Database $database
 55:      * @param bool     $isNew
 56:      * @param string   $table
 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:      * @return mixed
 72:      */
 73:     public function id()
 74:     {
 75:         return $this->get($this->primaryKey);
 76:     }
 77: 
 78:     /**
 79:      * @return string
 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:      * @return Database
 97:      */
 98:     protected function database()
 99:     {
100:         return $this->database;
101:     }
102: 
103:     /**
104:      * @param string $name
105:      *
106:      * @return mixed
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:      * @param string $name
120:      *
121:      * @return mixed
122:      */
123:     public function __get($name)
124:     {
125:         return $this->get($name);
126:     }
127: 
128:     /**
129:      * @param string $name
130:      * @param mixed  $value
131:      */
132:     public function __set($name, $value)
133:     {
134:         $this->set($name, $value);
135:     }
136: 
137:     /**
138:      * @param string $name
139:      * @param mixed  $value
140:      */
141:     public function set($name, $value)
142:     {
143:         $this->modify[$name] = $value;
144:     }
145: 
146:     /**
147:      * load data
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:      * @param array $storage
160:      *
161:      * @return bool
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:      * @return array
207:      */
208:     public function asArray()
209:     {
210:         return array_merge($this->origin, $this->modify);
211:     }
212: 
213:     /**
214:      * @param string $name
215:      *
216:      * @return bool
217:      */
218:     public function __isset($name)
219:     {
220:         return isset($this->origin[$name]);
221:     }
222: 
223:     /**
224:      * load data
225:      */
226:     public function __invoke()
227:     {
228:         $this->toLoad();
229:     }
230: 
231: }
API documentation generated by ApiGen