Completed
Push — master ( 1c0463...6ee2a7 )
by Rafael
34:09
created

AbstractFieldList   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 98
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 5 1
A build() 0 6 2
A toString() 0 16 3
A initializeFromString() 0 19 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[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
29
/**
30
 * The AbstractFieldList class
31
 */
32
abstract class AbstractFieldList implements ParameterBuilder
33
{
34
    /**
35
     * @var array
36
     */
37
    protected $fieldList = [];
38
39
    /**
40
     * Parameter key which should be used for Apache Solr URL query
41
     *
42
     * @var string
43
     */
44
    protected $parameterKey = '';
45
46
    /**
47
     * FieldList parameter builder constructor.
48
     *
49
     * private constructor should only be created with the from* methods
50
     *
51
     * @param array $fieldList
52
     */
53
    private function __construct(array $fieldList)
54
    {
55
        $this->fieldList = $fieldList;
56
    }
57
58
    /**
59
     * @param string $fieldName
60
     * @param float $boost
61
     *
62
     * @return ParameterBuilder
63
     */
64
    public function add(string $fieldName, float $boost = 1.0): ParameterBuilder
65
    {
66
        $this->fieldList[$fieldName] = (float)$boost;
67
        return $this;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function build() : array
74
    {
75
        $string = $this->toString();
76
        // return empty array on empty string
77
        return trim($string) === '' ? [] : [$this->parameterKey => $string];
78
    }
79
80
    /**
81
     * Creates the string representation
82
     *
83
     * @param string $delimiter
84
     * @return string
85
     */
86
    public function toString(string $delimiter = ' ')
87
    {
88
        $fieldListString = '';
89
90
        foreach ($this->fieldList as $fieldName => $fieldBoost) {
91
            $fieldListString .= $fieldName;
92
93
            if ($fieldBoost != 1.0) {
94
                $fieldListString .= '^' . number_format($fieldBoost, 1, '.', '');
95
            }
96
97
            $fieldListString .= $delimiter;
98
        }
99
100
        return rtrim($fieldListString, $delimiter);
101
    }
102
103
    /**
104
     * Parses the string representation of the fieldList (e.g. content^100, title^10) to the object representation.
105
     *
106
     * @param string $fieldListString
107
     * @param string $delimiter
108
     * @return AbstractFieldList
109
     */
110
    protected static function initializeFromString(string $fieldListString, string $delimiter = ',') : AbstractFieldList
111
    {
112
        $fields = GeneralUtility::trimExplode($delimiter, $fieldListString, true);
113
        $fieldList = [];
114
115
        foreach ($fields as $field) {
116
            $fieldNameAndBoost = explode('^', $field);
117
118
            $boost = 1.0;
119
            if (isset($fieldNameAndBoost[1])) {
120
                $boost = floatval($fieldNameAndBoost[1]);
121
            }
122
123
            $fieldName = $fieldNameAndBoost[0];
124
            $fieldList[$fieldName] = $boost;
125
        }
126
127
        return new static($fieldList);
128
    }
129
}
130