ItemChange::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Tmdb package.
4
 *
5
 * (c) Vincent Faliès <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author Vincent Faliès <[email protected]>
11
 * @copyright Copyright (c) 2017-2021
12
 */
13
14
namespace VfacTmdb\Results;
15
16
use VfacTmdb\Abstracts\Results;
17
use VfacTmdb\Interfaces\TmdbInterface;
18
use VfacTmdb\Interfaces\Results\ItemChangeResultsInterface;
19
20
/**
21
 * Class to manipulate an ItemChange result
22
 * @package Tmdb
23
 * @author Steve Richter <[email protected]>
24
 * @copyright Copyright (c) 2021
25
 */
26
class ItemChange extends Results implements ItemChangeResultsInterface
27
{
28
    /**
29
     * Id
30
     * @var string
31
     */
32
    protected $id;
33
    /**
34
     * Key
35
     * @var string
36
     */
37
    protected $key;
38
    /**
39
     * Action
40
     * @var string
41
     */
42
    protected $action;
43
    /**
44
     * Time
45
     * @var string
46
     */
47
    protected $time;
48
    /**
49
     * iso_639_1
50
     * @var string
51
     */
52
    protected $iso_639_1 = null;
53
    /**
54
     * iso_3166_1
55
     * @var string
56
     */
57
    protected $iso_3166_1 = null;
58
    /**
59
     * Value
60
     * @var array
61
     */
62
    protected $value;
63
    /**
64
     * Original value
65
     * @var array
66
     */
67
    protected $original_value;
68
69
    /**
70
     * Constructor
71
     * @param TmdbInterface $tmdb
72
     * @param \stdClass $result
73
     */
74 57
    public function __construct(TmdbInterface $tmdb, \stdClass $result)
75
    {
76 57
        $result = $this->initResultObject($result);
77
78 57
        parent::__construct($tmdb, $result);
79
80
        // Populate data
81 57
        $this->id             = $this->data->id;
82 57
        $this->key            = $this->data->key;
83 57
        $this->action         = $this->data->action;
84 57
        $this->time           = $this->data->time;
85 57
        $this->iso_639_1      = $this->data->iso_639_1;
86 57
        $this->iso_3166_1     = $this->data->iso_3166_1;
87 57
        $this->value          = $this->data->value;
88 57
        $this->original_value = $this->data->original_value;
89 57
    }
90
91
    /**
92
     * initResultObject
93
     * @param  \stdClass  $result
94
     * @return \stdClass
95
     */
96 57
    private function initResultObject(\stdClass $result) : \stdClass
97
    {
98 57
        foreach (['value', 'original_value'] as $key) {
99 57
            if (!isset($result->{$key})) {
100 54
                $result->{$key} = [];
101
            }
102 57
            if (is_object($result->{$key})) {
103 51
                $result->{$key} = get_object_vars($result->{$key});
104
            }
105 57
            if (!is_array($result->{$key})) {
106 48
                $result->{$key} = [$result->{$key}];
107
            }
108
        }
109 57
        return $result;
110
    }
111
112
    /**
113
     * Get Id
114
     * @return string
115
     */
116 6
    public function getId() : string
117
    {
118 6
        return $this->id;
119
    }
120
121
    /**
122
     * Get key
123
     * @return string
124
     */
125 42
    public function getKey() : string
126
    {
127 42
        return $this->key;
128
    }
129
130
    /**
131
     * Get action
132
     * @return string
133
     */
134 3
    public function getAction() : string
135
    {
136 3
        return $this->action;
137
    }
138
139
    /**
140
     * Get time
141
     * @return \DateTime
142
     */
143 3
    public function getTime() : \DateTime
144
    {
145 3
        return \DateTime::createFromFormat('Y-m-d H:i:s e', $this->time);
146
    }
147
148
    /**
149
     * Get iso_639_1
150
     * @return  string
151
     */
152 3
    public function getIso_639_1() : string
153
    {
154 3
        return $this->iso_639_1;
155
    }
156
157
    /**
158
     * Get iso_3166_1
159
     * @return  string
160
     */
161 3
    public function getIso_3166_1() : string
162
    {
163 3
        return $this->iso_3166_1;
164
    }
165
166
    /**
167
     * Get value
168
     * @return  array
169
     */
170 3
    public function getValue() : array
171
    {
172 3
        return $this->value;
173
    }
174
175
    /**
176
     * Get original value
177
     * @return  array
178
     */
179 3
    public function getOriginalValue() : array
180
    {
181 3
        return $this->original_value;
182
    }
183
184
    /**
185
     * Get value by key
186
     * @param   string  $key
187
     * @return  mixed
188
     */
189 12
    public function getValueByKey($key)
190
    {
191 12
        return $this->value[$key] ?? null;
192
    }
193
}
194