1212 */
1313abstract class Schema
1414{
15- /** @var string|Model */
16- protected $ model ;
17-
18- /** @var string */
19- protected $ table ;
20-
21- /** @var null|string|string[] */
22- protected $ primaryKey ;
23-
24- /** @var string[] */
25- protected $ fields ;
26-
27- /** @var array[] */
28- protected $ relationships = [];
29-
3015 /** @var Relationship[] */
3116 private $ relationshipCache ;
3217
@@ -39,20 +24,11 @@ public function __construct(ContainerInterface $container)
3924 $ this ->container = $ container ;
4025 }
4126
42- public function getTable (): string
43- {
44- return $ this ->table ;
45- }
46-
47- public function getPrimaryKey (): array
48- {
49- return $ this ->primaryKey === null ? [] : (array ) $ this ->primaryKey ;
50- }
51-
52- public function getFields (): array
53- {
54- return $ this ->fields ;
55- }
27+ abstract public function getModel (): string ;
28+ abstract public function getTable (): string ;
29+ abstract public function getPrimaryKey (): array ;
30+ abstract public function getFields (): array ;
31+ abstract public function getRelationshipDefinitions (): array ;
5632
5733 /**
5834 * @return Relationship[]
@@ -61,7 +37,7 @@ public function getRelationships(): array
6137 {
6238 $ relationships = [];
6339
64- foreach (array_keys ($ this ->relationships ) as $ name ) {
40+ foreach (array_keys ($ this ->getRelationshipDefinitions () ) as $ name ) {
6541 $ relationships [$ name ] = $ this ->getRelationship ($ name );
6642 }
6743
@@ -74,23 +50,23 @@ public function getRelationship(string $name): Relationship
7450 return $ this ->relationshipCache [$ name ];
7551 }
7652
77- if (!isset ($ this ->relationships [$ name ])) {
53+ $ definition = $ this ->getRelationshipDefinitions ()[$ name ] ?? null ;
54+
55+ if (empty ($ definition )) {
7856 throw new \InvalidArgumentException ("Invalid relationship ' $ name' " );
7957 }
8058
81- $ this ->relationshipCache [$ name ] = new Relationship (
82- $ name ,
83- $ this ,
84- (array ) $ this ->relationships [$ name ]['key ' ],
85- $ this ->getSchema ($ this ->relationships [$ name ]['schema ' ]),
86- (array ) $ this ->relationships [$ name ]['field ' ],
87- empty ($ this ->relationships [$ name ]['unique ' ]) ? false : true
88- );
59+ $ key = \is_array ($ definition ['key ' ]) ? $ definition ['key ' ] : [$ definition ['key ' ]];
60+ $ schema = $ this ->loadSchema ($ definition ['schema ' ]);
61+ $ fields = \is_array ($ definition ['field ' ]) ? $ definition ['field ' ] : [$ definition ['field ' ]];
62+ $ unique = empty ($ definition ['unique ' ]) ? false : true ;
63+
64+ $ this ->relationshipCache [$ name ] = new Relationship ($ name , $ this , $ key , $ schema , $ fields , $ unique );
8965
9066 return $ this ->relationshipCache [$ name ];
9167 }
9268
93- private function getSchema (string $ name ): Schema
69+ private function loadSchema (string $ name ): Schema
9470 {
9571 return $ this ->container ->get ($ name );
9672 }
@@ -129,7 +105,10 @@ public function createRecordFromRow(array $row, string $prefix = ''): Record
129105
130106 public function createModel (Record $ record ): Model
131107 {
132- return $ this ->model ::createFromDatabaseRecord ($ record );
108+ /** @var Model $model */
109+ $ model = $ this ->getModel ();
110+
111+ return $ model ::createFromDatabaseRecord ($ record );
133112 }
134113
135114 public function createModelFromRow (array $ row , string $ prefix = '' , array $ relationships = []): Model
0 commit comments