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
|
|
|
|