Skip to content

Commit

Permalink
use HttpMethod attribute instead of @httpMethod annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
dakorpar committed May 7, 2021
1 parent 3884557 commit 1eb00ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"nette/application": "^3.0",
"nette/forms": "^3.1.2",
"nette/reflection": "^2.4",
"wedo/utilities": "^1.0 || ^2.0"
"wedo/utilities": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
15 changes: 15 additions & 0 deletions src/Attributes/HttpMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare (strict_types = 1);

namespace Wedo\Api\Attributes;

#[Attribute]
class HttpMethod
{

public string $value;

public function __construct(string $value)
{
$this->value = $value;
}
}
57 changes: 36 additions & 21 deletions src/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Nette\Application\Responses\JsonResponse;
use Nette\Application\UI\Presenter;
use Nette\Localization\Translator;
use Nette\Reflection\Annotation;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use Psr\Log\LoggerInterface;
Expand All @@ -17,11 +16,11 @@
use ReflectionNamedType;
use ReflectionParameter;
use Throwable;
use Wedo\Api\Attributes\HttpMethod;
use Wedo\Api\Exceptions\BadRequestException;
use Wedo\Api\Exceptions\NotFoundException;
use Wedo\Api\Exceptions\ResponseException;
use Wedo\Api\Exceptions\ValidationException;
use Wedo\Api\Helpers\AnnotationHelper;
use Wedo\Api\Requests\BaseRequest;
use Wedo\Api\Responses\BaseResponse;
use Wedo\Api\Responses\ErrorResponse;
Expand Down Expand Up @@ -107,25 +106,7 @@ protected function tryCall(string $method, array $params): bool
*/
protected function validateRequest(ReflectionMethod $rm): void
{
/** @var Annotation $expectedMethod */
$expectedMethod = AnnotationHelper::getAnnotation($rm, 'httpMethod');
$expectedMethod = strtoupper((string) $expectedMethod);

if ($expectedMethod === '') {
$expectedMethod = 'get';
$params = $rm->getParameters();
/** @phpstan-ignore-next-line */
if (!empty($params)) {
/** @var ReflectionNamedType|null $paramClass */
$paramClass = $params[0]->getType();

if ($paramClass !== null) {
$paramClassName = $paramClass->getName();
if (class_exists($paramClassName) && (new ReflectionClass($paramClassName))->isSubclassOf(BaseRequest::class))
$expectedMethod = 'post';
}
}
}
$expectedMethod = $this->getExpectedHttpMethod($rm);

$request = $this->getHttpRequest();

Expand Down Expand Up @@ -256,4 +237,38 @@ public function injectTranslator(Translator $translator): void
$this->translator = $translator;
}


private function getExpectedHttpMethod(ReflectionMethod $rm): string
{
$attributes = $rm->getAttributes('HttpMethod');

if (count($attributes) > 0) {
/** @var HttpMethod $httpMethod */
$httpMethod = $attributes[0]->newInstance();
return Strings::upper($httpMethod->value);


}

$params = $rm->getParameters();

if (count($params) === 0) {
return 'GET';
}

/** @var ReflectionNamedType|null $paramClass */
$paramClass = $params[0]->getType();

if ($paramClass === NULL) {
return 'GET';
}

$paramClassName = $paramClass->getName();
if (class_exists($paramClassName) && (new ReflectionClass($paramClassName))->isSubclassOf(BaseRequest::class)) {
return 'POST';
}

return 'GET';
}

}

0 comments on commit 1eb00ef

Please sign in to comment.