1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Request Provider Class |
6
|
|
|
* @category Ticaje |
7
|
|
|
* @author Max Demian <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ticaje\AeSdk\Infrastructure\Provider\Request\Builder; |
11
|
|
|
|
12
|
|
|
use Ticaje\AeSdk\Infrastructure\Interfaces\Provider\Request\RequestDtoInterface as DtoInterface; |
13
|
|
|
use Ticaje\AeSdk\Infrastructure\Interfaces\Provider\Request\Builder\PublicBuilderInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class General |
17
|
|
|
* @package Ticaje\AeSdk\Infrastructure\Provider\Request\Builder |
18
|
|
|
*/ |
19
|
|
|
class General implements PublicBuilderInterface |
20
|
|
|
{ |
21
|
|
|
private $timeStamp; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->timeStamp = date("Y-m-d H:i:s"); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
|
|
public function compile(DtoInterface $dto): string |
32
|
|
|
{ |
33
|
|
|
$result = ''; |
34
|
|
|
$params = $this->compileAsArray($dto); |
35
|
|
|
array_walk($params, function ($value, $key) use (&$result) { |
36
|
|
|
$result .= "{$key}=$value&"; |
37
|
|
|
}); |
38
|
|
|
return $result; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
public function compileToSignature(DtoInterface $dto): string |
45
|
|
|
{ |
46
|
|
|
$publicAsArray = $this->compileAsArray($dto); |
47
|
|
|
return $this->build($publicAsArray, $dto->getRequest()); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param DtoInterface $dto |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
private function compileAsArray(DtoInterface $dto): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'method' => $dto->getApiMethodName(), |
|
|
|
|
58
|
|
|
'app_key' => $dto->getAppKey(), |
|
|
|
|
59
|
|
|
'session' => $dto->getAccessToken(), |
|
|
|
|
60
|
|
|
'timestamp' => $this->timeStamp, |
61
|
|
|
'format' => $dto->getResponseFormat(), |
|
|
|
|
62
|
|
|
'v' => $dto->getApiVersion(), |
|
|
|
|
63
|
|
|
'sign_method' => $dto->getSignMethod(), |
|
|
|
|
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $public |
69
|
|
|
* @param array $request |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function build(array $public, array $request): string |
73
|
|
|
{ |
74
|
|
|
$params = array_merge($request, $public); |
75
|
|
|
ksort($params); |
76
|
|
|
$result = ''; |
77
|
|
|
array_walk($params, function ($value, $key) use (&$result) { |
78
|
|
|
$result .= $key . $value; |
79
|
|
|
}); |
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|