1: <?php
2:
3: namespace Deimos\ORM;
4:
5: use Deimos\Database\Database;
6: use Doctrine\Common\Inflector\Inflector;
7:
8: class ORM
9: {
10:
11: /**
12: * @var Database
13: */
14: protected $database;
15:
16: /**
17: * @var []
18: */
19: protected $configure = [];
20:
21: /**
22: * @var []
23: */
24: protected $classMap = [];
25:
26: /**
27: * @var []
28: */
29: protected $tableMap = [];
30:
31: /**
32: * ORM constructor.
33: *
34: * @param Database $database
35: */
36: public function __construct(Database $database)
37: {
38: $this->database = $database;
39: }
40:
41: /**
42: * @param string $modelName
43: * @param string $class
44: */
45: public function register($modelName, $class)
46: {
47: $this->classMap[$modelName] = $class;
48: }
49:
50: /**
51: * @param $modelName
52: *
53: * @return mixed
54: */
55: protected function mapClass($modelName)
56: {
57: if (isset($this->classMap[$modelName]))
58: {
59: return $this->classMap[$modelName];
60: }
61:
62: return Entity::class;
63: }
64:
65: /**
66: * @param $modelName
67: *
68: * @return mixed
69: */
70: protected function mapTable($modelName)
71: {
72: if (!isset($this->tableMap[$modelName]))
73: {
74: $class = $this->mapClass($modelName);
75:
76: /**
77: * @var $object Entity
78: */
79: $object = new $class(null);
80:
81: $this->tableMap[$modelName] =
82: $object->tableName() ?:
83: Inflector::pluralize($modelName);
84: }
85:
86: return $this->tableMap[$modelName];
87: }
88:
89: /**
90: * @param string $modelName
91: *
92: * @return Queries\Query
93: */
94: public function repository($modelName)
95: {
96: $class = $this->mapClass($modelName);
97: $table = $this->mapTable($modelName);
98:
99: return new Queries\Query($this->database, $class, $table);
100: }
101:
102: /**
103: * @param string $modelName
104: *
105: * @return Entity
106: */
107: public function create($modelName)
108: {
109: $class = $this->mapClass($modelName);
110: $table = $this->mapTable($modelName);
111:
112: return new $class($this->database, true, $table);
113: }
114:
115: }
116: