Passed
Push — master ( e50b6a...8c2165 )
by Timo
22:54
created

Multivalue::cObjGetSingleExt()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.1929

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 36
ccs 16
cts 19
cp 0.8421
rs 8.8333
c 0
b 0
f 0
cc 7
nc 32
nop 4
crap 7.1929
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ContentObject;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
29
30
/**
31
 * A content object (cObj) to turn comma separated strings into an array to be
32
 * used in a multi value field in a Solr document.
33
 *
34
 * Example usage:
35
 *
36
 * keywords = SOLR_MULTIVALUE # supports stdWrap
37
 * keywords {
38
 *   field = tags # a comma separated field. instead of field you can also use "value"
39
 *   separator = , # comma is the default value
40
 *   removeEmptyValues = 1 # a flag to remove empty strings from the list, on by default.
41
 *   removeDuplicateValues = 1 # a flag to remove duplicate strings from the list, off by default.
42
 * }
43
 *
44
 * @author Ingo Renner <[email protected]>
45
 */
46
class Multivalue extends AbstractContentObject
47
{
48
    const CONTENT_OBJECT_NAME = 'SOLR_MULTIVALUE';
49
50
    /**
51
     * Executes the SOLR_MULTIVALUE content object.
52
     *
53
     * Turns a list of values into an array that can then be used to fill
54
     * multivalued fields in a Solr document. The array is returned in
55
     * serialized form as content objects are expected to return strings.
56
     *
57
     * @inheritDoc
58
     */
59 3
    public function render($conf = [])
60
    {
61 3
        $data = '';
62 3
        if (isset($conf['value'])) {
63 1
            $data = $conf['value'];
64 1
            unset($conf['value']);
65
        }
66
67 3
        if (!empty($conf)) {
68 3
            $data = $this->cObj->stdWrap($data, $conf);
69
        }
70
71 3
        if (!array_key_exists('separator', $conf)) {
72
            $conf['separator'] = ',';
73
        }
74
75 3
        $removeEmptyValues = true;
76 3
        if (isset($conf['removeEmptyValues']) && $conf['removeEmptyValues'] == 0) {
77
            $removeEmptyValues = false;
78
        }
79
80 3
        $listAsArray = GeneralUtility::trimExplode(
81 3
            $conf['separator'],
82 3
            $data,
83 3
            $removeEmptyValues
84
        );
85
86 3
        if (!empty($conf['removeDuplicateValues'])) {
87
            $listAsArray = array_unique($listAsArray);
88
        }
89
90 3
        return serialize($listAsArray);
91
    }
92
}
93