Completed
Pull Request — master (#42)
by Daniel
01:54
created

RowData::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
final class RowData implements \ArrayAccess
8
{
9
    /**
10
     * @var mixed
11
     */
12
    private $data;
13
14
    public function __construct($data)
15
    {
16
        if (
17
            false === is_object($data) &&
18
            false === is_array($data) &&
19
            false === $data instanceof \ArrayAccess
20
        ) {
21
            throw new \InvalidArgumentException(sprintf(
22
                'Row data must be either an object, an array, or it must implement ArrayAccess. Got "%s"',
23
                gettype($data)
24
            ));
25
        }
26
27
        $this->data = $data;
28
    }
29
30
    public function getData()
31
    {
32
        return $this->data;
33
    }
34
35
    public function isArrayLike()
36
    {
37
        return is_array($this->data) || $this->data instanceof \ArrayAccess;
38
    }
39
40
    public function __get($key)
41
    {
42
        if (false === $this->isArrayLike()) {
43
            throw new \InvalidArgumentException(sprintf(
44
                'Magic __get method can only be used on array-like data.'
45
            ));
46
        }
47
48
        if (false === array_key_exists($key, $this->data)) {
49
            throw new \InvalidArgumentException(sprintf(
50
                'Unknown property "%s", known properties: "%s"', $key, implode('", "', array_keys($this->data))
51
            ));
52
        }
53
54
        return $this->data[$key];
55
    }
56
57
    public function offsetGet($key)
58
    {
59
        return $this->data[$key];
60
    }
61
62
    public function offsetExists($key)
63
    {
64
        return isset($this->data[$key]);
65
    }
66
67
    public function offsetSet($key, $value)
68
    {
69
        throw new \BadMethodCallException('Row data  is immutable');
70
    }
71
72
    public function offsetUnset($key)
73
    {
74
        throw new \BadMethodCallException('Row data  is immutable');
75
    }
76
}
77