Skip to content

Commit 4461bd5

Browse files
fix(trait): 🐛 change method existance scenario according to public methods
Remove calling of protected methods of the model.
1 parent 722d347 commit 4461bd5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/Traits/HasSnapshot.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,20 @@ protected function castToEloquent(array $data, string $modelClass)
327327
$model->setRawAttributes($data);
328328

329329
foreach ($data as $key => $value) {
330-
if (method_exists($model, $key) && $model->$key() instanceof Relation) {
331-
$relatedModelClass = get_class($model->$key()->getRelated());
332-
if (is_array($value) && !isset($value[0])) {
333-
$model->setRelation($key, $this->castToEloquent($value, $relatedModelClass));
334-
} elseif (is_array($value)) {
335-
$model->setRelation($key, collect($value)->map(function ($item) use ($relatedModelClass) {
336-
return $this->castToEloquent($item, $relatedModelClass);
337-
}));
330+
if(method_exists($model, $key)){
331+
$reflectionMethod = new \ReflectionMethod($model, $key);
332+
if ($reflectionMethod->isPublic() && $reflectionMethod->isStatic() === false) {
333+
$relationInstance = $model->$key();
334+
if ($relationInstance instanceof Relation) {
335+
$relatedModelClass = get_class($relationInstance->getRelated());
336+
if (is_array($value) && !isset($value[0])) {
337+
$model->setRelation($key, $this->castToEloquent($value, $relatedModelClass));
338+
} elseif (is_array($value)) {
339+
$model->setRelation($key, collect($value)->map(function ($item) use ($relatedModelClass) {
340+
return $this->castToEloquent($item, $relatedModelClass);
341+
}));
342+
}
343+
}
338344
}
339345
}
340346
}

0 commit comments

Comments
 (0)