1
|
|
|
<?php // Copyright ⓒ 2018 Magneds IP B.V. - All Rights Reserved |
2
|
|
|
namespace Magneds\Lokalise\Project\Request; |
3
|
|
|
|
4
|
|
|
use Magneds\Lokalise\Project\Entity\ProjectID; |
5
|
|
|
use Magneds\Lokalise\RequestInterface; |
6
|
|
|
use Magneds\Lokalise\ResponseInfo; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class AddProjectRequest implements RequestInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $description; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $baseLang; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AddProjectRequest constructor. |
28
|
|
|
* @param string $name |
29
|
|
|
* @param string $description |
30
|
|
|
* @param string $baseLang |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $name, string $description, string $baseLang) |
33
|
|
|
{ |
34
|
|
|
$this->name = $name; |
35
|
|
|
$this->description = $description; |
36
|
|
|
$this->baseLang = $baseLang; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return the method to make this request in. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getMethod() |
45
|
|
|
{ |
46
|
|
|
return 'POST'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* This URI is appended to the route URL given to the Client object. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getURI() |
55
|
|
|
{ |
56
|
|
|
return 'project/add'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getQueryArguments() |
63
|
|
|
{ |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getBody() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
'name' => $this->name, |
74
|
|
|
'description' => $this->description, |
75
|
|
|
'base_lang' => $this->baseLang |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ResponseInterface $response |
81
|
|
|
* @return ProjectID |
82
|
|
|
*/ |
83
|
|
|
public function handleResponse(ResponseInterface $response): ResponseInfo |
84
|
|
|
{ |
85
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
86
|
|
|
$responseInfo = ResponseInfo::buildFromArray($responseData['response']); |
87
|
|
|
|
88
|
|
|
// Would be expecting 201 here. |
89
|
|
|
if ($responseInfo->getCode() !== 200) { |
90
|
|
|
return $responseInfo; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$responseInfo->setActionData(new ProjectID($responseData['project']['id'])); |
94
|
|
|
|
95
|
|
|
return $responseInfo; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|