Completed
Push — master ( 7d5f81...069e15 )
by Jan
02:05
created

SignatureDataFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatCsobGateway\Crypto;
4
5
class SignatureDataFormatter
6
{
7
8
	/** @var mixed[] */
9
	private $keysPriority;
10
11
	/**
12
	 * @param mixed[] $keysPriority
13
	 */
14 43
	public function __construct(array $keysPriority)
15
	{
16 43
		$this->keysPriority = $keysPriority;
17 43
	}
18
19
	/**
20
	 * @param mixed[] $data
21
	 * @return string
22
	 */
23 12
	public function formatDataForSignature(array $data): string
24
	{
25 12
		return implode('|', $this->generateMessage($data, $this->keysPriority));
26
	}
27
28
	/**
29
	 * @param mixed[] $data
30
	 * @param mixed[] $keys
31
	 * @return mixed[]
32
	 */
33 12
	private function generateMessage(array $data, array $keys): array
34
	{
35 12
		$message = [];
36
37 12
		foreach ($keys as $key => $values) {
38 8
			if (is_int($key)) {
39 3
				foreach ($data as $items) {
40 3
					$message = array_merge($message, $this->generateMessage($items, $values));
41
				}
42 3
				continue;
43
			}
44 8
			if (!array_key_exists($key, $data)) {
45 4
				continue;
46
			}
47
48 8
			if (is_array($values)) {
49 4
				$message = array_merge($message, $this->generateMessage($data[$key], $values));
50
			} else {
51 8
				if (is_bool($data[$key])) {
52
					$message[] = $data[$key] ? 'true' : 'false';
53
				} else {
54 8
					$message[] = $data[$key];
55
				}
56
			}
57
		}
58
59 12
		return $message;
60
	}
61
62
}
63