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

mlocati / nexi / 8551303918

04 Apr 2024 08:05AM UTC coverage: 2.295% (+2.3%) from 0.0%
8551303918

push

github

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

12 of 89 new or added lines in 7 files covered. (13.48%)

2 existing lines in 2 files now uncovered.

39 of 1699 relevant lines covered (2.3%)

0.02 hits per line

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

31.82
/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 {
84
                $type = gettype($value);
×
85
                if ($type !== 'string') {
×
NEW
86
                    throw new Exception\WrongFieldType($fieldName, 'string', $value);
×
87
                }
88

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

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

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

120
                return $value;
×
121
            }
×
122
        );
123
    }
124

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

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

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

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

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

222
        return $array;
1✔
223
    }
224

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

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

253
                return $value;
×
254
            }
×
255
        );
256
    }
257

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

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

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

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

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

319
        return $this->_set($fieldName, $value);
×
320
    }
321

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

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

344
            return null;
×
345
        }
346
        $type = gettype($value);
1✔
347
        if (!in_array($type, $types, true)) {
1✔
NEW
348
            throw new Exception\WrongFieldType($fieldName, implode('|', $types), $value);
×
349
        }
350

351
        return $value;
1✔
352
    }
353

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

365
        return array_map($callback, $values);
1✔
366
    }
367

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

388
        return $this->_set($fieldName, array_values($value));
1✔
389
    }
390
}
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