Skip to content

Commit 45be53c

Browse files
committed
Refactor _initRelationship
1 parent 5a99b07 commit 45be53c

1 file changed

Lines changed: 70 additions & 103 deletions

File tree

src/Models/Relations.php

Lines changed: 70 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -74,119 +74,86 @@ protected static function _initRelationships()
7474
}
7575
}
7676

77-
// TODO: Make relations getPrimaryKeyValue() instead of using ID all the time.
78-
protected static function _initRelationship($relationship, $options)
77+
protected static function _prepareOneOne(string $relationship, array $options): array
7978
{
80-
$classShortName = basename(str_replace('\\', '/', static::$rootClass));
81-
82-
// apply defaults
83-
if (empty($options['type'])) {
84-
$options['type'] = 'one-one';
85-
}
86-
87-
if ($options['type'] == 'one-one') {
88-
if (empty($options['local'])) {
89-
$options['local'] = $relationship . 'ID';
90-
}
91-
92-
if (empty($options['foreign'])) {
93-
$options['foreign'] = 'ID';
94-
}
95-
} elseif ($options['type'] == 'one-many') {
96-
if (empty($options['local'])) {
97-
$options['local'] = 'ID';
98-
}
99-
100-
if (empty($options['foreign'])) {
101-
$options['foreign'] = $classShortName. 'ID';
102-
}
103-
104-
if (!isset($options['indexField'])) {
105-
$options['indexField'] = false;
106-
}
107-
108-
if (!isset($options['conditions'])) {
109-
$options['conditions'] = [];
110-
} elseif (is_string($options['conditions'])) {
111-
$options['conditions'] = [$options['conditions']];
112-
}
113-
114-
if (!isset($options['order'])) {
115-
$options['order'] = false;
116-
}
117-
} elseif ($options['type'] == 'context-children') {
118-
if (empty($options['local'])) {
119-
$options['local'] = 'ID';
120-
}
121-
122-
if (empty($options['contextClass'])) {
123-
$options['contextClass'] = get_called_class();
124-
}
125-
126-
if (!isset($options['indexField'])) {
127-
$options['indexField'] = false;
128-
}
129-
130-
if (!isset($options['conditions'])) {
131-
$options['conditions'] = [];
132-
}
133-
134-
if (!isset($options['order'])) {
135-
$options['order'] = false;
136-
}
137-
} elseif ($options['type'] == 'context-parent') {
138-
if (empty($options['local'])) {
139-
$options['local'] = 'ContextID';
140-
}
141-
142-
if (empty($options['foreign'])) {
143-
$options['foreign'] = 'ID';
144-
}
145-
146-
if (empty($options['classField'])) {
147-
$options['classField'] = 'ContextClass';
148-
}
79+
$options['local'] = $options['local'] ?? $relationship . 'ID';
80+
$options['foreign'] = $options['foreign'] ?? 'ID';
81+
return $options;
82+
}
14983

150-
if (empty($options['allowedClasses'])) {
151-
$options['allowedClasses'] = static::$contextClasses;
152-
}
153-
} elseif ($options['type'] == 'many-many') {
154-
if (empty($options['class'])) {
155-
throw new Exception('Relationship type many-many option requires a class setting.');
156-
}
84+
protected static function _prepareOneMany(string $classShortName, array $options): array
85+
{
86+
$options['local'] = $options['local'] ?? 'ID';
87+
$options['foreign'] = $options['foreign'] ?? $classShortName. 'ID';
88+
$options['indexField'] = $options['indexField'] ?? false;
89+
$options['conditions'] = $options['conditions'] ?? [];
90+
$options['conditions'] = is_string($options['conditions']) ? [$options['conditions']] : $options['conditions'];
91+
$options['order'] = $options['order'] ?? false;
92+
return $options;
93+
}
15794

158-
if (empty($options['linkClass'])) {
159-
throw new Exception('Relationship type many-many option requires a linkClass setting.');
160-
}
95+
protected static function _prepareContextChildren($options): array {
96+
$options['local'] = $options['local'] ?? 'ID';
97+
$options['contextClass'] = $options['contextClass'] ?? get_called_class();
98+
$options['indexField'] = $options['indexField'] ?? false;
99+
$options['conditions'] = $options['conditions'] ?? [];
100+
$options['order'] = $options['order'] ?? false;
101+
return $options;
102+
}
161103

162-
if (empty($options['linkLocal'])) {
163-
$options['linkLocal'] = $classShortName . 'ID';
164-
}
104+
protected static function _prepareContextParent($options): array {
105+
$options['local'] = $options['local'] ?? 'ContextID';
106+
$options['foreign'] = $options['foreign'] ?? 'ID';
107+
$options['classField'] = $options['classField'] ?? 'ContextClass';
108+
$options['allowedClasses'] = $options['allowedClasses'] ?? (!empty(static::$contextClasses)?static::$contextClasses:null);
109+
return $options;
110+
}
165111

166-
if (empty($options['linkForeign'])) {
167-
$foreignShortname = basename(str_replace('\\', '/', $options['class']::$rootClass));
168-
$options['linkForeign'] = $foreignShortname . 'ID';
169-
}
112+
protected static function _prepareManyMany($classShortName, $options): array {
113+
if (empty($options['class'])) {
114+
throw new Exception('Relationship type many-many option requires a class setting.');
115+
}
170116

171-
if (empty($options['local'])) {
172-
$options['local'] = 'ID';
173-
}
117+
if (empty($options['linkClass'])) {
118+
throw new Exception('Relationship type many-many option requires a linkClass setting.');
119+
}
174120

175-
if (empty($options['foreign'])) {
176-
$options['foreign'] = 'ID';
177-
}
121+
$options['linkLocal'] = $options['linkLocal'] ?? $classShortName . 'ID';
122+
$options['linkForeign'] = $options['linkForeign'] ?? basename(str_replace('\\', '/', $options['class']::$rootClass)).'ID';
123+
$options['local'] = $options['local'] ?? 'ID';
124+
$options['foreign'] = $options['foreign'] ?? 'ID';
125+
$options['indexField'] = $options['indexField'] ?? false;
126+
$options['conditions'] = $options['conditions'] ?? [];
127+
$options['order'] = $options['order'] ?? false;
128+
return $options;
129+
}
178130

179-
if (!isset($options['indexField'])) {
180-
$options['indexField'] = false;
181-
}
131+
// TODO: Make relations getPrimaryKeyValue() instead of using ID all the time.
132+
protected static function _initRelationship($relationship, $options)
133+
{
134+
$classShortName = basename(str_replace('\\', '/', static::$rootClass));
182135

183-
if (!isset($options['conditions'])) {
184-
$options['conditions'] = [];
185-
}
136+
// apply defaults
137+
if (empty($options['type'])) {
138+
$options['type'] = 'one-one';
139+
}
186140

187-
if (!isset($options['order'])) {
188-
$options['order'] = false;
189-
}
141+
switch($options['type']) {
142+
case 'one-one':
143+
$options = static::_prepareOneOne($relationship, $options);
144+
break;
145+
case 'one-many':
146+
$options = static::_prepareOneMany($classShortName, $options);
147+
break;
148+
case 'context-children':
149+
$options = static::_prepareContextChildren($options);
150+
break;
151+
case 'context-parent':
152+
$options = static::_prepareContextParent($options);
153+
break;
154+
case 'many-many':
155+
$options = static::_prepareManyMany($classShortName,$options);
156+
break;
190157
}
191158

192159
if (static::isVersioned() && $options['type'] == 'history') {

0 commit comments

Comments
 (0)