RelationResolver::getColumnValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
namespace Codappix\SearchCore\Domain\Index\TcaIndexer;
3
4
/*
5
 * Copyright (C) 2016  Daniel Siepmann <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
 * 02110-1301, USA.
21
 */
22
23
use Codappix\SearchCore\Utility\FrontendUtility;
24
use TYPO3\CMS\Backend\Utility\BackendUtility;
25
use TYPO3\CMS\Core\SingletonInterface as Singleton;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
/**
29
 * Resolves relations from TCA using TCA.
30
 *
31
 * E.g. resolves mm relations, items for selects, group db, etc.
32
 * Will replace the column with an array of resolved labels.
33
 */
34
class RelationResolver implements Singleton
35
{
36 32
    public function resolveRelationsForRecord(TcaTableServiceInterface $service, array $record) : array
37
    {
38 32
        foreach (array_keys($record) as $column) {
39 32
            if (in_array($column, ['pid', $service->getLanguageUidColumn()])) {
40 32
                $record[$column] = (int) $record[$column];
41 32
                continue;
42
            }
43
44 32
            $record[$column] = $this->getColumnValue($record, $column, $service);
45
46
            try {
47 32
                $config = $service->getColumnConfig($column);
48
49 32
                if ($this->isRelation($config)) {
50 32
                    $record[$column] = $this->resolveValue($record[$column], $config);
51
                }
52 32
            } catch (InvalidArgumentException $e) {
53
                // Column is not configured.
54 32
                continue;
55
            }
56
        }
57
58 32
        return $record;
59
    }
60
61 32
    protected function resolveValue($value, array $tcaColumn)
62
    {
63 32
        if ($value === '' || $value === 'N/A') {
64 32
            return [];
65
        }
66
67 16
        if ($tcaColumn['type'] === 'select' && strpos($value, ';') !== false) {
68 4
            return $this->resolveForeignDbValue($value);
69
        }
70 14
        if (in_array($tcaColumn['type'], ['inline', 'group', 'select'])) {
71 14
            return $this->resolveInlineValue($value);
72
        }
73
74
        return [];
75
    }
76
77 32
    protected function isRelation(array &$config) : bool
78
    {
79 32
        return isset($config['foreign_table'])
80 32
            || (isset($config['renderType']) && !in_array($config['renderType'], ['selectSingle', 'inputDateTime']))
81 32
            || (isset($config['internal_type']) && strtolower($config['internal_type']) === 'db')
82
            ;
83
    }
84
85 4
    protected function resolveForeignDbValue(string $value) : array
86
    {
87 4
        return array_map('trim', explode(';', $value));
88
    }
89
90 14
    protected function resolveInlineValue(string $value) : array
91
    {
92 14
        return array_map('trim', explode(',', $value));
93
    }
94
95 32
    protected function getUtilityForMode() : string
96
    {
97 32
        if (TYPO3_MODE === 'BE') {
0 ignored issues
show
introduced by
The condition Codappix\SearchCore\Doma...xer\TYPO3_MODE === 'BE' is always true.
Loading history...
98 32
            return BackendUtility::class;
99
        }
100
101
        return FrontendUtility::class;
102
    }
103
104 32
    protected function getColumnValue(array $record, string $column, TcaTableServiceInterface $service): string
105
    {
106 32
        $utility = GeneralUtility::makeInstance($this->getUtilityForMode());
107 32
        return $utility::getProcessedValueExtra(
108 32
            $service->getTableName(),
109 32
            $column,
110 32
            $record[$column],
111 32
            0,
112 32
            $record['uid']
113 32
        ) ?? '';
114
    }
115
}
116