Laravel AT Protocol Client (alpha & unstable)
1<?php
2
3namespace SocialDept\AtpClient\RichText;
4
5use SocialDept\AtpClient\Builders\Concerns\BuildsRichText;
6
7class TextBuilder
8{
9 use BuildsRichText;
10
11 /**
12 * Create a new text builder instance
13 */
14 public static function make(): self
15 {
16 return new self;
17 }
18
19 /**
20 * Build text using a callback
21 */
22 public static function build(callable $callback): array
23 {
24 $builder = new self;
25 $callback($builder);
26
27 return $builder->toArray();
28 }
29
30 /**
31 * Build the final text and facets array
32 */
33 public function toArray(): array
34 {
35 return $this->getTextAndFacets();
36 }
37
38 /**
39 * Convert to JSON string
40 */
41 public function toJson(int $options = 0): string
42 {
43 return json_encode($this->toArray(), $options);
44 }
45
46 /**
47 * Create from existing text with auto-detection
48 */
49 public static function parse(string $text): array
50 {
51 return [
52 'text' => $text,
53 'facets' => FacetDetector::detect($text),
54 ];
55 }
56
57 /**
58 * Get character count (for post limits)
59 */
60 public function getCharacterCount(): int
61 {
62 return mb_strlen($this->text, 'UTF-8');
63 }
64
65 /**
66 * Get byte count
67 */
68 public function getByteCount(): int
69 {
70 return strlen($this->text);
71 }
72
73 /**
74 * Convert to string (returns text only)
75 */
76 public function __toString(): string
77 {
78 return $this->text;
79 }
80}