/
jagepard
/
Rudra-Model
Обзор
Документация
Войти
/
jagepard
/
Rudra-Model
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Model.php
54 строки
2 KB
Jagepard
feat(model): allow direct instantiation of Entity and Model
14 июл 2026, 16:43
14 июл 2026, 16:43
b7c4165
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. * * @author Korotkov Danila (Jagepard) <jagepard@yandex.ru> * @license https://mozilla.org/MPL/2.0/ MPL-2.0 */ namespace Rudra\Model; use Rudra\Exceptions\RudraException; /** * @mixin \Rudra\Model\Repository */ class Model { public ?string $table; public function __construct(?string $table = null) { $this->table = $table; } /** * Handles calls to undefined methods by delegating them to the corresponding Repository class. * The method dynamically resolves the Repository class associated with the Model. * If the Repository does not exist, it falls back to the parent Repository class. * If the method exists in the resolved Repository, it is invoked with the provided parameters. * Otherwise, an exception is thrown. * * @throws RudraException */ public function __call(string $method, array $parameters = []): mixed { $className = str_replace("Model", "Repository", get_called_class()) . "Repository"; // If there is no Repository, then call the parent Repository if (!class_exists($className)) { $className = Repository::class; } $newInstance = new $className($this->table); if (method_exists($newInstance, $method)) { return $newInstance->$method(...$parameters); } throw new RudraException(sprintf('Method %s does not exists', $method)); } }