• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / nexi / 8551769251

04 Apr 2024 08:40AM UTC coverage: 2.179% (+2.2%) from 0.0%
8551769251

push

github

mlocati
Improve JSON generation, add ways to enable insecure HTTPS connections

14 of 153 new or added lines in 7 files covered. (9.15%)

2 existing lines in 2 files now uncovered.

37 of 1698 relevant lines covered (2.18%)

0.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

32.04
/src/Entity.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MLocati\Nexi;
6

7
use Closure;
8
use JsonSerializable;
9
use stdClass;
10

11
abstract class Entity implements JsonSerializable
12
{
13
    /**
14
     * @var \stdClass
15
     */
16
    private $data;
17

18
    public function __construct(?stdClass $data = null)
19
    {
20
        $this->data = $data ?? new stdClass();
1✔
21
    }
1✔
22

23
    public function __clone()
24
    {
25
        $this->data = unserialize(serialize($this->data));
1✔
26
    }
1✔
27

28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @see \JsonSerializable::jsonSerialize()
32
     */
33
    public function jsonSerialize(): stdClass
34
    {
35
        return $this->data;
1✔
36
    }
37

38
    protected function _getRawData(): stdClass
39
    {
40
        return $this->data;
×
41
    }
42

43
    /**
44
     * @return $this
45
     */
46
    protected function _set(string $fieldName, $value): self
47
    {
48
        $this->data->{$fieldName} = $value;
1✔
49

50
        return $this;
1✔
51
    }
52

53
    /**
54
     * @return $this
55
     */
56
    protected function _unset(string $fieldName): self
57
    {
NEW
58
        unset($this->data->{$fieldName});
×
59

NEW
60
        return $this;
×
61
    }
62

63
    /**
64
     * @throws \MLocati\Nexi\Exception\MissingField
65
     * @throws \MLocati\Nexi\Exception\WrongFieldType
66
     */
67
    protected function _getString(string $fieldName, bool $required = false): ?string
68
    {
69
        return $this->_get($fieldName, ['string'], $required);
×
70
    }
71

72
    /**
73
     * @throws \MLocati\Nexi\Exception\MissingField
74
     * @throws \MLocati\Nexi\Exception\WrongFieldType
75
     *
76
     * @return string[]|null
77
     */
78
    protected function _getStringArray(string $fieldName, bool $required = false): ?array
79
    {
80
        return $this->_getArray(
×
81
            $fieldName,
×
82
            $required,
83
            static function ($value) use ($fieldName): string {
NEW
84
                if (gettype($value) !== 'string') {
×
NEW
85
                    throw new Exception\WrongFieldType($fieldName, 'string', $value);
×
86
                }
87

88
                return $value;
×
89
            }
×
90
        );
91
    }
92

93
    /**
94
     * @throws \MLocati\Nexi\Exception\MissingField
95
     * @throws \MLocati\Nexi\Exception\WrongFieldType
96
     */
97
    protected function _getInt(string $fieldName, bool $required = false): ?int
98
    {
99
        return $this->_get($fieldName, ['integer'], $required);
×
100
    }
101

102
    /**
103
     * @throws \MLocati\Nexi\Exception\MissingField
104
     * @throws \MLocati\Nexi\Exception\WrongFieldType
105
     *
106
     * @return int[]|null
107
     */
108
    protected function _getIntArray(string $fieldName, bool $required = false): ?array
109
    {
110
        return $this->_getArray(
×
111
            $fieldName,
×
112
            $required,
113
            static function ($value) use ($fieldName): int {
NEW
114
                if (gettype($value) !== 'integer') {
×
NEW
115
                    throw new Exception\WrongFieldType($fieldName, 'integer', $value);
×
116
                }
117

118
                return $value;
×
119
            }
×
120
        );
121
    }
122

123
    /**
124
     * @throws \MLocati\Nexi\Exception\MissingField
125
     * @throws \MLocati\Nexi\Exception\WrongFieldType
126
     */
127
    protected function _getBool(string $fieldName, bool $allow01 = false, bool $required = false): ?bool
128
    {
129
        try {
UNCOV
130
            return $this->_get($fieldName, ['boolean'], $required);
×
131
        } catch (Exception\WrongFieldType $x) {
×
NEW
132
            if ($allow01) {
×
NEW
133
                if ($x->getActualValue() === 0) {
×
NEW
134
                    return false;
×
135
                }
NEW
136
                if ($x->getActualValue() === 1) {
×
NEW
137
                    return true;
×
138
                }
139
            }
NEW
140
            throw $x;
×
141
        }
142
    }
143

144
    /**
145
     * @throws \MLocati\Nexi\Exception\MissingField
146
     * @throws \MLocati\Nexi\Exception\WrongFieldType
147
     *
148
     * @return bool[]|null
149
     */
150
    protected function _getBoolArray(string $fieldName, bool $allow01 = false, bool $required = false): ?array
151
    {
152
        return $this->_getArray(
×
153
            $fieldName,
×
154
            $required,
155
            static function ($value) use ($fieldName, $allow01): bool {
NEW
156
                if (gettype($value) === 'boolean') {
×
157
                    return $value;
×
158
                }
NEW
159
                if ($allow01) {
×
160
                    if ($value === 0) {
×
161
                        return false;
×
162
                    }
163
                    if ($value === 1) {
×
164
                        return true;
×
165
                    }
166
                }
NEW
167
                throw new Exception\WrongFieldType($fieldName, 'boolean', $value);
×
168
            }
×
169
        );
170
    }
171

172
    /**
173
     * @throws \MLocati\Nexi\Exception\MissingField
174
     * @throws \MLocati\Nexi\Exception\WrongFieldType
175
     */
176
    protected function _getEntity(string $fieldName, string $className, bool $required = false): ?Entity
177
    {
NEW
178
        $value = $this->_get($fieldName, ['object'], $required);
×
179
        if ($value === null) {
×
180
            return null;
×
181
        }
NEW
182
        if ($value instanceof stdClass) {
×
183
            $value = new $className($value);
×
NEW
184
            $this->_set($fieldName, $value);
×
185

186
            return $value;
×
187
        }
188
        if ($value instanceof $className) {
×
189
            return $value;
×
190
        }
NEW
191
        throw new Exception\WrongFieldType($fieldName, $className, $value);
×
192
    }
193

194
    /**
195
     * @throws \MLocati\Nexi\Exception\MissingField
196
     * @throws \MLocati\Nexi\Exception\WrongFieldType
197
     *
198
     * @return \MLocati\Nexi\Entity[]|null
199
     */
200
    protected function _getEntityArray(string $fieldName, string $className, bool $required = false): ?array
201
    {
202
        $array = $this->_getArray(
1✔
203
            $fieldName,
1✔
204
            $required,
205
            static function ($value) use ($fieldName, $className): Entity {
206
                if ($value instanceof $className) {
1✔
207
                    return $value;
1✔
208
                }
NEW
209
                if ($value instanceof stdClass) {
×
NEW
210
                    return new $className($value);
×
211
                }
NEW
212
                throw new Exception\WrongFieldType($fieldName, is_object($value) ? $className : 'object', $value);
×
213
            }
1✔
214
        );
215
        if ($array !== null) {
1✔
216
            $this->_set($fieldName, $array);
1✔
217
        }
218

219
        return $array;
1✔
220
    }
221

222
    /**
223
     * @throws \MLocati\Nexi\Exception\MissingField
224
     * @throws \MLocati\Nexi\Exception\WrongFieldType
225
     *
226
     * @return object|array|null
227
     */
228
    protected function _getCustomObject(string $fieldName, bool $required = false)
229
    {
230
        return $this->_get($fieldName, ['array', 'object'], $required);
×
231
    }
232

233
    /**
234
     * @throws \MLocati\Nexi\Exception\MissingField
235
     * @throws \MLocati\Nexi\Exception\WrongFieldType
236
     *
237
     * @return object[]|array[]|null
238
     */
239
    protected function _getCustomObjectArray(string $fieldName, bool $required = false): ?array
240
    {
241
        return $this->_getArray(
×
242
            $fieldName,
×
243
            $required,
244
            static function ($value) use ($fieldName) {
NEW
245
                if (!in_array(gettype($value), ['array', 'object'], true)) {
×
NEW
246
                    throw new Exception\WrongFieldType($fieldName, 'object|array', $value);
×
247
                }
248

249
                return $value;
×
250
            }
×
251
        );
252
    }
253

254
    /**
255
     * @throws \MLocati\Nexi\Exception\WrongFieldType
256
     *
257
     * @return $this
258
     */
259
    protected function _setBoolArray(string $fieldName, array $value): self
260
    {
261
        return $this->_setArray($fieldName, ['boolean'], $value);
×
262
    }
263

264
    /**
265
     * @throws \MLocati\Nexi\Exception\WrongFieldType
266
     *
267
     * @return $this
268
     */
269
    protected function _setIntArray(string $fieldName, array $value): self
270
    {
271
        return $this->_setArray($fieldName, ['integer'], $value);
×
272
    }
273

274
    /**
275
     * @throws \MLocati\Nexi\Exception\WrongFieldType
276
     *
277
     * @return $this
278
     */
279
    protected function _setStringArray(string $fieldName, array $value): self
280
    {
281
        return $this->_setArray($fieldName, ['string'], $value);
×
282
    }
283

284
    /**
285
     * @throws \MLocati\Nexi\Exception\WrongFieldType
286
     *
287
     * @return $this
288
     */
289
    protected function _setEntityArray(string $fieldName, string $className, array $value): self
290
    {
291
        return $this->_setArray(
1✔
292
            $fieldName,
1✔
293
            ['object'],
1✔
294
            $value,
295
            static function (object $instance) use ($fieldName, $className): void {
296
                if (!$instance instanceof $className) {
1✔
NEW
297
                    throw new Exception\WrongFieldType($fieldName, $className, $instance);
×
298
                }
299
            }
1✔
300
        );
301
    }
302

303
    /**
304
     * @throws \MLocati\Nexi\Exception\WrongFieldType
305
     *
306
     * @return $this
307
     */
308
    protected function _setCustomObject(string $fieldName, $value): self
309
    {
NEW
310
        if (!in_array(gettype($value), ['array', 'object'], true)) {
×
NEW
311
            throw new Exception\WrongFieldType($fieldName, 'array|object', $value);
×
312
        }
313

314
        return $this->_set($fieldName, $value);
×
315
    }
316

317
    /**
318
     * @throws \MLocati\Nexi\Exception\WrongFieldType
319
     *
320
     * @return $this
321
     */
322
    protected function _setCustomObjectArray(string $fieldName, array $value): self
323
    {
324
        return $this->_setArray($fieldName, ['array', 'object'], $value);
×
325
    }
326

327
    /**
328
     * @throws \MLocati\Nexi\Exception\MissingField
329
     * @throws \MLocati\Nexi\Exception\WrongFieldType
330
     */
331
    private function _get(string $fieldName, array $types, bool $required)
332
    {
333
        $value = $this->data->{$fieldName} ?? null;
1✔
334
        if ($value === null) {
1✔
335
            if ($required) {
×
336
                throw new Exception\MissingField($fieldName);
×
337
            }
338

339
            return null;
×
340
        }
341
        if (!in_array(gettype($value), $types, true)) {
1✔
NEW
342
            throw new Exception\WrongFieldType($fieldName, implode('|', $types), $value);
×
343
        }
344

345
        return $value;
1✔
346
    }
347

348
    /**
349
     * @throws \MLocati\Nexi\Exception\MissingField
350
     * @throws \MLocati\Nexi\Exception\WrongFieldType
351
     */
352
    private function _getArray(string $fieldName, bool $required, Closure $callback): ?array
353
    {
354
        $values = $this->_get($fieldName, ['array'], $required);
1✔
355
        if ($values === null) {
1✔
356
            return $values;
×
357
        }
358

359
        return array_map($callback, $values);
1✔
360
    }
361

362
    /**
363
     * @throws \MLocati\Nexi\Exception\MissingField
364
     *
365
     * @return $this
366
     */
367
    private function _setArray(string $fieldName, array $types, array $value, ?Closure $callback = null): self
368
    {
369
        array_map(
1✔
370
            static function ($item) use ($fieldName, $types, $callback) {
371
                if (!in_array(gettype($item), $types, true)) {
1✔
NEW
372
                    throw new Exception\WrongFieldType($fieldName, implode('|', $types), $item);
×
373
                }
374
                if ($callback !== null) {
1✔
375
                    $callback($item);
1✔
376
                }
377
            },
1✔
378
            $value
379
        );
380

381
        return $this->_set($fieldName, array_values($value));
1✔
382
    }
383
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc