Passed
Push — master ( c1f9a4...4b0053 )
by Rimas
02:42 queued 32s
created

ResponseMapper::map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
namespace Dokobit\Gateway;
3
4
use Dokobit\Gateway\Result\ResultInterface;
5
6
/**
7
 * Mapping response array to result objects
8
 */
9
class ResponseMapper implements ResponseMapperInterface
10
{
11
    /**
12
     * @param array           $response
13
     * @param ResultInterface $result
14
     *
15
     * @return ResultInterface
16
     */
17
    public function map(array $response, ResultInterface $result): ResultInterface
18
    {
19
        foreach ($result->getFields() as $field) {
20
            $this->mapField($field, $response, $result);
21
        }
22
23
        return $result;
24
    }
25
26
    /**
27
     * Map single field from response to result object. Omit the fields which
28
     * are not present in response array.
29
     * @param string $field
30
     * @param array $response
31
     * @param ResultInterface $result
32
     * @return void
33
     */
34
    protected function mapField($field, array $response, ResultInterface $result)
35
    {
36
        if (!isset($response[$field])) {
37
            return;
38
        }
39
        $method = 'set' . $this->toMethodName($field);
40
        $result->$method($response[$field]);
41
    }
42
43
    /**
44
     * Convert value to camel case method name with first capital letter
45
     * @param string $value
46
     * @return string
47
     */
48
    protected function toMethodName($value)
49
    {
50
        $parts = explode('_', $value);
51
        $parts = array_map('ucfirst', $parts);
52
53
        return implode('', $parts);
54
    }
55
}
56