-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDataItem.php
More file actions
115 lines (101 loc) · 2.27 KB
/
DataItem.php
File metadata and controls
115 lines (101 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Mapbender\DataSourceBundle\Entity;
/**
* @author Andriy Oblivantsev <eslider@gmail.com>
*/
class DataItem implements \ArrayAccess
{
/** @var mixed[] */
protected $attributes = array();
/** @var string */
protected $uniqueIdField;
/**
* @param mixed[] $attributes array
* @param string $uniqueIdField ID field name
* @internal
*/
public function __construct(array $attributes = array(), $uniqueIdField = 'id')
{
$this->uniqueIdField = $uniqueIdField;
if (!array_key_exists($this->uniqueIdField, $attributes)) {
// ensure getId works
$attributes[$this->uniqueIdField] = null;
}
$this->setAttributes($attributes);
}
/**
* @return array
*/
public function toArray()
{
return $this->attributes;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->attributes[$this->uniqueIdField] = $id;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->attributes[$this->uniqueIdField];
}
/**
* Get attributes
*
* @return mixed[]
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param string $name
* @return mixed
*/
public function getAttribute($name)
{
return $this->attributes[$name];
}
/**
* ADD attributes
*
* @param mixed $attributes
*/
public function setAttributes($attributes)
{
$this->attributes = array_merge($this->attributes, $attributes);
}
/**
* Set attribute
*
* @param string $key
* @param mixed $value
*/
public function setAttribute($key, $value)
{
$this->attributes[ $key ] = $value;
}
public function offsetExists($offset): bool
{
return \array_key_exists($offset, $this->attributes);
}
public function offsetGet(mixed $offset): mixed
{
return $this->attributes[$offset];
}
public function offsetSet($offset, $value): void
{
$this->setAttribute($offset, $value);
}
public function offsetUnset($offset): void
{
unset($this->attributes[$offset]);
}
}