Skip to content

Commit

Permalink
handle content type
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Sep 15, 2024
1 parent 6e18c81 commit f344f40
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Parser/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Api\Parser;

use Psr\Http\Message\StreamInterface;
use PSX\Api\Attribute as Attr;
use PSX\Api\Attribute\ParamAbstract;
use PSX\Api\Exception\InvalidArgumentException;
Expand All @@ -32,6 +33,7 @@
use PSX\Api\Specification;
use PSX\Api\SpecificationInterface;
use PSX\Api\Util\Inflection;
use PSX\Data\Multipart\Body;
use PSX\DateTime\Duration;
use PSX\DateTime\LocalDate;
use PSX\DateTime\LocalDateTime;
Expand Down Expand Up @@ -354,11 +356,23 @@ private function resolveDescription(Attr\Description $attribute): string
}
}

private function getSchemaFromTypeHint(?\ReflectionType $type): ?string
private function getSchemaFromTypeHint(?\ReflectionType $type): string|ContentType|null
{
if ($type instanceof \ReflectionNamedType) {
if ($type->getName() === 'mixed') {
return Passthru::class;
} elseif ($type->getName() === \DOMDocument::class) {
return ContentType::XML;
} elseif ($type->getName() === \stdClass::class) {
return ContentType::JSON;
} elseif ($type->getName() === StreamInterface::class) {
return ContentType::BINARY;
} elseif ($type->getName() === 'string') {
return ContentType::TEXT;
} elseif ($type->getName() === Body::class) {
return ContentType::MULTIPART;
} elseif ($type->getName() === 'array') {
return ContentType::FORM;
} elseif (class_exists($type->getName())) {
return $type->getName();
}
Expand Down Expand Up @@ -412,7 +426,7 @@ private function inspectTypeHints(\ReflectionMethod $method, Meta $meta): void
} elseif (!$meta->hasIncoming() && in_array($meta->getMethod()->method, ['POST', 'PUT', 'PATCH'])) {
$schema = $this->getSchemaFromTypeHint($parameter->getType());
if (!empty($schema)) {
if (!class_exists($schema)) {
if (!$schema instanceof ContentType && !class_exists($schema)) {
throw new ParserException('The method ' . $method->getName() . ' contains an argument "' . $parameter->getName() . '" which has as type-hint a non existing class "' . $schema . '"');
}

Expand All @@ -437,7 +451,7 @@ private function inspectTypeHints(\ReflectionMethod $method, Meta $meta): void
// if we have no outgoing attribute we parse it from the return type hint
if (!$meta->hasOutgoing()) {
$schema = $this->getSchemaFromTypeHint($method->getReturnType());
if (!empty($schema) && class_exists($schema)) {
if (!empty($schema) && ($schema instanceof ContentType || class_exists($schema))) {
$meta->addOutgoing(new Attr\Outgoing($meta->getStatusCode()?->code ?? 200, $schema));
}
}
Expand Down Expand Up @@ -490,7 +504,7 @@ private function getFromAttribute(\ReflectionParameter $parameter, \ReflectionMe
$schema = $this->getSchemaFromTypeHint($parameter->getType());
if (empty($schema)) {
throw new ParserException('The method ' . $method->getName() . ' contains an argument "' . $parameter->getName() . '" which is marked as body but has an invalid type-hint');
} elseif (!class_exists($schema)) {
} elseif (!$schema instanceof ContentType && !class_exists($schema)) {
throw new ParserException('The method ' . $method->getName() . ' contains an argument "' . $parameter->getName() . '" which has as type-hint a non existing class "' . $schema . '"');
}

Expand Down

0 comments on commit f344f40

Please sign in to comment.