Repository navigation
Expand file tree
/
Copy pathPanelView.php
More file actions
741 lines (674 loc) · 25.9 KB
/
Copy pathPanelView.php
File metadata and controls
741 lines (674 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<?php
declare(strict_types=1);
namespace PHPForge\Debug;
use InvalidArgumentException;
use PHPForge\Debug\Exception\PanelViewMessage;
use function array_is_list;
use function array_values;
use function is_array;
use function is_float;
use function is_int;
use function is_string;
/**
* Builds immutable panel descriptions without exposing the host's markup or styles.
*
* Every method validates its arguments and returns a new view. Ordinary overview and table values need no wrappers:
* scalars become text, with literal text for `null`, `true`, and `false`. Styled text, badges, and structured values
* come from the static factories and are accepted wherever a scalar is accepted.
*
* The finished description is a tree of `PHPForge\Debug\Presenter` value objects. The host reads it through
* {@see self::summaryMetrics()}, {@see self::toolbarMetrics()}, and {@see self::blocks()}, then narrows each value
* with `instanceof` over the sealed {@see Block} and {@see Inline} unions.
*/
final readonly class PanelView
{
/**
* @param list<Presenter\SummaryMetric> $summary Summary metrics in display order.
* @param list<Presenter\Block> $blocks Panel content blocks in display order.
* @param list<Presenter\ToolbarMetric> $toolbar Toolbar metrics in display order, separate from the summary.
*/
private function __construct(
private array $summary,
private array $blocks,
private array $toolbar,
) {}
/**
* Creates an inline status label with a semantic tone.
*
* @param string $label Badge text; the host escapes it.
* @param Tone $tone Semantic tone interpreted by the host frontend.
*
* @return Presenter\BadgeInline Inline badge accepted by every content method.
*/
public static function badge(string $label, Tone $tone = Tone::MUTED): Presenter\BadgeInline
{
return new Presenter\BadgeInline($label, $tone);
}
/**
* Returns the content blocks for the host renderer.
*
* @return list<Presenter\Block> Validated content blocks in display order.
*/
public function blocks(): array
{
return $this->blocks;
}
/**
* Appends a paragraph with a semantic callout tone.
*
* @param Tone $tone Callout tone interpreted by the host frontend.
* @param mixed ...$content Ordered factory-produced inline values, scalars, or `null`.
*
* @throws InvalidArgumentException if an argument is not an accepted inline value.
*
* @return self New view with the callout appended.
*/
public function callout(Tone $tone, mixed ...$content): self
{
return $this->append(self::paragraphBlock($content, $tone));
}
/**
* Appends a card describing one entity, optionally split into titled columns of its own content.
*
* @param string $id Anchor the host emits so other blocks can link to the card, or `''` to omit it.
* @param string $icon Host icon key shown before the title, or `''` to omit it.
* @param string $title Entity name shown as the card heading; the host escapes it.
* @param string $subtitle Qualifier shown under the title, or `''` to omit it.
* @param array<array-key, mixed> $meta Inline values or scalars shown beside the title, such as counts.
* @param Presenter\ColumnEntry ...$columns Entries produced by {@see self::column()}.
*
* @throws InvalidArgumentException if a meta value is not an accepted inline value.
*
* @return self New view with the card appended.
*/
public function card(
string $id,
string $icon,
string $title,
string $subtitle,
array $meta,
Presenter\ColumnEntry ...$columns
): self {
$inline = [];
foreach ($meta as $value) {
$inline[] = self::inline($value);
}
return $this->append(
new Presenter\CardBlock(
$id,
$icon,
$title,
$subtitle,
$inline,
array_values($columns),
),
);
}
/**
* Creates inline text presented as source code.
*
* @param string $value Text content; the host escapes it.
*
* @return Presenter\TextInline Inline text accepted by every content method.
*/
public static function code(string $value): Presenter\TextInline
{
return new Presenter\TextInline($value, Presenter\TextStyle::CODE);
}
/**
* Creates one titled column of a card body.
*
* @param string $title Column heading announced as its accessible name.
* @param self $content Child view contributing only its ordered content blocks.
*
* @return Presenter\ColumnEntry Column entry accepted by {@see self::card()}.
*/
public static function column(string $title, self $content): Presenter\ColumnEntry
{
return new Presenter\ColumnEntry($title, $content);
}
/**
* Creates a view without content or metrics as the starting point for fluent composition.
*
* @return self Empty view.
*/
public static function create(): self
{
return new self([], [], []);
}
/**
* Appends a titled plain-text disclosure.
*
* @param string $title Disclosure label.
* @param string $content Plain-text payload, preserved without formatting.
*
* @return self New view with the disclosure appended.
*/
public function disclosure(string $title, string $content): self
{
return $this->append(new Presenter\DisclosureBlock($title, $content));
}
/**
* Appends an explicit empty state whose arguments each describe one paragraph.
*
* @param string $title Empty-state heading.
* @param mixed ...$paragraphs Ordered explanations, each one paragraph: a scalar, a factory-produced inline value,
* or a list of inline values and scalars.
*
* @throws InvalidArgumentException if a paragraph is neither an inline value nor a list of them.
*
* @return self New view with the empty state appended.
*/
public function emptyState(string $title, mixed ...$paragraphs): self
{
$content = [];
foreach ($paragraphs as $paragraph) {
$content[] = self::paragraphOf($paragraph);
}
return $this->append(new Presenter\EmptyStateBlock($title, $content));
}
/**
* Creates one compact label and value pair of a fact strip.
*
* @param string $label Name of the fact; the host escapes it.
* @param string $value Recorded value; the host escapes it.
*
* @return Presenter\FactEntry Fact entry accepted by {@see self::facts()}.
*/
public static function fact(string $label, string $value): Presenter\FactEntry
{
return new Presenter\FactEntry($label, $value);
}
/**
* Appends a compact strip of label and value pairs.
*
* @param Presenter\FactEntry ...$facts Entries produced by {@see self::fact()}.
*
* @return self New view with the fact strip appended.
*/
public function facts(Presenter\FactEntry ...$facts): self
{
return $this->append(new Presenter\FactsBlock(array_values($facts)));
}
/**
* Creates one typed file entry of a file list.
*
* @param string $type Short kind label shown as a pill, such as `.css`; the host escapes it.
* @param string $name File name or URL; the host escapes it.
* @param Tone $tone Semantic tone interpreted by the host frontend.
*
* @return Presenter\FileEntry File entry accepted by {@see self::files()}.
*/
public static function file(string $type, string $name, Tone $tone = Tone::MUTED): Presenter\FileEntry
{
return new Presenter\FileEntry($type, $name, $tone);
}
/**
* Appends a list of typed file names.
*
* @param Presenter\FileEntry ...$files Entries produced by {@see self::file()}.
*
* @return self New view with the file list appended.
*/
public function files(Presenter\FileEntry ...$files): self
{
return $this->append(new Presenter\FilesBlock(array_values($files)));
}
/**
* Groups only the child's content; metrics belong to the root view.
*
* @param string $label Accessible group label.
* @param self $content Child view contributing only its ordered content blocks.
*
* @return self New view with the group appended and the current metrics preserved.
*/
public function group(string $label, self $content): self
{
return $this->append(new Presenter\GroupBlock($label, $content));
}
/**
* Appends an ordinary or section-level heading.
*
* @param string $title Heading text.
* @param bool $section Whether to request section-heading presentation from the host.
*
* @return self New view with the heading appended.
*/
public function heading(string $title, bool $section = false): self
{
return $this->append(new Presenter\HeadingBlock($title, $section));
}
/**
* Creates an inline navigation link the host renders as an anchor.
*
* Only relative targets and the `http`, `https`, and `mailto` schemes are accepted, so a captured value can never
* turn into an executable target.
*
* @param string $label Link text; the host escapes it.
* @param string $href Relative target, or an absolute `http`, `https`, or `mailto` URL.
* @param bool $external Whether the host opens the target in a new browsing context.
*
* @throws InvalidArgumentException if the target declares a scheme the host must not follow.
*
* @return Presenter\LinkInline Inline link accepted by every content method.
*/
public static function link(string $label, string $href, bool $external = false): Presenter\LinkInline
{
return new Presenter\LinkInline($label, $href, $external);
}
/**
* Appends a labeled strip of navigation links.
*
* @param string $label Text introducing the strip, such as `Depends on 2`; the host escapes it.
* @param Presenter\LinkInline ...$links Inline values produced by {@see self::link()}.
*
* @return self New view with the link strip appended.
*/
public function links(string $label, Presenter\LinkInline ...$links): self
{
return $this->append(new Presenter\LinksBlock($label, array_values($links)));
}
/**
* Appends a vendor-grouped package manifest.
*
* @param string $label Vendor prefix the packages share, shown as the group heading.
* @param Presenter\PackageEntry ...$packages Entries produced by {@see self::package()}.
*
* @return self New view with the manifest appended.
*/
public function manifest(string $label, Presenter\PackageEntry ...$packages): self
{
return $this->append(new Presenter\ManifestBlock($label, array_values($packages)));
}
/**
* Appends labeled overview fields, using array keys as labels.
*
* Labels are unique because they are array keys; repeat a value under a different label instead.
*
* @param array<array-key, mixed> $values Labeled values in display order, each an inline value or a scalar.
* @param bool $compact Whether to request compact presentation from the host.
*
* @throws InvalidArgumentException if a value is not an accepted inline value.
*
* @return self New view with the overview appended.
*/
public function overview(array $values, bool $compact = false): self
{
$fields = [];
foreach ($values as $label => $value) {
$fields[] = new Presenter\FieldEntry((string) $label, self::inline($value));
}
return $this->append(new Presenter\OverviewBlock($fields, $compact));
}
/**
* Creates one package entry of a manifest.
*
* @param string $name Package name; the host escapes it.
* @param string $version Resolved version; the host escapes it.
*
* @return Presenter\PackageEntry Package entry accepted by {@see self::manifest()}.
*/
public static function package(string $name, string $version): Presenter\PackageEntry
{
return new Presenter\PackageEntry($name, $version);
}
/**
* Appends an ordinary paragraph, converting plain values to text.
*
* @param mixed ...$content Ordered factory-produced inline values, scalars, or `null`.
*
* @throws InvalidArgumentException if an argument is not an accepted inline value.
*
* @return self New view with the paragraph appended.
*/
public function paragraph(mixed ...$content): self
{
return $this->append(self::paragraphBlock($content, null));
}
/**
* Creates one status pill.
*
* @param string $label Subject of the pill, such as an extension name; the host escapes it.
* @param string $state Short state text shown after the label, such as `on` or a version.
* @param bool $enabled Whether the subject is active, selecting the on or off presentation.
*
* @return Presenter\PillEntry Pill entry accepted by {@see self::pills()}.
*/
public static function pill(string $label, string $state, bool $enabled): Presenter\PillEntry
{
return new Presenter\PillEntry($label, $state, $enabled);
}
/**
* Appends a strip of status pills.
*
* @param Presenter\PillEntry ...$pills Entries produced by {@see self::pill()}.
*
* @return self New view with the pill strip appended.
*/
public function pills(Presenter\PillEntry ...$pills): self
{
return $this->append(new Presenter\PillsBlock(array_values($pills)));
}
/**
* Creates inline text the host may clamp behind its standard expand control.
*
* @param string $value Text content; the host escapes it.
*
* @return Presenter\TextInline Inline text accepted by every content method.
*/
public static function preview(string $value): Presenter\TextInline
{
return new Presenter\TextInline($value, Presenter\TextStyle::PREVIEW);
}
/**
* Creates one headline readout card.
*
* @param string $label Metric name shown above the value; the host escapes it.
* @param string $value Headline value; the host escapes it.
* @param string $caption Qualifier shown under the value, or `''` to omit it.
*
* @return Presenter\ReadoutEntry Readout entry accepted by {@see self::readouts()}.
*/
public static function readout(string $label, string $value, string $caption = ''): Presenter\ReadoutEntry
{
return new Presenter\ReadoutEntry($label, $value, $caption);
}
/**
* Appends a row of headline readout cards.
*
* @param Presenter\ReadoutEntry ...$readouts Entries produced by {@see self::readout()}.
*
* @return self New view with the readout row appended.
*/
public function readouts(Presenter\ReadoutEntry ...$readouts): self
{
return $this->append(new Presenter\ReadoutsBlock(array_values($readouts)));
}
/**
* Appends a titled section wrapping its own content.
*
* @param string $mark Short glyph shown before the title, such as `::` or `//`.
* @param string $title Section title announced as its accessible name.
* @param self $content Blocks the section wraps.
* @param int|null $count Optional tally shown at the end of the title, or `null` to omit it.
*
* @return self New view with the section appended.
*/
public function section(string $mark, string $title, self $content, int|null $count = null): self
{
return $this->append(new Presenter\SectionBlock($mark, $title, $count, $content));
}
/**
* Creates inline text the host highlights as an SQL statement.
*
* @param string $value Statement text; the host escapes it.
*
* @return Presenter\TextInline Inline text accepted by every content method.
*/
public static function sql(string $value): Presenter\TextInline
{
return new Presenter\TextInline($value, Presenter\TextStyle::SQL);
}
/**
* Creates one headline stat tile.
*
* @param string $icon Host icon key shown above the value.
* @param string $label Metric name shown under the value; the host escapes it.
* @param string $value Headline value; the host escapes it.
* @param Tone $tone Semantic tone interpreted by the host frontend, or {@see Tone::MUTED} to keep its own accent.
*
* @return Presenter\StatEntry Stat entry accepted by {@see self::stats()}.
*/
public static function stat(string $icon, string $label, string $value, Tone $tone = Tone::MUTED): Presenter\StatEntry
{
return new Presenter\StatEntry($icon, $label, $value, $tone);
}
/**
* Appends a strip of headline stat tiles.
*
* @param Presenter\StatEntry ...$stats Entries produced by {@see self::stat()}.
*
* @return self New view with the stat strip appended.
*/
public function stats(Presenter\StatEntry ...$stats): self
{
return $this->append(new Presenter\StatsBlock(array_values($stats)));
}
/**
* Creates emphasized inline text.
*
* @param string $value Text content; the host escapes it.
*
* @return Presenter\TextInline Inline text accepted by every content method.
*/
public static function strong(string $value): Presenter\TextInline
{
return new Presenter\TextInline($value, Presenter\TextStyle::STRONG);
}
/**
* Adds a metric whose label follows its value. Include any intended spacing in the label.
*
* @param string $label Suffix displayed after the metric value.
* @param string|int|float $value Metric value converted to text.
* @param bool $emphasized Whether to request emphasis for the value.
*
* @return self New view with the summary metric appended.
*/
public function summary(string $label, string|int|float $value, bool $emphasized = true): self
{
$metric = new Presenter\SummaryMetric(
$label,
$emphasized ? self::strong((string) $value) : self::text((string) $value),
);
return new self([...$this->summary, $metric], $this->blocks, $this->toolbar);
}
/**
* Returns the summary metrics for the host renderer.
*
* @return list<Presenter\SummaryMetric> Validated summary metrics in display order.
*/
public function summaryMetrics(): array
{
return $this->summary;
}
/**
* Appends a table whose columns share a semantic style.
*
* @param array<array-key, mixed> $headers Plain-text column headings in display order.
* @param array<array-key, mixed> $rows Rows of inline or plain values in display order.
* @param bool $collapsible Whether the host may collapse the table.
* @param array<array-key, mixed> $styles Optional {@see ColumnStyle} cases keyed by column index.
* @param bool $filterable Whether to request the host's in-place row filter for the table.
*
* @throws InvalidArgumentException if a header, row width, cell, or column style is invalid.
*
* @return self New view with the table appended.
*/
public function table(
array $headers,
array $rows,
bool $collapsible = false,
array $styles = [],
bool $filterable = false,
): self {
return $this->append(
new Presenter\TableBlock(
$headers,
self::cells($rows),
$styles,
$collapsible,
$filterable,
),
);
}
/**
* Creates plain inline text.
*
* @param string $value Text content; the host escapes it.
*
* @return Presenter\TextInline Inline text accepted by every content method.
*/
public static function text(string $value): Presenter\TextInline
{
return new Presenter\TextInline($value, Presenter\TextStyle::PLAIN);
}
/**
* Adds a toolbar metric independently of the panel summary.
*
* @param string $label Toolbar metric label.
* @param string|int|float $value Metric value converted to text.
*
* @return self New view with the toolbar metric appended.
*/
public function toolbar(string $label, string|int|float $value): self
{
$metric = new Presenter\ToolbarMetric($label, (string) $value);
return new self($this->summary, $this->blocks, [...$this->toolbar, $metric]);
}
/**
* Returns the toolbar metrics for the host renderer.
*
* @return list<Presenter\ToolbarMetric> Validated toolbar metrics in display order.
*/
public function toolbarMetrics(): array
{
return $this->toolbar;
}
/**
* Creates the captured source frames of a call site, which the host renders through its own frame renderer.
*
* Frames travel as captured data, never as markup, so the host keeps ownership of the source-link format.
*
* @param array<array-key, mixed> $frames Captured frames in call order, each an array of frame fields.
*
* @throws InvalidArgumentException if a frame is not an array of fields.
*
* @return Presenter\TraceInline Inline trace accepted by every content method.
*/
public static function trace(array $frames): Presenter\TraceInline
{
return new Presenter\TraceInline($frames);
}
/**
* Creates a captured diagnostic value the host formats and escapes.
*
* @param mixed $value Diagnostic value, preserved without conversion.
* @param bool $typeOnly Whether to show only the value's type instead of its contents.
*
* @return Presenter\ValueInline Inline value accepted by every content method.
*/
public static function value(mixed $value, bool $typeOnly = false): Presenter\ValueInline
{
return new Presenter\ValueInline($value, $typeOnly);
}
/**
* Appends a content block while retaining metrics.
*
* @param Presenter\Block $block Content block placed after the existing blocks.
*
* @return self New view containing the additional block.
*/
private function append(Presenter\Block $block): self
{
return new self($this->summary, [...$this->blocks, $block], $this->toolbar);
}
/**
* Converts table rows to inline cells, rejecting rows that are not lists.
*
* @param array<array-key, mixed> $rows Rows to normalize.
*
* @throws InvalidArgumentException if a row is not a list or a cell is not an accepted inline value.
*
* @return list<list<Presenter\Inline>> Normalized rows in display order.
*/
private static function cells(array $rows): array
{
$result = [];
foreach ($rows as $row) {
if (is_array($row) === false || array_is_list($row) === false) {
throw new InvalidArgumentException(
PanelViewMessage::TABLE_ROW_WIDTH_INVALID->getMessage(),
);
}
$cells = [];
foreach ($row as $value) {
$cells[] = self::inline($value);
}
$result[] = $cells;
}
return $result;
}
/**
* Retains inline values and converts scalars and `null` to text.
*
* @param mixed $value Inline value or plain value to normalize.
*
* @throws InvalidArgumentException if the value is not an accepted inline input.
*
* @return Presenter\Inline Normalized inline value.
*/
private static function inline(mixed $value): Presenter\Inline
{
return match (true) {
$value instanceof Presenter\Inline => $value,
$value === null => self::text('null'),
$value === true => self::text('true'),
$value === false => self::text('false'),
is_string($value) => self::text($value),
is_int($value), is_float($value) => self::text((string) $value),
default => throw self::unsupportedInline($value),
};
}
/**
* Builds a paragraph block from ordered inline inputs.
*
* @param array<array-key, mixed> $content Inline values or plain values in display order.
* @param Tone|null $tone Callout tone, or `null` for an ordinary paragraph.
*
* @throws InvalidArgumentException if an item is not an accepted inline value.
*
* @return Presenter\ParagraphBlock Normalized paragraph block.
*/
private static function paragraphBlock(array $content, Tone|null $tone): Presenter\ParagraphBlock
{
$inline = [];
foreach ($content as $value) {
$inline[] = self::inline($value);
}
return new Presenter\ParagraphBlock($inline, $tone);
}
/**
* Builds one paragraph from a scalar, a single inline value, or a list of inline values.
*
* @param mixed $paragraph Paragraph description.
*
* @throws InvalidArgumentException if the description is neither an inline value nor a list of them.
*
* @return Presenter\ParagraphBlock Normalized paragraph block.
*/
private static function paragraphOf(mixed $paragraph): Presenter\ParagraphBlock
{
if (is_array($paragraph) === false) {
return self::paragraphBlock([$paragraph], null);
}
if (array_is_list($paragraph) === false) {
throw new InvalidArgumentException(
PanelViewMessage::PARAGRAPH_CONTENT_INVALID->getMessage(),
);
}
return self::paragraphBlock($paragraph, null);
}
/**
* Builds the rejection naming the value that cannot be displayed inline.
*
* @param mixed $value Rejected value.
*
* @return InvalidArgumentException Rejection carrying the received type.
*/
private static function unsupportedInline(mixed $value): InvalidArgumentException
{
return new InvalidArgumentException(
PanelViewMessage::INLINE_CONTENT_INVALID->getMessage(get_debug_type($value)),
);
}
}