1: <?php
2:
3: namespace IPay\Builders;
4:
5: use IPay\Encryption\Encryptor;
6: use Nette\Utils\Json;
7:
8: /**
9: * @psalm-type ValueType = string|int
10: * @psalm-type ParametersType = array<string, ValueType>
11: */
12: final class RequestBodyBuilder implements \Stringable, \JsonSerializable
13: {
14: /**
15: * @param ParametersType $parameters
16: */
17: private function __construct(
18: private array $parameters = [],
19: ) {
20: }
21:
22: public static function new(): self
23: {
24: return new self();
25: }
26:
27: /**
28: * @param ParametersType $parameters
29: */
30: public function enhance(array $parameters): self
31: {
32: return new self($parameters);
33: }
34:
35: /**
36: * @param ParametersType $parameters
37: */
38: public function build(array $parameters = []): self
39: {
40: $data = array_merge($this->parameters, $parameters);
41: ksort($data);
42: $data['signature'] = md5(http_build_query($data));
43:
44: return new self($data);
45: }
46:
47: public function encrypt(): string
48: {
49: return new self(['encrypted' => Encryptor::encrypt($this)]);
50: }
51:
52: public function __toString(): string
53: {
54: return Json::encode($this->parameters);
55: }
56:
57: #[\ReturnTypeWillChange]
58: public function jsonSerialize()
59: {
60: return $this->parameters;
61: }
62: }
63: