From 94fef81cb62f3db8daa63702cd48a3334f901721 Mon Sep 17 00:00:00 2001 From: "Hermann D. Schimpf" Date: Wed, 20 Mar 2024 10:52:16 -0300 Subject: [PATCH] Add scopes support --- src/Kitar/Dynamodb/Model/Model.php | 2 +- src/Kitar/Dynamodb/Query/Builder.php | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Kitar/Dynamodb/Model/Model.php b/src/Kitar/Dynamodb/Model/Model.php index 1698f56..6eb4b57 100644 --- a/src/Kitar/Dynamodb/Model/Model.php +++ b/src/Kitar/Dynamodb/Model/Model.php @@ -374,7 +374,7 @@ public function __call($method, $parameters) return $this->$method(...$parameters); } - if (! in_array($method, $allowedBuilderMethods)) { + if (! in_array($method, $allowedBuilderMethods) && ! $this->hasNamedScope($method)) { static::throwBadMethodCallException($method); } diff --git a/src/Kitar/Dynamodb/Query/Builder.php b/src/Kitar/Dynamodb/Query/Builder.php index a1b51ed..68fb2c5 100644 --- a/src/Kitar/Dynamodb/Query/Builder.php +++ b/src/Kitar/Dynamodb/Query/Builder.php @@ -472,9 +472,38 @@ public function __call($method, $parameters) return $this; } + if ($this->hasNamedScope($method)) { + return $this->callNamedScope($method, $parameters); + } + throw new BadMethodCallException('Call to undefined method ' . static::class . "::{$method}()"); } + /** + * Determine if the given model has a scope. + * + * @param string $scope + * + * @return bool + */ + public function hasNamedScope(string $scope): bool + { + return $this->model_class && (new $this->model_class)->hasNamedScope($scope); + } + + /** + * Apply the given named scope on the current builder instance. + * + * @param string $scope + * @param array $parameters + * + * @return mixed + */ + protected function callNamedScope(string $scope, array $parameters = []) + { + return (new $this->model_class)->callNamedScope($scope, array_merge([$this], $parameters)); + } + /** * @inheritdoc */