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;
6
7class RefType extends TypeDefinition
8{
9 /**
10 * Reference to another type (NSID or local #def).
11 */
12 public readonly string $ref;
13
14 /**
15 * Create a new RefType.
16 */
17 public function __construct(
18 string $ref,
19 ?string $description = null
20 ) {
21 parent::__construct('ref', $description);
22
23 $this->ref = $ref;
24 }
25
26 /**
27 * Create from array data.
28 */
29 public static function fromArray(array $data): self
30 {
31 if (! isset($data['ref'])) {
32 throw new \InvalidArgumentException('RefType requires a ref property');
33 }
34
35 return new self(
36 ref: $data['ref'],
37 description: $data['description'] ?? null
38 );
39 }
40
41 /**
42 * Convert to array.
43 */
44 public function toArray(): array
45 {
46 $array = [
47 'type' => $this->type,
48 'ref' => $this->ref,
49 ];
50
51 if ($this->description !== null) {
52 $array['description'] = $this->description;
53 }
54
55 return $array;
56 }
57
58 /**
59 * Validate a value against this type definition.
60 */
61 public function validate(mixed $value, string $path = ''): void
62 {
63 // Ref validation requires resolving the reference to its actual type
64 // This would be handled by a higher-level validator with schema repository access
65 // For now, we just accept any value
66 }
67
68 /**
69 * Check if this is a local reference (starts with #).
70 */
71 public function isLocal(): bool
72 {
73 return str_starts_with($this->ref, '#');
74 }
75
76 /**
77 * Check if this is an external reference (contains a dot).
78 */
79 public function isExternal(): bool
80 {
81 return str_contains($this->ref, '.') && ! $this->isLocal();
82 }
83
84 /**
85 * Get the definition name from a local reference.
86 */
87 public function getLocalDefinition(): ?string
88 {
89 if (! $this->isLocal()) {
90 return null;
91 }
92
93 return substr($this->ref, 1);
94 }
95}