Skip to content

Commit 381d8bc

Browse files
authoredJun 21, 2022
Merge pull request #99 from kayrunm/define-child-tasks-via-method
Allow child types to be set via a method.
2 parents 2779b83 + eeccf4c commit 381d8bc

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed
 

‎src/HasChildren.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ protected static function registerModelEvent($event, $callback)
1414
{
1515
parent::registerModelEvent($event, $callback);
1616

17-
if (static::class === self::class && property_exists(self::class, 'childTypes')) {
17+
$childTypes = (new self)->getChildTypes();
18+
19+
if (static::class === self::class && $childTypes !== []) {
1820
// We don't want to register the callbacks that happen in the boot method of the parent, as they'll be called
1921
// from the child's boot method as well.
2022
if (! self::parentIsBooting()) {
21-
foreach ((new self)->childTypes as $childClass) {
23+
foreach ($childTypes as $childClass) {
2224
if ($childClass !== self::class) {
2325
$childClass::registerModelEvent($event, $callback);
2426
}
@@ -193,10 +195,10 @@ protected function getChildModel(array $attributes)
193195
*/
194196
public function classFromAlias($aliasOrClass)
195197
{
196-
if (property_exists($this, 'childTypes')) {
197-
if (isset($this->childTypes[$aliasOrClass])) {
198-
return $this->childTypes[$aliasOrClass];
199-
}
198+
$childTypes = $this->getChildTypes();
199+
200+
if (isset($childTypes[$aliasOrClass])) {
201+
return $childTypes[$aliasOrClass];
200202
}
201203

202204
return $aliasOrClass;
@@ -208,10 +210,10 @@ public function classFromAlias($aliasOrClass)
208210
*/
209211
public function classToAlias($className)
210212
{
211-
if (property_exists($this, 'childTypes')) {
212-
if (in_array($className, $this->childTypes)) {
213-
return array_search($className, $this->childTypes);
214-
}
213+
$childTypes = $this->getChildTypes();
214+
215+
if (in_array($className, $childTypes)) {
216+
return array_search($className, $childTypes);
215217
}
216218

217219
return $className;

‎tests/Unit/HasChildrenTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ function child_model_mutators_are_not_instigated()
1818

1919
$this->assertEquals($model->mutatorWasCalled, false);
2020
}
21+
22+
/** @test */
23+
function child_model_types_can_be_set_via_method()
24+
{
25+
$types = (new HasChildrenParentModelWithMethodTypes)->getChildTypes();
26+
27+
$this->assertEquals([
28+
'foo' => Foo::class,
29+
'bar' => Bar::class,
30+
], $types);
31+
}
2132
}
2233

2334
class HasChildrenParentModel extends Model {
@@ -34,3 +45,16 @@ public function setTestAttribute()
3445
$this->mutatorWasCalled = true;
3546
}
3647
}
48+
49+
class HasChildrenParentModelWithMethodTypes extends Model
50+
{
51+
use HasChildren;
52+
53+
public function getChildTypes()
54+
{
55+
return [
56+
'foo' => Foo::class,
57+
'bar' => Bar::class,
58+
];
59+
}
60+
}

0 commit comments

Comments
 (0)
Please sign in to comment.