Completed
Push — master ( c00acb...97e960 )
by Timo
06:00
created

QueryParametersContainer::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 Timo Hund
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
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
/**
29
 * The QueryParameterContainer is responsible to hold all parameters that are needed to build a solr query.
30
 *
31
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query
32
 */
33
class QueryParametersContainer {
34
35
36
    /**
37
     * @var array
38
     */
39
    protected $queryParameters = [];
40
41
    /**
42
     * This method can be used to set a query parameter when the value is a string and not empty or unset it
43
     * in any other case. Extracted to avoid duplicate code.
44
     *
45
     * @param string $parameterName
46
     * @param mixed $value
47
     */
48
    public function setWhenStringOrUnsetWhenEmpty($parameterName, $value)
49
    {
50
        if (is_string($value) && !empty($value)) {
51
            $this->set($parameterName, $value);
52
        } else {
53
            unset($this->queryParameters[$parameterName]);
54
        }
55
    }
56
57
    /**
58
     * This method can be used to set a query parameter when the value is a int and not empty or unset it
59
     * in any other case. Extracted to avoid duplicate code.
60
     *
61
     * @param string $parameterName
62
     * @param int $value
63
     */
64
    public function setWhenIntOrUnsetWhenNull(string $parameterName, int $value = null)
65
    {
66
        if (null === $value) {
67
            unset($this->queryParameters[$parameterName]);
68
            return;
69
        }
70
        $this->set($parameterName, $value);
71
    }
72
73
    /**
74
     * Adds any generic query parameter.
75
     *
76
     * @param string $parameterName Query parameter name
77
     * @param mixed $parameterValue Parameter value
78
     */
79
    public function set($parameterName, $parameterValue)
80
    {
81
        $this->queryParameters[$parameterName] = $parameterValue;
82
    }
83
84
    /**
85
     * Removes a queryParameter.
86
     *
87
     * @param mixed $parameterName
88
     */
89
    public function remove($parameterName)
90
    {
91
        unset($this->queryParameters[$parameterName]);
92
    }
93
94
    /**
95
     * Removes multiple query parameters by name
96
     *
97
     * @param array $parameterNames
98
     */
99
    public function removeMany(array $parameterNames)
100
    {
101
        foreach ($parameterNames as $parameterName) {
102
            $this->remove($parameterName);
103
        }
104
    }
105
106
    /**
107
     * @param string $prefix
108
     */
109
    public function removeByPrefix($prefix)
110
    {
111
        foreach ($this->queryParameters as $parameterName => $parameterValue) {
112
            if (GeneralUtility::isFirstPartOfStr($parameterName, $prefix)) {
113
                unset($this->queryParameters[$parameterName]);
114
            }
115
        }
116
    }
117
118
    /**
119
     * Returns a queryParameter
120
     * @param string $parameterName
121
     * @return mixed
122
     */
123
    public function get($parameterName)
124
    {
125
        return $this->queryParameters[$parameterName];
126
    }
127
128
    /**
129
     * @param array $toMerge
130
     */
131
    public function merge(array $toMerge)
132
    {
133
        $this->queryParameters = array_merge($this->queryParameters, $toMerge);
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function toArray()
140
    {
141
        return $this->queryParameters;
142
    }
143
}