diff --git a/ValueResolver/PropelValueResolver.php b/ValueResolver/PropelValueResolver.php new file mode 100644 index 00000000..7552c61a --- /dev/null +++ b/ValueResolver/PropelValueResolver.php @@ -0,0 +1,53 @@ +getType(); + if (!is_subclass_of($className, ActiveRecordInterface::class)) { + return []; + } + + $idParam = lcfirst($this->baseName($className) . 'Id'); + $id = $request->attributes->get($idParam); + if ($id === null) { + return []; + } + + $queryClass = $className . 'Query'; + if (!class_exists($queryClass)) { + return []; + } + + $entity = $queryClass::create()->findPk($id); + + if (!$entity) { + $msg = sprintf( + 'Could not resolve entity (%s: %s) for class: %s', + $idParam, + $id, + $className, + ); + throw new NotFoundHttpException($msg); + } + + return [$entity]; + } + + private function baseName(string $className): string + { + $parts = explode('\\', $className); + return end($parts); + } +} \ No newline at end of file