Completed
Push — master ( c75a1a...5e3b3c )
by François
02:10
created

Hash::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer;
13
14
class Hash
15
{
16
17
    public static $identityHeaders = array(
18
        'User-Agent',
19
        'Accept',
20
        'Accept-Charset',
21
        'Accept-Language',
22
        'Accept-Encoding',
23
        'From',
24
        'Dnt',
25
    );
26
27
    public static function headers($array)
28
    {
29
        $array = self::filterArrayKeys($array, self::$identityHeaders);
30
        ksort($array);
31
        $string = '';
32
        foreach ($array as $key => $value) {
33
            $key = self::normalizeKey($key);
34
            $string .= "$key:$value;";
35
        }
36
        return self::hash($string);
37
    }
38
39
    public static function filterArrayKeys($array = array(), $keys = array())
40
    {
41
        $ikeys = array_map('strtolower', $keys);
42
        foreach ($array as $key => $value) {
43
            $ikey = strtolower($key);
44
            if (!in_array($ikey, $ikeys)) {
45
                unset($array[$key]);
46
            }
47
        }
48
        return $array;
49
    }
50
51
    public static function normalizeKey($key)
52
    {
53
        if (strpos($key, '-')) {
54
            $parts = explode('-', $key);
55
            $parts = array_map('strtolower', $parts);
56
            $parts = array_map('ucfirst', $parts);
57
            $key = implode('-', $parts);
58
        } else {
59
            $key = ucfirst(strtolower($key));
60
        }
61
        return $key;
62
    }
63
64
    public static function hash($value)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
65
    {
66
        return md5($value);
67
    }
68
69
}
70