Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
3.1 kB · 143 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144<?php
namespace SocialDept\AtpSchema\Data;
use Illuminate\Contracts\Support\Arrayable;use SocialDept\AtpSchema\Exceptions\SchemaValidationException;
/** * @implements Arrayable<string, mixed> */class BlobReference implements Arrayable{ /** * CID of the blob. */ public readonly string $ref;
/** * MIME type of the blob. */ public readonly string $mimeType;
/** * Size of the blob in bytes. */ public readonly int $size;
/** * Create a new BlobReference. */ public function __construct( string $ref, string $mimeType, int $size ) { $this->ref = $ref; $this->mimeType = $mimeType; $this->size = $size; }
/** * Create from array data. */ public static function fromArray(array $data): self { // Extract ref - can be a string or array with $link $ref = null; if (isset($data['ref'])) { if (is_string($data['ref'])) { $ref = $data['ref']; } elseif (is_array($data['ref']) && isset($data['ref']['$link'])) { $ref = $data['ref']['$link']; } }
if ($ref === null) { throw SchemaValidationException::missingField('blob', 'ref'); }
if (! isset($data['mimeType'])) { throw SchemaValidationException::missingField('blob', 'mimeType'); }
if (! isset($data['size'])) { throw SchemaValidationException::missingField('blob', 'size'); }
return new self( ref: $ref, mimeType: $data['mimeType'], size: (int) $data['size'] ); }
/** * Convert to array. */ public function toArray(): array { return [ '$type' => 'blob', 'ref' => [ '$link' => $this->ref, ], 'mimeType' => $this->mimeType, 'size' => $this->size, ]; }
/** * Get the CID. */ public function getCid(): string { return $this->ref; }
/** * Get the MIME type. */ public function getMimeType(): string { return $this->mimeType; }
/** * Get the size in bytes. */ public function getSize(): int { return $this->size; }
/** * Check if blob is an image. */ public function isImage(): bool { return str_starts_with($this->mimeType, 'image/'); }
/** * Check if blob is a video. */ public function isVideo(): bool { return str_starts_with($this->mimeType, 'video/'); }
/** * Check if blob matches a MIME type pattern. */ public function matchesMimeType(string $pattern): bool { if (str_contains($pattern, '*')) { $regex = '/^'.str_replace('\\*', '.*', preg_quote($pattern, '/')).'$/';
return (bool) preg_match($regex, $this->mimeType); }
return $this->mimeType === $pattern; }}