Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
2.0 kB · 75 lines
PHP
at dev
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576<?php
namespace SocialDept\AtpSchema\Parser;
use SocialDept\AtpSchema\Data\TypeDefinition;use SocialDept\AtpSchema\Data\Types\BooleanType;use SocialDept\AtpSchema\Data\Types\BytesType;use SocialDept\AtpSchema\Data\Types\CidLinkType;use SocialDept\AtpSchema\Data\Types\IntegerType;use SocialDept\AtpSchema\Data\Types\NullType;use SocialDept\AtpSchema\Data\Types\StringType;use SocialDept\AtpSchema\Data\Types\UnknownType;use SocialDept\AtpSchema\Exceptions\TypeResolutionException;
class PrimitiveParser{ /** * Parse a primitive type definition from array data. * * @throws TypeResolutionException */ public function parse(array $data): TypeDefinition { $type = $data['type'] ?? null;
if ($type === null) { throw TypeResolutionException::unknownType('(missing type field)'); }
return match ($type) { 'null' => NullType::fromArray($data), 'boolean' => BooleanType::fromArray($data), 'integer' => IntegerType::fromArray($data), 'string' => StringType::fromArray($data), 'bytes' => BytesType::fromArray($data), 'cid-link' => CidLinkType::fromArray($data), 'unknown' => UnknownType::fromArray($data), default => throw TypeResolutionException::unknownType($type), }; }
/** * Check if a type is a primitive type. */ public function isPrimitive(string $type): bool { return in_array($type, [ 'null', 'boolean', 'integer', 'string', 'bytes', 'cid-link', 'unknown', ]); }
/** * Get all supported primitive types. * * @return array<string> */ public function getSupportedTypes(): array { return [ 'null', 'boolean', 'integer', 'string', 'bytes', 'cid-link', 'unknown', ]; }}