Signature.php
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Lackoxygen\MinPayment\Utils;
class Signature
{
/**
* @var array $item
*/
protected $item = [];
protected function __construct(array $item)
{
$this->item = $item;
}
public static function make(array $items, string $secret): string
{
$signature = new self($items);
return $signature->toString($secret);
}
protected function sortByASCII()
{
$item = &$this->item;
ksort($item);
}
/**
* @param array $item
* @param callable $filter
*
* @return string
*/
protected function map(array $item, callable $filter): string
{
$array = [];
foreach ($item as $key => $value) {
if ($filter($key, $value) !== false) {
$array[] = $key . '=' . $value;
}
}
return join($array, '&');
}
/**
* @param string $string
*
* @return string
*/
public static function encode(string $string): string
{
return md5($string);
}
/**
* @param string $secret
*
* @return string
*/
protected function toString(string $secret): string
{
$this->sortByASCII();
$string = $this->map($this->item, function ($k, $v) {
return !($v === '' || $v === null);
}) . '&key=' . $secret;
return self::encode($string);
}
}