Completed
Pull Request — master (#36)
by Daniel
02:33
created

ObjectView::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType\Standard\View;
6
7
use Metadata\MetadataFactory;
8
use Metadata\NullMetadata;
9
use Psi\Component\ContentType\View\View;
10
use Psi\Component\ContentType\View\ViewBuilder;
11
use Psi\Component\ContentType\View\ViewInterface;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
class ObjectView implements ViewInterface
15
{
16
    private $metadataFactory;
17
    private $fieldLoader;
18
19
    public function __construct(
20
        MetadataFactory $metadataFactory,
21
        FieldLoader $fieldLoader
22
    ) {
23
        $this->metadataFactory = $metadataFactory;
24
        $this->fieldLoader = $fieldLoader;
25
    }
26
27
    public function buildView(ViewBuilder $builder, $data, array $options)
28
    {
29
        $classFqn = get_class($data);
30
        $metadata = $this->metadataFactory->getMetadataForClass($classFqn);
31
32
        if ($metadata instanceof NullMetadata) {
33
            throw new \RuntimeException(sprintf(
34
                'Class "%s" is not mapped',
35
                $classFqn
36
            ));
37
        }
38
39
        foreach ($metadata->getPropertyMetadata() as $propertyMetadata) {
40
            $field = $this->fieldLoader->load(
41
                $propertyMetadata->getType(),
42
                $propertyMetadata->getOptions()
43
            );
44
45
            $builder->add(
46
                $propertyMetadata->getName(), 
47
                $field->getInnerType()->getViewType(),
48
                $propertyMetadata->getValue($data),
49
                $field->getViewOptions()
50
            );
51
        }
52
    }
53
54
    public function configureOptions(OptionsResolver $options)
55
    {
56
        $options->setDefault('template', 'psi/object');
57
    }
58
}
59