-
Notifications
You must be signed in to change notification settings - Fork 0
/
Puzzle.php
81 lines (72 loc) · 3.06 KB
/
Puzzle.php
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
76
77
78
79
80
81
<?php
/**
* Created by Denis Zhadan on 14.03.2024 17:12.
*/
class Puzzle
{
protected string $url = 'https://cv.microservices.credy.com/v1';
protected static function getSignature(int $timestamp): string
{
return sha1(strval($timestamp) . 'credy');
}
protected function arrayToJSONx(array $data, SimpleXMLElement $xml = null, bool $with_name = true): SimpleXMLElement
{
if ($xml === null) {
$xml = new SimpleXMLElement('<json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"/>');
}
foreach ($data as $key => $value) {
if (is_array($value)) {
if (array_values($value) === $value) { // Check if array is associative or not
$sub_node = $xml->addChild('json:array');
$sub_node->addAttribute('name', $key);
$this->arrayToJSONx($value, $sub_node, false);
} else {
$sub_node = $xml->addChild('json:object');
$sub_node->addAttribute('name', $key);
$this->arrayToJSONx($value, $sub_node);
}
} else {
if (is_null($value)) {
$child = $xml->addChild('json:null');
} elseif (is_bool($value)) {
$child = $xml->addChild('json:boolean', htmlspecialchars(var_export($value, true)));
} elseif (is_numeric($value)) {
$child = $xml->addChild('json:number', htmlspecialchars("$value"));
} else {
$child = $xml->addChild('json:string', htmlspecialchars("$value"));
}
if ($with_name) {
$child->addAttribute('name', $key);
}
}
}
return $xml;
}
public function send(string $first_name, string $last_name, string $email, string $bio,
array $technologies, int $timestamp, string $vcs_uri): string|false
{
$data = [
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'bio' => $bio,
'technologies' => $technologies,
'timestamp' => $timestamp,
'signature' => self::getSignature($timestamp),
'vcs_uri' => $vcs_uri
];
$content = $this->arrayToJSONx($data)->asXML();
$options = [
'http' => [
'protocol_version' => '1.1',
'method' => 'POST',
'header' =>
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' . PHP_EOL .
'Content-type: application/xml',
'content' => $content
]
];
$context = stream_context_create($options);
return file_get_contents($this->url, false, $context);
}
}