Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
1.7 kB · 66 lines
PHP
at dev
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667<?php
namespace SocialDept\AtpSchema\Data\Types;
use SocialDept\AtpSchema\Data\TypeDefinition;use SocialDept\AtpSchema\Exceptions\RecordValidationException;
class CidLinkType extends TypeDefinition{ /** * Create a new CidLinkType. */ public function __construct(?string $description = null) { parent::__construct('cid-link', $description); }
/** * Create from array data. */ public static function fromArray(array $data): self { return new self( description: $data['description'] ?? null ); }
/** * Convert to array. */ public function toArray(): array { $array = ['type' => $this->type];
if ($this->description !== null) { $array['description'] = $this->description; }
return $array; }
/** * Validate a value against this type definition. */ public function validate(mixed $value, string $path = ''): void { if (! is_array($value)) { throw RecordValidationException::invalidType($path, 'cid-link (object with $link)', gettype($value)); }
if (! isset($value['$link'])) { throw RecordValidationException::invalidValue($path, 'must contain $link property'); }
$link = $value['$link'];
if (! is_string($link)) { throw RecordValidationException::invalidValue($path, '$link must be a string'); }
// Basic CID validation if (! preg_match('/^[a-zA-Z0-9]+$/', $link)) { throw RecordValidationException::invalidValue($path, '$link must be a valid CID'); } }}