Completed
Pull Request — master (#1319)
by Timo
31:48
created

NumericRangeUrlDecoder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A decode() 0 12 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased\NumericRange;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2011 Markus Goldbach <[email protected]>
8
 *  (c) 2012-2015 Ingo Renner <[email protected]>
9
 *  (c) 2016 Markus Friedrich <[email protected]>
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 2 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\FacetUrlDecoderInterface;
30
31
/**
32
 * Parser to build Solr range queries from tx_solr[filter]
33
 *
34
 * @author Markus Goldbach <[email protected]>
35
 * @author Ingo Renner <[email protected]>
36
 * @author Markus Friedrich <[email protected]>
37
 */
38
class NumericRangeUrlDecoder implements FacetUrlDecoderInterface
39
{
40
41
    /**
42
     * Delimiter for ranges in the URL.
43
     *
44
     * @var string
45
     */
46
    const DELIMITER = '-';
47
48
    /**
49
     * Parses the given range from a GET parameter and returns a Solr range
50
     * filter.
51
     *
52
     * @param string $range The range filter from the URL.
53
     * @param array $configuration Facet configuration
54
     * @return string Lucene query language filter to be used for querying Solr
55
     * @throws \InvalidArgumentException
56
     */
57
    public function decode($range, array $configuration = [])
58
    {
59
        preg_match('/(-?\d*?)' . self::DELIMITER . '(-?\d*)/', $range, $filterParts);
60
        if ($filterParts[1] == '' || $filterParts[2] == '') {
61
            throw new \InvalidArgumentException(
62
                'Invalid numeric range given',
63
                1466062730
64
            );
65
        }
66
67
        return '[' . (int)$filterParts[1] . ' TO ' . (int)$filterParts[2] . ']';
68
    }
69
}
70