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

RowData   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 70
rs 10

8 Methods

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