|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DoclerLabs\ApiClientGenerator\Entity; |
| 6 | + |
| 7 | +use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\FormUrlencodedContentTypeSerializer; |
| 8 | +use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\JsonContentTypeSerializer; |
| 9 | +use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\VdnApiJsonContentTypeSerializer; |
| 10 | +use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\XmlContentTypeSerializer; |
| 11 | + |
| 12 | +final class ContentType |
| 13 | +{ |
| 14 | + private const ALLOWED_CONTENT_TYPES = [ |
| 15 | + JsonContentTypeSerializer::MIME_TYPE, |
| 16 | + FormUrlencodedContentTypeSerializer::MIME_TYPE, |
| 17 | + XmlContentTypeSerializer::MIME_TYPE, |
| 18 | + VdnApiJsonContentTypeSerializer::MIME_TYPE, |
| 19 | + ]; |
| 20 | + |
| 21 | + private const JSON_SUFFIX = '+json'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Checks if a content type is supported by the generator. |
| 25 | + * |
| 26 | + * Supports: |
| 27 | + * - Standard content types (application/json, application/xml, etc.) |
| 28 | + * - RFC 6839 structured syntax suffixes (+json indicates JSON format) |
| 29 | + * - Content types with parameters (charset, version, etc.) |
| 30 | + */ |
| 31 | + public static function isSupported(string $contentType): bool |
| 32 | + { |
| 33 | + $normalizedContentType = self::normalize($contentType); |
| 34 | + |
| 35 | + if (in_array($normalizedContentType, self::ALLOWED_CONTENT_TYPES, true)) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + // RFC 6839: +json suffix indicates JSON-based format |
| 40 | + return str_ends_with($normalizedContentType, self::JSON_SUFFIX); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Normalizes a content type by removing parameters and converting to lowercase. |
| 45 | + * |
| 46 | + * Example: "Application/JSON; charset=utf-8" becomes "application/json" |
| 47 | + */ |
| 48 | + public static function normalize(string $contentType): string |
| 49 | + { |
| 50 | + return strtolower(trim(explode(';', $contentType)[0])); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks if a content type is JSON-based (either standard or +json suffix). |
| 55 | + */ |
| 56 | + public static function isJsonBased(string $contentType): bool |
| 57 | + { |
| 58 | + $normalizedContentType = self::normalize($contentType); |
| 59 | + |
| 60 | + return $normalizedContentType === JsonContentTypeSerializer::MIME_TYPE |
| 61 | + || str_ends_with($normalizedContentType, self::JSON_SUFFIX); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Filters an array of content types and returns only unsupported ones. |
| 66 | + * |
| 67 | + * @param string[] $contentTypes |
| 68 | + * |
| 69 | + * @return string[] |
| 70 | + */ |
| 71 | + public static function filterUnsupported(array $contentTypes): array |
| 72 | + { |
| 73 | + return array_values( |
| 74 | + array_filter( |
| 75 | + $contentTypes, |
| 76 | + static fn (string $contentType): bool => !self::isSupported($contentType) |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
0 commit comments