1 | <?php |
||||
2 | |||||
3 | namespace DansMaCulotte\Monetico\Responses; |
||||
4 | |||||
5 | use DansMaCulotte\Monetico\Exceptions\Exception; |
||||
6 | |||||
7 | abstract class AbstractResponse |
||||
8 | { |
||||
9 | /** @var array */ |
||||
10 | const REQUIRED_KEYS = [ |
||||
11 | 'cdr' => 'returnCode', |
||||
12 | 'lib' => 'description', |
||||
13 | 'version' => 'version', |
||||
14 | 'reference' => 'reference', |
||||
15 | ]; |
||||
16 | |||||
17 | /** @var string */ |
||||
18 | public $returnCode; |
||||
19 | |||||
20 | /** @var string */ |
||||
21 | public $description; |
||||
22 | |||||
23 | /** @var string */ |
||||
24 | public $version; |
||||
25 | |||||
26 | /** @var string */ |
||||
27 | public $reference; |
||||
28 | |||||
29 | /** |
||||
30 | * AbstractResponse constructor. |
||||
31 | * @param array $data |
||||
32 | * @throws Exception |
||||
33 | */ |
||||
34 | public function __construct(array $data) |
||||
35 | { |
||||
36 | $this->validateRequiredKeys($data); |
||||
37 | $this->setDataRequiredKeys($data); |
||||
38 | } |
||||
39 | |||||
40 | /** |
||||
41 | * @return array |
||||
42 | */ |
||||
43 | protected function getRequiredKeys(): array |
||||
44 | { |
||||
45 | return self::REQUIRED_KEYS; |
||||
46 | } |
||||
47 | |||||
48 | /** |
||||
49 | * @param array $data |
||||
50 | * @throws Exception |
||||
51 | */ |
||||
52 | private function validateRequiredKeys(array $data): void |
||||
53 | { |
||||
54 | $keys = $this->getRequiredKeys(); |
||||
55 | foreach ($keys as $key => $value) { |
||||
56 | if (!array_key_exists($key, $data)) { |
||||
57 | throw Exception::missingResponseKey($key); |
||||
58 | } |
||||
59 | } |
||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * @param array $data |
||||
64 | */ |
||||
65 | private function setDataRequiredKeys(array $data) |
||||
66 | { |
||||
67 | $keys = $this->getRequiredKeys(); |
||||
68 | |||||
69 | foreach ($data as $key => $value) { |
||||
70 | if (isset($keys[$key])) { |
||||
71 | $method = $keys[$key]; |
||||
72 | $this->$method = $value; |
||||
73 | } |
||||
74 | } |
||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * @param string $eptCode |
||||
79 | * @param string $companyCode |
||||
80 | * @return bool |
||||
81 | */ |
||||
82 | public function validateSeal(string $eptCode, string $companyCode): bool |
||||
0 ignored issues
–
show
The parameter
$eptCode is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
83 | { |
||||
84 | return false; |
||||
85 | } |
||||
86 | } |
||||
87 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.