forked from EverexIO/JSON-RPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityProvider.php
More file actions
186 lines (170 loc) · 4.29 KB
/
EntityProvider.php
File metadata and controls
186 lines (170 loc) · 4.29 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* @package AmiLabs/JSONRPC/Core
*/
namespace AmiLabs\JSONRPC\Core;
use InvalidArgumentException;
use RuntimeException;
/**
* Entiry provider class.
*
* @author deepeloper ({@see https://github.com/deepeloper})
*/
class EntityProvider
{
/**
* @var array
*/
protected $entities = array();
/**
* @var string
*/
protected $defaultId;
/**
* Pointer to default entity
*
* @var mixed
*/
protected $defaultEntity;
/**
* Adds entity.
*
* @param string $id
* @param mixed $entity
* @param bool $asDefault Flag specifying to use entity as default,
* first added entity becames also default
* @return void
*/
public function add($id, $entity, $asDefault = FALSE)
{
$this->validateId($id, FALSE);
$this->entities[$id] = $entity;
if ($asDefault || 1 == sizeof($this->entities)) {
$this->setDefaultId($id);
}
}
/**
* Adds entity by reference.
*
* @param string $id
* @param mixed &$entity
* @param bool $asDefault Flag specifying to use entity as default,
* first added entity becames also default
* @return void
*/
public function addByReference($id, &$entity, $asDefault = FALSE)
{
$this->validateId($id, FALSE);
$this->entities[$id] = &$entity;
if ($asDefault || 1 == sizeof($this->entities)) {
$this->setDefaultId($id);
}
}
/**
* Sets default Id.
*
* @param string $id
* @param bool $byReference
* @return void
*/
public function setDefaultId($id, $byReference = FALSE)
{
$this->validateId($id, TRUE);
$this->defaultId = $id;
if ($byReference) {
$this->defaultEntity = &$this->entities[$id];
} else {
$this->defaultEntity = $this->entities[$id];
}
}
/**
* Returns default.
*
* @return mixed
* @throws RuntimeException
*/
public function getDefault()
{
if (is_null($this->defaultId)) {
throw new RuntimeException('No default entity found');
}
return $this->defaultEntity;
}
/**
* Returns default by reference.
*
* @return mixed
* @throws RuntimeException
*/
public function &getDefaultByReference()
{
if (is_null($this->defaultId)) {
throw new RuntimeException('No default entity found');
}
return $this->defaultEntity;
}
/**
* Frees entity.
*
* If default entity will be deleted last added becames default.
*
* @param string $id
* @return void
*/
public function free($id)
{
$this->validateId($id, TRUE);
unset($this->entities[$id]);
if ($id === $this->defaultId) {
$qty = sizeof($this->entities);
if ($qty) {
$ids = array_keys($this->entities);
$this->setDefaultId($ids[$qty - 1]);
} else {
$this->defaultId = NULL;
$this->defaultEntity = NULL;
}
}
}
/**
* Returns Id by entity.
*
* @param mixed
* @param string
* @return string
* @throws RuntimeException
*/
public function find($entity)
{
$id = array_search($entity, $this->entities, TRUE);
if (FALSE === $id) {
throw new RuntimeException('Passed entity not exists');
}
return $id;
}
/**
* Validates id.
*
* @param string $id
* @param bool $shouldExist
* @return void
* @throws InvalidArgumentException
* @throws RuntimeException
*/
protected function validateId($id, $shouldExist = FALSE)
{
if (!is_string($id)) {
throw new InvalidArgumentException(
'Passed entity Id is not a string'
);
}
$exists = array_key_exists($id, $this->entities);
$message =
$shouldExist
? "Entity '%s' does not exist"
: "Entity '%s' already exists";
if ($shouldExist ? !$exists : $exists) {
throw new RuntimeException(sprintf($message, $id));
}
}
}