Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Data\Types;
4
5use SocialDept\AtpSchema\Data\TypeDefinition;
6use SocialDept\AtpSchema\Exceptions\RecordValidationException;
7
8class NullType extends TypeDefinition
9{
10 /**
11 * Create a new NullType.
12 */
13 public function __construct(?string $description = null)
14 {
15 parent::__construct('null', $description);
16 }
17
18 /**
19 * Create from array data.
20 */
21 public static function fromArray(array $data): self
22 {
23 return new self(
24 description: $data['description'] ?? null
25 );
26 }
27
28 /**
29 * Convert to array.
30 */
31 public function toArray(): array
32 {
33 $array = ['type' => $this->type];
34
35 if ($this->description !== null) {
36 $array['description'] = $this->description;
37 }
38
39 return $array;
40 }
41
42 /**
43 * Validate a value against this type definition.
44 */
45 public function validate(mixed $value, string $path = ''): void
46 {
47 if ($value !== null) {
48 throw RecordValidationException::invalidType($path, 'null', gettype($value));
49 }
50 }
51}