aboutsummaryrefslogtreecommitdiffstats
path: root/public/system/storage/vendor/cardinity/cardinity-sdk-php/src/Method/ResultObject.php
blob: 006d9e7bc1e9a55e94db2f60e46945ace4c47340 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

namespace Cardinity\Method;

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

abstract class ResultObject implements ResultObjectInterface
{
    /**
     * Wrap single result error into array of errors
     * @return array
     */
    public function getErrors()
    {
        return [
            [
                'field' => 'status',
                'message' => $this->getError()
            ]
        ];
    }

    /**
     * Return single error
     */
    public function getError()
    {
        return '';
    }

    /**
     * Serializes result object to json object
     * @param boolean $toJson encode result to json
     * @return string
     */
    public function serialize($toJson = true)
    {
        $data = [];

        $getters = $this->classGetters(get_class($this));
        foreach ($getters as $method) {
            $property = $this->propertyName($method);
            $value = $this->$method();

            if (is_float($value)) {
                $value = sprintf("%01.2f", $value);
            } elseif (is_object($value)) {
                $value = $value->serialize(false);
            }

            if ($value !== null) {
                $data[$property] = $value;
            }
        }

        if ($toJson === true) {
            return json_encode($data);
        }

        return $data;
    }

    /**
     * Loads result object values from json object
     * @param string $string json
     * @return void
     */
    public function unserialize($string)
    {
        $data = json_decode($string);
        foreach ($data as $property => $value) {
            $method = $this->setterName($property);

            if (is_numeric($value) && strstr($value, '.')) {
                $value = floatval($value);
            } elseif (is_object($value)) {
                if ($property == 'authorization_information') {
                    $object = new AuthorizationInformation();
                    $object->unserialize(json_encode($value));
                    $value = $object;
                } elseif ($property == 'payment_instrument') {
                    if (!isset($data->payment_method)) {
                        throw new Exception\Runtime('Property "payment_method" is missing');
                    }

                    if ($data->payment_method == Payment\Create::CARD) {
                        $object = new PaymentInstrumentCard();
                    } elseif ($data->payment_method == Payment\Create::RECURRING) {
                        $object = new PaymentInstrumentRecurring();
                    }
                    $object->unserialize(json_encode($value));
                    $value = $object;
                }
            }

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

    /**
     * @param string $class
     */
    private function classGetters($class)
    {
        $methods = get_class_methods($class);
        return array_filter($methods, function ($value) use ($methods) {
            if ($value == 'getErrors') {
                return false;
            }

            // no setter means it's inherited property, should be ignored
            $setter = $this->setterName($this->propertyName($value));
            if (!in_array($setter, $methods)) {
                return false;
            }

            return substr($value, 0, 3) == 'get';
        });
    }

    private function propertyName($method)
    {
        $method = lcfirst(substr($method, 3));
        $method = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $method));
        return $method;
    }

    private function setterName($property)
    {
        $parts = explode('_', $property);
        $parts = array_map('ucfirst', $parts);
        $property = implode('', $parts);

        return 'set' . ucfirst($property);
    }
}