Laravel AT Protocol Client (alpha & unstable)
1<?php
2
3namespace SocialDept\AtpClient\Data;
4
5use Illuminate\Contracts\Support\Arrayable;
6use Illuminate\Support\Collection;
7
8/**
9 * Collection wrapper for paginated AT Protocol records.
10 *
11 * @template T
12 * @implements Arrayable<string, mixed>
13 */
14class RecordCollection implements Arrayable
15{
16 /**
17 * @param Collection<int, Record<T>> $records
18 */
19 public function __construct(
20 public readonly Collection $records,
21 public readonly ?string $cursor = null,
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 records: collect($data['records'] ?? [])->map(
34 fn (array $record) => Record::fromArray($record, $transformer)
35 ),
36 cursor: $data['cursor'] ?? null,
37 );
38 }
39
40 /**
41 * Create without transforming values.
42 */
43 public static function fromArrayRaw(array $data): self
44 {
45 return new self(
46 records: collect($data['records'] ?? [])->map(
47 fn (array $record) => Record::fromArrayRaw($record)
48 ),
49 cursor: $data['cursor'] ?? null,
50 );
51 }
52
53 public function toArray(): array
54 {
55 return [
56 'records' => $this->records->map(fn (Record $r) => $r->toArray())->all(),
57 'cursor' => $this->cursor,
58 ];
59 }
60}