Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.37% covered (success)
97.37%
37 / 38
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Helper
97.37% covered (success)
97.37%
37 / 38
75.00% covered (warning)
75.00%
3 / 4
8
0.00% covered (danger)
0.00%
0 / 1
 proxySanitizeUri
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFormatedDates
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 hashQueryParameters
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getContentForHash
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2
3/**
4 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
5 **/
6
7namespace BO\Slim;
8
9class Helper
10{
11    /**
12     * @param null|string|string[] $uri
13     *
14     *
15     * @return string|string[]
16     *
17     */
18    public static function proxySanitizeUri(array|string|null $uri): array|string
19    {
20        $uri = str_replace(':80/', '/', $uri);
21        return $uri;
22    }
23
24    /**
25     * @param \DateTimeImmutable|int $timestamp
26     *
27     * @return false|string
28     */
29    public static function getFormatedDates(
30        int|\DateTimeImmutable $timestamp,
31        string $pattern = 'MMMM',
32        $locale = 'de_DE',
33        $timezone = 'Europe/Berlin'
34    ): string|false {
35        $dateFormatter = new \IntlDateFormatter(
36            $locale,
37            \IntlDateFormatter::MEDIUM,
38            \IntlDateFormatter::MEDIUM,
39            $timezone,
40            \IntlDateFormatter::GREGORIAN,
41            $pattern
42        );
43        return $dateFormatter->format($timestamp);
44    }
45
46
47    public static function hashQueryParameters(
48        string $section,
49        array $queryVariables,
50        array $parameters,
51        string $hashFunction = 'md5'
52    ): string {
53        $content = $section . self::getContentForHash($queryVariables, $parameters);
54        $hashString = $hashFunction($content . \App::$urlSignatureSecret);
55        $firstHalf  = substr($hashString, 0, floor(strlen($hashString) / 2));
56        $secondHalf = substr($hashString, strlen($firstHalf));
57        $alphabet   = '0123456789' . implode(range('A', 'Z')) . implode(range('a', 'z'));
58        $rotation   = 31;
59        // reducing the hash to half its length by combining first half and second half
60        for ($i = 0; $i < strlen($firstHalf); $i++) {
61            $rotation = (strpos($alphabet, $firstHalf[$i]) + ord($secondHalf[$i]) + $rotation) % strlen($alphabet);
62            $firstHalf[$i] = $alphabet[$rotation];
63        }
64        return $firstHalf;
65    }
66
67    protected static function getContentForHash(array $queryVariables, array $parameters): string
68    {
69        $content = '';
70        foreach ($parameters as $parameter) {
71            if (isset($queryVariables[$parameter])) {
72                if (is_iterable($queryVariables[$parameter])) {
73                    $parameterArray = $queryVariables[$parameter];
74                    ksort($parameterArray);
75                    $flat = [];
76                    array_walk_recursive(
77                        $parameterArray,
78                        function ($value) use (&$flat) {
79                            $flat[] = strval($value);
80                        }
81                    );
82                    $content .= implode('', $flat);
83                } else {
84                    $content .= (string) $queryVariables[$parameter];
85                }
86            } else {
87                $content .= 'NULL';
88            }
89        }
90        return $content;
91    }
92}