Mapper::getMappers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.9666
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * Mapper for variables types to TCA and DB information.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader;
9
10
use HDNET\Autoloader\Utility\ExtendedUtility;
11
12
/**
13
 * Mapper for variables types to TCA and DB information.
14
 */
15
class Mapper implements SingletonInterface
16
{
17
    /**
18
     * Custom mapper.
19
     *
20
     * @var array
21
     */
22
    protected $customMapper = [];
23
24
    /**
25
     * Internal mapper.
26
     *
27
     * @var array
28
     */
29
    protected $internalMapper = [
30
        'Boolean',
31
        'Float',
32
        'DateTime',
33
        'FileReference',
34
        'FileReferenceObjectStorage',
35
        'ObjectStorage',
36
        'Int',
37
        'String',
38
        'Model',
39
        'StaticInfoTables',
40
        'Array',
41
    ];
42
43
    /**
44
     * Get the TCA configuration for the current type.
45
     *
46
     * @param string $type
47
     * @param string $fieldName
48
     * @param bool   $overWriteLabel
49
     *
50
     * @return array
51
     */
52
    public function getTcaConfiguration($type, $fieldName, $overWriteLabel = false)
53
    {
54
        try {
55
            $mapper = $this->getMapperByType($type);
56
        } catch (\Exception $exception) {
57
            // always return a valid mapper
58
            $mapper = $this->getMapperByType('String');
59
        }
60
61
        return $mapper->getTcaConfiguration($fieldName, $overWriteLabel);
62
    }
63
64
    /**
65
     * Get the database definition for the current mapper.
66
     *
67
     * @param string $type
68
     *
69
     * @return string
70
     */
71
    public function getDatabaseDefinition($type)
72
    {
73
        $mapper = $this->getMapperByType($type);
74
75
        return $mapper->getDatabaseDefinition();
76
    }
77
78
    /**
79
     * Add a custom mapper.
80
     *
81
     * @param string $className
82
     */
83
    public function addCustomMapper($className): void
84
    {
85
        $this->customMapper[] = $className;
86
    }
87
88
    /**
89
     * Get a valid mapper for the given type.
90
     *
91
     * @param string $type
92
     *
93
     * @throws \Exception
94
     *
95
     * @return MapperInterface
96
     */
97
    protected function getMapperByType($type)
98
    {
99
        $mappers = $this->getMappers();
100
        foreach ($mappers as $mapper) {
101
            /** @var MapperInterface $mapper */
102
            if ($mapper->canHandleType($type)) {
103
                return $mapper;
104
            }
105
        }
106
107
        throw new Exception('No valid mapper for the given type found: ' . $type, 123712631);
108
    }
109
110
    /**
111
     * Get all mappers.
112
     *
113
     * @return array
114
     */
115
    protected function getMappers()
116
    {
117
        $mapper = array_merge($this->customMapper, $this->getInternalMapperClasses());
118
        foreach ($mapper as $key => $className) {
119
            $mapper[$key] = ExtendedUtility::create($className);
120
        }
121
122
        return $mapper;
123
    }
124
125
    /**
126
     * Get internal mapper class names.
127
     *
128
     * @return array
129
     */
130
    protected function getInternalMapperClasses()
131
    {
132
        $mapper = [];
133
        foreach ($this->internalMapper as $key => $value) {
134
            $mapper[$key] = 'HDNET\\Autoloader\\Mapper\\' . $value . 'Mapper';
135
        }
136
137
        return $mapper;
138
    }
139
}
140