DataWrapper::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.3142
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
3
namespace Chadicus\Marvel\Api;
4
5
use DominionEnterprises\Util;
6
7
/**
8
 * Object representation of the API result data wrapper.
9
 */
10
final class DataWrapper implements DataWrapperInterface
11
{
12
    /**
13
     * The HTTP status code of the returned result.
14
     *
15
     * @var integer
16
     */
17
    private $code;
18
19
    /**
20
     * A string description of the call status.
21
     *
22
     * @var string
23
     */
24
    private $status;
25
26
    /**
27
     * The copyright notice for the returned result.
28
     *
29
     * @var string
30
     */
31
    private $copyright;
32
33
    /**
34
     * The attribution notice for this result
35
     *
36
     * @var string
37
     */
38
    private $attributionText;
39
40
    /**
41
     * An HTML representation of the attribution notice for this result.
42
     *
43
     * @var string
44
     */
45
    private $attributionHTML;
46
47
    /**
48
     * A digest value of the content returned by the call.
49
     *
50
     * @var string
51
     */
52
    private $etag;
53
54
    /**
55
     * The results returned by the call.
56
     *
57
     * @var DataContainerInterface
58
     */
59
    private $data;
60
61
    /**
62
     * Create a new DataWrapper instance.
63
     *
64
     * @param array $input The data for the DataWrapper.
65
     */
66
    public function __construct(array $input)
67
    {
68
        $filters = [
69
            'code' => [['int', true]],
70
            'status' => [['string', true]],
71
            'copyright' => [['string', true]],
72
            'attributionText' => [['string', true]],
73
            'attributionHTML' => [['string', true]],
74
            'etag' => [['string', true]],
75
            'data' => ['default' => [], ['array', 0]],
76
        ];
77
78
        list($success, $filteredInput, $error) = Filterer::filter($filters, $input, ['allowUnknowns' => true]);
79
        Util::ensure(true, $success, $error);
80
81
        foreach ($filteredInput as $key => $value) {
82
            $this->$key = $value;
83
        }
84
85
        $this->data = new DataContainer($filteredInput['data']);
86
    }
87
88
    /**
89
     * Construct a DataWrapper from the given JSON string.
90
     *
91
     * @param string $json The json encoded data.
92
     *
93
     * @return static
94
     */
95
    public static function fromJson(string $json)
96
    {
97
        return new static(json_decode($json, true));
98
    }
99
100
    /**
101
     * Returns the HTTP status code of the returned result.
102
     *
103
     * @return integer
104
     */
105
    public function getCode() : int
106
    {
107
        return $this->code;
108
    }
109
110
    /**
111
     * Returns A string description of the call status.
112
     *
113
     * @return string
114
     */
115
    public function getStatus() : string
116
    {
117
        return $this->status;
118
    }
119
120
    /**
121
     * Returns the copyright notice for the returned result.
122
     *
123
     * @return string
124
     */
125
    public function getCopyright() : string
126
    {
127
        return $this->copyright;
128
    }
129
130
    /**
131
     * Returns the attribution notice for this result
132
     *
133
     * @return string
134
     */
135
    public function getAttributionText() : string
136
    {
137
        return $this->attributionText;
138
    }
139
140
    /**
141
     * Returns an HTML representation of the attribution notice for this result.
142
     *
143
     * @return string
144
     */
145
    public function getAttributionHTML() : string
146
    {
147
        return $this->attributionHTML;
148
    }
149
150
    /**
151
     * Returns a digest value of the content returned by the call.
152
     *
153
     * @return string
154
     */
155
    public function getEtag() : string
156
    {
157
        return $this->etag;
158
    }
159
160
    /**
161
     * Returns the results returned by the call.
162
     *
163
     * @return DataContainerInterface
164
     */
165
    public function getData() : DataContainerInterface
166
    {
167
        return $this->data;
168
    }
169
}
170