Passed
Pull Request — master (#2755)
by Rafael
03:55
created

RoutingUtility::deflateString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Utility;
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
/**
17
 * This utility class contains several functions used inside of the middleware and enhancer for routing purposes
18
 *
19
 * @author Lars Tode <[email protected]>
20
 */
21
class RoutingUtility
22
{
23
    /**
24
     * Builds the hash of an inflated parameter
25
     * This method based on the VariableProcessor since the logic is not public
26
     *
27
     * @see \TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor::addHash
28
     *
29
     * @param string $value Deflated argument path
30
     * @return string
31
     */
32
    public static function buildHash(string $value): string
33
    {
34
        if (strlen($value) < 31 && !preg_match('#[^\w]#', $value)) {
35
            return $value;
36
        }
37
        // removing one bit, e.g. for enforced route prefix `{!value}`
38
        $hash = substr(md5($value), 0, -1);
39
        // Symfony Route Compiler requires first literal to be non-integer
40
        if ($hash[0] === (string)(int)$hash[0]) {
41
            $hash[0] = str_replace(
42
                range('0', '9'),
43
                range('o', 'x'),
44
                $hash[0]
45
            );
46
        }
47
48
        return $hash;
49
    }
50
51
    /**
52
     * Deflate a given string with a given namespace
53
     * This method based on the VariableProcessor since the logic is not public
54
     *
55
     * @see \TYPO3\CMS\Core\Routing\Enhancer\VariableProcessor
56
     *
57
     * @param string $parameterName
58
     * @param string $namespace
59
     * @return string
60
     */
61
    public static function deflateString(string $parameterName, string $namespace = 'tx_solr'): string
62
    {
63
        if (!empty($namespace)) {
64
            $parameterName = $namespace . '/' . $parameterName;
65
        }
66
        return str_replace(
67
            '/',
68
            '__',
69
            $parameterName
70
        );
71
    }
72
}