ObjectResource::__get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Lifeboat\Resource;
4
5
use Lifeboat\Connector;
6
use IteratorAggregate;
7
use ArrayIterator;
8
9
/**
10
 * Class ResourceList
11
 * @package Lifeboat\Resource
12
 *
13
 * @property array $_object_data
14
 */
15
abstract class ObjectResource extends ApiResource implements IteratorAggregate {
16
17
    protected static $casting = [];
18
19
    private $_object_data;
20
21
    /**
22
     * ObjectResource constructor.
23
     * @param Connector $client
24
     * @param array $_object_data
25
     */
26
    public function __construct(Connector $client, array $_object_data = [])
27
    {
28
        parent::__construct($client);
29
30
        foreach ($_object_data as $k => $v) {
31
            if (!is_null($v) && array_key_exists($k, static::$casting)) {
32
                $cls_func = static::$casting[$k];
33
                $v = (class_exists($cls_func)) ? new $cls_func($v) : $cls_func($v);
34
            }
35
36
            $this->__set($k, $v);
37
        }
38
    }
39
40
    /**
41
     * @param string $field
42
     * @return mixed
43
     */
44
    public function __get(string $field)
45
    {
46
        return $this->_object_data[$field] ?? null;
47
    }
48
49
    /**
50
     * @return ArrayIterator
51
     */
52
    public function getIterator(): ArrayIterator
53
    {
54
        return new ArrayIterator($this->_object_data);
55
    }
56
57
    /**
58
     * @param string $field
59
     * @param $value
60
     * @return void
61
     */
62
    public function __set(string $field, $value): void
63
    {
64
        $this->_object_data[$field] = $value;
65
    }
66
67
    public function offsetGet($offset)
68
    {
69
        return $this->_object_data[$offset] ?? null;
70
    }
71
72
    public function offsetSet($offset, $value)
73
    {
74
        return $this->_object_data[$offset] = $value;
75
    }
76
77
    /**
78
     * @param mixed $offset
79
     * @return void
80
     */
81
    public function offsetUnset($offset): void
82
    {
83
        unset($this->_object_data[$offset]);
84
    }
85
86
    /**
87
     * @param mixed $offset
88
     * @return bool
89
     */
90
    public function offsetExists($offset): bool
91
    {
92
        return array_key_exists($offset, $this->_object_data);
93
    }
94
95
    /**
96
     * @return int
97
     */
98
    public function count(): int
99
    {
100
        return count($this->_object_data);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function keys(): array
107
    {
108
        return \array_keys($this->_object_data);
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function values(): array
115
    {
116
        return \array_values($this->_object_data);
117
    }
118
}
119