aboutsummaryrefslogtreecommitdiffstats
path: root/public/system/storage/vendor/cardinity/cardinity-sdk-php/src/Method/ResultObjectMapper.php
blob: b4b1c6739959c7afce3ba2fbd60daa7da47c83a2 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Cardinity\Method;

use Cardinity\Exception;
use Cardinity\Method\MethodInterface;
use Cardinity\Method\MethodResultCollectionInterface;
use Cardinity\Method\Payment\AuthorizationInformation;
use Cardinity\Method\Payment\PaymentInstrumentCard;
use Cardinity\Method\Payment\PaymentInstrumentRecurring;

class ResultObjectMapper implements ResultObjectMapperInterface
{
    /**
     * Map each item in response data to instance of ResultObjectInterface
     * @param array $response
     * @param MethodResultCollectionInterface $method
     *
     * @return array
     */
    public function mapCollection(array $response, MethodResultCollectionInterface $method)
    {
        $return = [];

        foreach ($response as $item) {
            $return[] = $this->map($item, $method->createResultObject());
        }

        return $return;
    }

    /**
     * Map response data to instance of ResultObjectInterface
     * @param array           $response
     * @param ResultObjectInterface $result
     *
     * @return ResultObjectInterface
     */
    public function map(array $response, ResultObjectInterface $result)
    {
        foreach ($response as $field => $value) {
            $method = $this->getSetterName($field);

            if (!method_exists($result, $method)) {
                throw new Exception\ResultObjectInterfacePropertyNotFound(
                    sprintf(
                        'Result object %s property "%s" not found.',
                        get_class($result),
                        $field
                    )
                );
            }

            if ($field == 'payment_instrument') {
                $value = $this->transformPaymentInstrumentValue($value, $response['payment_method']);
            } elseif ($field == 'authorization_information') {
                $value = $this->transformAuthorizationInformationValue($value);
            }

            $result->$method($value);
        }

        return $result;
    }

    /**
     * Extracts camelCased setter name from underscore notation.
     * Eg. my_field_name => myFieldName
     * @param string $field
     * @return string
     */
    private function getSetterName($field)
    {
        $parts = explode('_', $field);
        array_map('ucfirst', $parts);
        $name = 'set' . implode('', $parts);

        return $name;
    }

    /**
     * Transform PaymentInstrument result array to object
     * @param array $data
     * @param string $method
     * @return PaymentInstrumentCard|PaymentInstrumentRecurring
     * @throws Exception\Runtime for unsupported methods
     */
    private function transformPaymentInstrumentValue(array $data, $method)
    {
        if ($method == 'card') {
            $instrument = new PaymentInstrumentCard();
        } elseif ($method == 'recurring') {
            $instrument = new PaymentInstrumentRecurring();
        } else {
            throw new Exception\Runtime(sprintf('Method "%s" is not supported', $method));
        }

        $this->map($data, $instrument);

        return $instrument;
    }

    /**
     * Transform AuthorizationInformation result array to object
     * @param array $data
     * @return AuthorizationInformation
     */
    private function transformAuthorizationInformationValue($data)
    {
        $info = new AuthorizationInformation();
        $this->map($data, $info);

        return $info;
    }
}