Completed
Pull Request — master (#9)
by Jérémy
06:26 queued 02:39
created

Payload   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 14
c 0
b 0
f 0
dl 0
loc 45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataType() 0 3 1
A getStringData() 0 11 3
A getArrayData() 0 11 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Thunder micro CLI framework.
7
 * (c) Jérémy Marodon <[email protected]>
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RxThunder\Core\Model;
13
14
use RxThunder\Core\Model\Exception\InvalidFormatException;
15
16
final class Payload
17
{
18
    /** @var array<mixed, mixed>|string */
19
    private $data;
20
21
    /**
22
     * @param array<mixed, mixed>|string $data
23
     */
24
    public function __construct($data = null)
25
    {
26
        $this->data = $data;
27
    }
28
29
    public function getStringData(): ?string
30
    {
31
        if ($this->data === null) {
32
            return null;
33
        }
34
35
        if (!\is_string($this->data)) {
36
            throw new InvalidFormatException($this->getDataType(), 'string');
37
        }
38
39
        return $this->data;
40
    }
41
42
    /**
43
     * @return array<mixed, mixed>|null
44
     */
45
    public function getArrayData(): ?array
46
    {
47
        if ($this->data === null) {
48
            return null;
49
        }
50
51
        if (!\is_array($this->data)) {
52
            throw new InvalidFormatException($this->getDataType(), 'array');
53
        }
54
55
        return $this->data;
56
    }
57
58
    public function getDataType(): string
59
    {
60
        return \gettype($this->data);
61
    }
62
}
63