Passed
Pull Request — master (#116)
by Arnaud
03:38
created

FieldRenderer::renderHeader()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 2
dl 0
loc 25
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Field\Render;
4
5
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
6
use LAG\AdminBundle\Field\RendererAwareFieldInterface;
7
use LAG\AdminBundle\Field\EntityAwareFieldInterface;
8
use LAG\AdminBundle\Field\FieldInterface;
9
use LAG\AdminBundle\Utils\StringUtils;
10
use LAG\AdminBundle\View\ViewInterface;
11
use Symfony\Component\PropertyAccess\PropertyAccess;
12
use Symfony\Contracts\Translation\TranslatorInterface;
13
14
class FieldRenderer implements FieldRendererInterface
15
{
16
    /**
17
     * @var ApplicationConfigurationStorage
18
     */
19
    private $storage;
20
21
    /**
22
     * @var TranslatorInterface
23
     */
24
    private $translator;
25
26
    public function __construct(ApplicationConfigurationStorage $storage, TranslatorInterface $translator)
27
    {
28
        $this->storage = $storage;
29
        $this->translator = $translator;
30
    }
31
32
    public function render(FieldInterface $field, $entity): string
33
    {
34
        $value = null;
35
        $accessor = PropertyAccess::createPropertyAccessor();
36
37
        if ('_' !== substr($field->getName(), 0, 1)) {
38
            // The name starts with a underscore, it is not a custom field and it should be mapped to the entity
39
            $value = $accessor->getValue($entity, $field->getName());
40
        }
41
42
        if ($field instanceof EntityAwareFieldInterface) {
43
            // The field required the entity to be rendered
44
            $field->setEntity($entity);
45
        }
46
47
        if ($field instanceof RendererAwareFieldInterface) {
48
            // Some fields types (collections...) can require children render
49
            $field->setRenderer($this);
50
        }
51
        $render = $field->render($value);
52
53
        return $render;
54
    }
55
56
    public function renderHeader(ViewInterface $admin, FieldInterface $field): string
57
    {
58
        if (StringUtils::startWith($field->getName(), '_')) {
59
            return '';
60
        }
61
        $configuration = $this->storage->getConfiguration();
62
63
        if ($configuration->get('translation')) {
64
            $key = StringUtils::getTranslationKey(
65
                $configuration->get('translation_pattern'),
66
                $admin->getName(),
67
                $field->getName()
68
            );
69
            $title = $this->translator->trans($key);
70
        } else {
71
            $title = StringUtils::camelize($field->getName());
72
            $title = preg_replace('/(?<!\ )[A-Z]/', ' $0', $title);
73
            $title = trim($title);
74
75
            if ('Id' === $title) {
76
                $title = '#';
77
            }
78
        }
79
80
        return $title;
81
    }
82
}
83