Passed
Push — master ( 0676a7...01ab94 )
by Artem
11:54
created

Entity::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
// ---------------------------------------------------------------------
4
//
5
//  Copyright (C) 2018-2024 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <https://opensource.org/licenses/MIT>.
9
//
10
// ---------------------------------------------------------------------
11
12
namespace Linode;
13
14
/**
15
 * An abstract read-only entity.
16
 */
17
abstract class Entity
18
{
19
    /**
20
     * @param LinodeClient $client Linode API client.
21
     * @param array        $data   JSON data retrieved from Linode.
22
     */
23 3
    public function __construct(
24
        protected LinodeClient $client,
25
        protected array $data = []
26 3
    ) {}
27
28
    /**
29
     * Checks whether the specified property exists in the entity.
30
     *
31
     * @param string $name Property name.
32
     */
33 1
    public function __isset(string $name): bool
34
    {
35 1
        return array_key_exists($name, $this->data);
36
    }
37
38
    /**
39
     * Returns current value of the specified property, or `null` if the property doesn't exist.
40
     *
41
     * @param string $name Property name.
42
     *
43
     * @return null|mixed Property value.
44
     */
45 1
    public function __get(string $name): mixed
46
    {
47 1
        return $this->data[$name] ?? null;
48
    }
49
50
    /**
51
     * Converts the entity into an array.
52
     */
53 1
    public function toArray(): array
54
    {
55 1
        return $this->data;
56
    }
57
}
58