From ade476ec44b10364adbaf735f82fa2330812f7b4 Mon Sep 17 00:00:00 2001 From: Ruslan Samburov Date: Fri, 26 Dec 2025 11:17:47 +0300 Subject: [PATCH 1/2] edit magic function BaseApiModel::__set --- .gitignore | 2 ++ src/AmoCRM/EntitiesServices/Webhooks.php | 4 ++-- src/AmoCRM/EntitiesServices/Widgets.php | 17 ++++++----------- src/AmoCRM/Models/BaseApiModel.php | 20 ++++++++++++++++++-- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index c2f6e62b..fb4d9490 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ composer.lock examples/.env .phpunit.result.cache /.DS_Store +.vscode +index.php \ No newline at end of file diff --git a/src/AmoCRM/EntitiesServices/Webhooks.php b/src/AmoCRM/EntitiesServices/Webhooks.php index c49ce500..10338b6c 100644 --- a/src/AmoCRM/EntitiesServices/Webhooks.php +++ b/src/AmoCRM/EntitiesServices/Webhooks.php @@ -151,8 +151,8 @@ public function subscribe(WebhookModel $webhookModel): WebhookModel */ public function unsubscribe(WebhookModel $webhookModel): bool { - $result = $this->request->delete($this->getMethod(), $webhookModel->toUnsubscribeApi()); + $response = $this->request->delete($this->getMethod(), $webhookModel->toUnsubscribeApi()); - return $result['result']; + return $response['result']; } } diff --git a/src/AmoCRM/EntitiesServices/Widgets.php b/src/AmoCRM/EntitiesServices/Widgets.php index e42d2f2c..081178b3 100644 --- a/src/AmoCRM/EntitiesServices/Widgets.php +++ b/src/AmoCRM/EntitiesServices/Widgets.php @@ -129,7 +129,8 @@ public function install(WidgetModel $widgetModel): WidgetModel $widgetModel->toApi() ); - foreach ($response as $key => $value) { + foreach ($response as $key => $value) + { $widgetModel->$key = $value; } @@ -141,20 +142,14 @@ public function install(WidgetModel $widgetModel): WidgetModel * * @param WidgetModel $widgetModel * - * @return WidgetModel + * @return bool * @throws AmoCRMApiException * @throws AmoCRMoAuthApiException */ - public function uninstall(WidgetModel $widgetModel): WidgetModel + public function uninstall(WidgetModel $widgetModel): bool { - $response = $this->request->delete( - $this->getMethod() . '/' . $widgetModel->getCode() - ); - - foreach ($response as $key => $value) { - $widgetModel->$key = $value; - } + $response = $this->request->delete($this->getMethod() . '/' . $widgetModel->getCode()); - return $widgetModel; + return $response['result']; } } diff --git a/src/AmoCRM/Models/BaseApiModel.php b/src/AmoCRM/Models/BaseApiModel.php index afd267df..771c8880 100644 --- a/src/AmoCRM/Models/BaseApiModel.php +++ b/src/AmoCRM/Models/BaseApiModel.php @@ -37,10 +37,26 @@ public function __get($name) } } - public function __set($name, $value) + public function __set($name, $value): void { $methodName = 'set' . Str::camel(Str::ucfirst($name)); - if (method_exists($this, $methodName) && is_callable([$this, $methodName])) { + + if (method_exists($this, $methodName) && is_callable([$this, $methodName])) + { + $method = new \ReflectionMethod($this, $methodName); + $param = $method->getParameters()[0] ?? null; + $type = $param?->getType(); + + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) + { + $class = $type->getName(); + + if (is_array($value) && method_exists($class, 'fromArray')) + { + $value = $class::fromArray($value); + } + } + $this->$methodName($value); } } From ba69530a699c4e0f8ae7e63f1fc3e155ed403e76 Mon Sep 17 00:00:00 2001 From: Ruslan Samburov Date: Fri, 26 Dec 2025 18:17:30 +0300 Subject: [PATCH 2/2] rollback AmoCRM\EntitiesServices --- src/AmoCRM/EntitiesServices/Widgets.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/AmoCRM/EntitiesServices/Widgets.php b/src/AmoCRM/EntitiesServices/Widgets.php index 081178b3..e42d2f2c 100644 --- a/src/AmoCRM/EntitiesServices/Widgets.php +++ b/src/AmoCRM/EntitiesServices/Widgets.php @@ -129,8 +129,7 @@ public function install(WidgetModel $widgetModel): WidgetModel $widgetModel->toApi() ); - foreach ($response as $key => $value) - { + foreach ($response as $key => $value) { $widgetModel->$key = $value; } @@ -142,14 +141,20 @@ public function install(WidgetModel $widgetModel): WidgetModel * * @param WidgetModel $widgetModel * - * @return bool + * @return WidgetModel * @throws AmoCRMApiException * @throws AmoCRMoAuthApiException */ - public function uninstall(WidgetModel $widgetModel): bool + public function uninstall(WidgetModel $widgetModel): WidgetModel { - $response = $this->request->delete($this->getMethod() . '/' . $widgetModel->getCode()); + $response = $this->request->delete( + $this->getMethod() . '/' . $widgetModel->getCode() + ); + + foreach ($response as $key => $value) { + $widgetModel->$key = $value; + } - return $response['result']; + return $widgetModel; } }