Passed
Push — master ( 302660...3652f8 )
by payever
04:19 queued 01:32
created

MessageEntity   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 31
eloc 55
c 1
b 0
f 0
dl 0
loc 199
rs 9.92

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequired() 0 3 1
B toArray() 0 29 9
A offsetExists() 0 5 1
A __call() 0 20 6
A toString() 0 3 1
A offsetSet() 0 5 1
A __construct() 0 4 2
A load() 0 9 2
A offsetGet() 0 9 2
A __toString() 0 3 1
A offsetUnset() 0 5 1
A isValid() 0 13 4
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Base
7
 * @package   Payever\Core
8
 * @author    payever GmbH <[email protected]>
9
 * @copyright 2017-2021 payever GmbH
10
 * @license   MIT <https://opensource.org/licenses/MIT>
11
 * @link      https://docs.payever.org/shopsystems/api/getting-started
12
 */
13
14
namespace Payever\ExternalIntegration\Core\Base;
15
16
use Payever\ExternalIntegration\Core\Helper\StringHelper;
17
18
/**
19
 * Class implements and describes functions of Message Entity
20
 * @SuppressWarnings(PHPMD.NumberOfChildren)
21
 * @SuppressWarnings(PHPMD.StaticAccess)
22
 */
23
abstract class MessageEntity implements MessageEntityInterface, \ArrayAccess
24
{
25
    /**
26
     * Whether entity fields must be underscored when converting to array/json
27
     */
28
    const UNDERSCORE_ON_SERIALIZATION = true;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function __construct($data = null)
34
    {
35
        if ($data) {
36
            $this->load($data);
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function load($data)
44
    {
45
        foreach ($data as $key => $value) {
46
            $function = StringHelper::camelize('set_' . $key);
47
48
            $this->{$function}($value);
49
        }
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function toString()
58
    {
59
        return json_encode($this->toArray());
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * @SuppressWarnings(PHPMD.ElseExpression)
65
     */
66
    public function toArray($object = null)
67
    {
68
        $result = [];
69
70
        if ($object === null) {
71
            $object = get_object_vars($this);
72
        }
73
74
        foreach ($object as $property => $value) {
75
            if (is_null($value)) {
76
                continue;
77
            }
78
79
            if (static::UNDERSCORE_ON_SERIALIZATION && !is_integer($property)) {
80
                $property = StringHelper::underscore($property);
81
            }
82
83
            if (is_array($value)) {
84
                $result[$property] = $this->toArray($value);
85
            } elseif ($value instanceof MessageEntity) {
86
                $result[$property] = $value->toArray();
87
            } elseif ($value instanceof \DateTime) {
88
                $result[$property] = $value->format(DATE_ATOM);
89
            } else {
90
                $result[$property] = $value;
91
            }
92
        }
93
94
        return $result;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getRequired()
101
    {
102
        return [];
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function isValid()
109
    {
110
        foreach ($this->getRequired() as $property) {
111
            if (static::UNDERSCORE_ON_SERIALIZATION) {
112
                $property = StringHelper::camelize($property);
113
            }
114
115
            if ($this->{$property} === null) {
116
                return false;
117
            }
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * Implements magic method __toString()
125
     *
126
     * @return string
127
     */
128
    public function __toString()
129
    {
130
        return $this->toString();
131
    }
132
133
    /**
134
     * Set/Get attribute wrapper
135
     *
136
     * @param string $method
137
     * @param array  $args
138
     *
139
     * @return self|bool|mixed
140
     */
141
    public function __call($method, $args)
142
    {
143
        $property = lcfirst(substr($method, 3));
144
145
        switch (substr($method, 0, 3)) {
146
            case 'get':
147
                if (property_exists($this, $property)) {
148
                    return $this->{$property};
149
                }
150
151
                return null;
152
            case 'set':
153
                if (property_exists($this, $property)) {
154
                    $this->{$property} = isset($args[0]) ? $args[0] : null;
155
                }
156
157
                return $this;
158
        }
159
160
        return false;
161
    }
162
163
    /**
164
     * Implementation of \ArrayAccess::offsetSet()
165
     *
166
     * @param string $offset
167
     * @param mixed $value
168
     * @return void
169
     * @link http://www.php.net/manual/en/arrayaccess.offsetset.php
170
     */
171
    public function offsetSet($offset, $value)
172
    {
173
        $property = StringHelper::camelize($offset);
174
175
        $this->{$property} = $value;
176
    }
177
178
    /**
179
     * Implementation of \ArrayAccess::offsetExists()
180
     *
181
     * @param string $offset
182
     * @return bool
183
     * @link http://www.php.net/manual/en/arrayaccess.offsetexists.php
184
     */
185
    public function offsetExists($offset)
186
    {
187
        $property = StringHelper::camelize($offset);
188
189
        return property_exists($this, $property);
190
    }
191
192
    /**
193
     * Implementation of \ArrayAccess::offsetUnset()
194
     *
195
     * @param string $offset
196
     * @return void
197
     * @link http://www.php.net/manual/en/arrayaccess.offsetunset.php
198
     */
199
    public function offsetUnset($offset)
200
    {
201
        $property = StringHelper::camelize($offset);
202
203
        unset($this->{$property});
204
    }
205
206
    /**
207
     * Implementation of \ArrayAccess::offsetGet()
208
     *
209
     * @param string $offset
210
     * @return mixed
211
     * @link http://www.php.net/manual/en/arrayaccess.offsetget.php
212
     */
213
    public function offsetGet($offset)
214
    {
215
        $property = StringHelper::camelize($offset);
216
217
        if (property_exists($this, $property)) {
218
            return $this->{$property};
219
        }
220
221
        return null;
222
    }
223
}
224