Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
27 / 28
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FieldDisplayComponentFactory
96.43% covered (success)
96.43%
27 / 28
75.00% covered (warning)
75.00%
3 / 4
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 doCreateDisplayFor
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 createDisplayFor
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\HtmlBuilders\Factories;
3
4use Apie\Core\Context\ApieContext;
5use Apie\HtmlBuilders\Columns\ColumnSelector;
6use Apie\HtmlBuilders\FieldDisplayBuildContext;
7use Apie\HtmlBuilders\FieldDisplayProviders\ArrayDisplayProvider;
8use Apie\HtmlBuilders\FieldDisplayProviders\BooleanDisplayProvider;
9use Apie\HtmlBuilders\FieldDisplayProviders\EnumDisplayProvider;
10use Apie\HtmlBuilders\FieldDisplayProviders\FallbackDisplayProvider;
11use Apie\HtmlBuilders\FieldDisplayProviders\ListDisplayProvider;
12use Apie\HtmlBuilders\FieldDisplayProviders\NullDisplayProvider;
13use Apie\HtmlBuilders\FieldDisplayProviders\SafeHtmlDisplayProvider;
14use Apie\HtmlBuilders\FieldDisplayProviders\SegmentDisplayProvider;
15use Apie\HtmlBuilders\FieldDisplayProviders\UploadedFileDisplayProvider;
16use Apie\HtmlBuilders\FieldDisplayProviders\ValueObjectDisplayProvider;
17use Apie\HtmlBuilders\Interfaces\ComponentInterface;
18use Apie\HtmlBuilders\Interfaces\FieldDisplayComponentProviderInterface;
19
20final class FieldDisplayComponentFactory
21{
22    /**
23     * @var array<int, FieldDisplayComponentProviderInterface>
24     */
25    private array $valueComponentProviders;
26
27    public function __construct(FieldDisplayComponentProviderInterface ...$valueComponentProviders)
28    {
29        $this->valueComponentProviders = $valueComponentProviders;
30    }
31
32    /**
33     * @param iterable<int, FieldDisplayComponentProviderInterface> $customDisplayProviders
34     */
35    public static function create(
36        iterable $customDisplayProviders,
37        ?ColumnSelector $columnSelector = null
38    ): self {
39        return new self(
40            ...[
41                ...$customDisplayProviders,
42                new SafeHtmlDisplayProvider(),
43                new UploadedFileDisplayProvider(),
44                new ArrayDisplayProvider(),
45                new ListDisplayProvider($columnSelector ?? new ColumnSelector()),
46                new ValueObjectDisplayProvider(),
47                new EnumDisplayProvider(),
48                new BooleanDisplayProvider(),
49                new NullDisplayProvider(),
50                new SegmentDisplayProvider(),
51                new FallbackDisplayProvider(),
52            ]
53        );
54    }
55
56    private function doCreateDisplayFor(mixed $object, FieldDisplayBuildContext $fieldDisplayBuildContext): ComponentInterface
57    {
58        foreach ($this->valueComponentProviders as $valueComponentProvider) {
59            if ($valueComponentProvider->supports($object, $fieldDisplayBuildContext)) {
60                return $valueComponentProvider->createComponentFor($object, $fieldDisplayBuildContext);
61            }
62        }
63        return (new FallbackDisplayProvider)->createComponentFor($object, $fieldDisplayBuildContext);
64    }
65
66    public function createDisplayFor(mixed $object, ApieContext $apieContext): ComponentInterface
67    {
68        $context = new FieldDisplayBuildContext(
69            function (mixed $object, FieldDisplayBuildContext $fieldDisplayBuildContext) {
70                return $this->doCreateDisplayFor($object, $fieldDisplayBuildContext);
71            },
72            $apieContext,
73            $object
74        );
75        
76        return $this->doCreateDisplayFor($object, $context);
77    }
78}