Laravel AT Protocol Client (alpha & unstable)
1<?php
2
3namespace SocialDept\AtpClient\Data;
4
5use Illuminate\Contracts\Support\Arrayable;
6
7/**
8 * Generic wrapper for AT Protocol records.
9 *
10 * @template T
11 * @implements Arrayable<string, mixed>
12 */
13class Record implements Arrayable
14{
15 /**
16 * @param T $value
17 */
18 public function __construct(
19 public readonly string $uri,
20 public readonly string $cid,
21 public readonly mixed $value,
22 ) {}
23
24 /**
25 * @template U
26 * @param array $data
27 * @param callable(array): U $transformer
28 * @return self<U>
29 */
30 public static function fromArray(array $data, callable $transformer): self
31 {
32 return new self(
33 uri: $data['uri'],
34 cid: $data['cid'],
35 value: $transformer($data['value']),
36 );
37 }
38
39 /**
40 * Create without transforming value.
41 */
42 public static function fromArrayRaw(array $data): self
43 {
44 return new self(
45 uri: $data['uri'],
46 cid: $data['cid'],
47 value: $data['value'],
48 );
49 }
50
51 public function toArray(): array
52 {
53 return [
54 'uri' => $this->uri,
55 'cid' => $this->cid,
56 'value' => $this->value instanceof Arrayable
57 ? $this->value->toArray()
58 : $this->value,
59 ];
60 }
61}