1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace leocata\M1\Abstracts; |
4
|
|
|
|
5
|
|
|
use leocata\M1\Exceptions\MissingMandatoryField; |
6
|
|
|
use leocata\M1\Interfaces\MethodDefinitions; |
7
|
|
|
|
8
|
|
|
abstract class RequestMethods implements MethodDefinitions |
9
|
|
|
{ |
10
|
|
|
private $result; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @return mixed |
14
|
|
|
*/ |
15
|
|
|
final public function getResult() |
16
|
|
|
{ |
17
|
|
|
return $this->result->result; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
final public function setResult($data) |
21
|
|
|
{ |
22
|
|
|
if (is_array($data->result)) { |
23
|
|
|
foreach ($data->result as $key => $value) { |
24
|
|
|
$this->result[$key] = $value; |
25
|
|
|
} |
26
|
|
|
} else { |
27
|
|
|
$this->result = $data; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
final public function getRequestString() |
35
|
|
|
{ |
36
|
|
|
$request = ['method' => $this->getMethodName()]; |
37
|
|
|
|
38
|
|
|
if (!empty($params = $this->export())) { |
39
|
|
|
$request += ['params' => $params]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return \GuzzleHttp\json_encode($request); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws MissingMandatoryField |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
2 |
|
final public function export() |
51
|
|
|
{ |
52
|
2 |
|
$finalArray = []; |
53
|
2 |
|
$mandatoryFields = $this->getMandatoryFields(); |
54
|
|
|
|
55
|
2 |
|
$cleanObject = new $this(); |
56
|
2 |
|
foreach ($cleanObject as $fieldId => $value) { |
57
|
2 |
|
if ($this->$fieldId === $cleanObject->$fieldId) { |
58
|
2 |
|
if (in_array($fieldId, $mandatoryFields, true)) { |
59
|
2 |
|
throw new MissingMandatoryField(sprintf( |
60
|
2 |
|
'The field "%s" is mandatory and empty, please correct', |
61
|
2 |
|
$fieldId |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
$finalArray[$fieldId] = $this->$fieldId; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return empty($finalArray) ? null : $finalArray; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
final public function getMethodName(): string |
73
|
|
|
{ |
74
|
|
|
return lcfirst((new \ReflectionClass($this))->getShortName()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|