Skip to content

Commit 0432151

Browse files
committed
Save more progress on swagger generator
1 parent e8163da commit 0432151

7 files changed

Lines changed: 254 additions & 105 deletions

File tree

src/Enum/InputParamType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpApi\Enum;
44

5+
use InvalidArgumentException;
56
use PhpApi\Model\Request\Attribute\InputParam;
67
use PhpApi\Model\Request\Attribute\JsonRequestParam;
78
use PhpApi\Model\Request\Attribute\QueryParam;
@@ -24,4 +25,13 @@ public static function fromClassInstance(mixed $instance): ?self
2425

2526
return null;
2627
}
28+
29+
public function toContentType(): string
30+
{
31+
return match ($this) {
32+
self::Input => 'application/x-www-form-urlencoded',
33+
self::Json => 'application/json',
34+
default => throw new InvalidArgumentException('Invalid content type'),
35+
};
36+
}
2737
}

src/Model/Request/AbstractRequest.php

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace PhpApi\Model\Request;
44

5-
use InvalidArgumentException;
6-
use PhpApi\Enum\HttpMethod;
75
use PhpApi\Enum\InputParamType;
8-
use ReflectionClass;
9-
use ReflectionNamedType;
106
use Sapien\Request;
117

128
abstract class AbstractRequest
@@ -18,7 +14,7 @@ public function __construct(
1814
) {
1915
$this->request = $request;
2016

21-
$paramTypes = $this->getParamTypes($this->request->method->name);
17+
$paramTypes = RequestParser::getParamTypes(self::class, $this->request->method->name);
2218

2319
foreach ($paramTypes as $paramType) {
2420
if ($paramType->type === InputParamType::Query) {
@@ -39,58 +35,4 @@ public function __construct(
3935
}
4036
}
4137
}
42-
43-
/**
44-
* @return RequestProperty[]
45-
* @throws InvalidArgumentException
46-
*/
47-
public static function getParamTypes(?string $method): array
48-
{
49-
if ($method === null) {
50-
throw new InvalidArgumentException('Method cannot be null when getting param types for request');
51-
}
52-
53-
$result = [];
54-
55-
$reflectionClass = new ReflectionClass(static::class);
56-
$properties = $reflectionClass->getProperties();
57-
foreach ($properties as $property) {
58-
$propertyType = $property->getType();
59-
60-
if (!($propertyType instanceof ReflectionNamedType)) {
61-
throw new InvalidArgumentException("Property type must be a named type. Cannot be null or union type");
62-
} elseif ($propertyType->getName() === Request::class) {
63-
continue;
64-
}
65-
66-
$attributes = $property->getAttributes();
67-
foreach ($attributes as $attribute) {
68-
$attributeInstance = $attribute->newInstance();
69-
$inputParamType = InputParamType::fromClassInstance($attributeInstance);
70-
if ($inputParamType === null) {
71-
continue;
72-
}
73-
$name = $attributeInstance->name;
74-
break;
75-
}
76-
77-
if (!isset($name)) {
78-
$name = $property->getName();
79-
}
80-
81-
if (!isset($inputParamType)) {
82-
$inputParamType = in_array($method, HttpMethod::getQueryOnlyMethods())
83-
? InputParamType::Query
84-
: InputParamType::Json;
85-
}
86-
87-
$result[] = new RequestProperty(
88-
name: $name,
89-
propertyName: $property->getName(),
90-
type: $inputParamType,
91-
);
92-
}
93-
94-
return $result;
95-
}
9638
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace PhpApi\Model\Request;
4+
5+
use InvalidArgumentException;
6+
use PhpApi\Enum\HttpMethod;
7+
use PhpApi\Enum\InputParamType;
8+
use ReflectionClass;
9+
use ReflectionNamedType;
10+
use Sapien\Request;
11+
12+
class RequestParser
13+
{
14+
/**
15+
* @return RequestProperty[]
16+
* @throws InvalidArgumentException
17+
*/
18+
public static function getParamTypes(string $requestClass, ?string $method): array
19+
{
20+
if ($method === null) {
21+
throw new InvalidArgumentException('Method cannot be null when getting param types for request');
22+
}
23+
24+
$result = [];
25+
26+
$reflectionClass = new ReflectionClass($requestClass);
27+
$properties = $reflectionClass->getProperties();
28+
foreach ($properties as $property) {
29+
$propertyType = $property->getType();
30+
31+
if (!($propertyType instanceof ReflectionNamedType)) {
32+
throw new InvalidArgumentException("Property type must be a named type. Cannot be null or union type");
33+
} elseif ($propertyType->getName() === Request::class) {
34+
continue;
35+
}
36+
37+
$attributes = $property->getAttributes();
38+
foreach ($attributes as $attribute) {
39+
$attributeInstance = $attribute->newInstance();
40+
$inputParamType = InputParamType::fromClassInstance($attributeInstance);
41+
if ($inputParamType === null) {
42+
continue;
43+
}
44+
$name = $attributeInstance->name;
45+
break;
46+
}
47+
48+
if (!isset($name)) {
49+
$name = $property->getName();
50+
}
51+
52+
if (!isset($inputParamType)) {
53+
$inputParamType = in_array($method, HttpMethod::getQueryOnlyMethods())
54+
? InputParamType::Query
55+
: InputParamType::Json;
56+
}
57+
58+
$result[] = new RequestProperty(
59+
name: $name,
60+
propertyName: $property->getName(),
61+
type: $inputParamType,
62+
);
63+
}
64+
65+
return $result;
66+
}
67+
}

0 commit comments

Comments
 (0)