-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCatalog.php
293 lines (262 loc) · 7.52 KB
/
Catalog.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
namespace Xmf\Xadr;
use Xmf\Xadr\Catalog\Entry;
use Xmf\Xadr\Exceptions\InvalidCatalogEntryException;
/**
* Catalog - a domain implementation providing a catalog of data definitions
*
* @category Xmf\Xadr\Catalog
* @package Xmf
* @author Richard Griffith <[email protected]>
* @copyright 2013-2015 XOOPS Project (http://xoops.org)
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @link http://xoops.org
*/
abstract class Catalog extends Domain implements \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* Create a new entry, and add to catalog
*
* @param string $typeClass A class in the Xmf\Xadr\Catalog namespace, or
* a Fully Qualified Class Name for the new entry
* @param string $name Name of the new entry entry
*
* @return Entry the new entry
*
* @throws InvalidCatalogEntryException
*/
public function newEntry($typeClass, $name)
{
$varArgs = func_get_args();
array_shift($varArgs); // pull off $type
array_shift($varArgs); // pull off $name
// default to Xmf\Xadr\Catalog namespace
if (false === strpos($typeClass, "\\")) {
$typeClass = '\Xmf\Xadr\Catalog\\' . $typeClass;
}
if (!class_exists($typeClass)) {
throw new InvalidCatalogEntryException(sprintf('Unknown class %s', $typeClass));
}
$count = count($varArgs);
switch ($count) {
case 0:
$entry = new $typeClass($name);
break;
case 1:
$entry = new $typeClass($name, $varArgs[0]);
break;
case 2:
$entry = new $typeClass($name, $varArgs[0], $varArgs[1]);
break;
case 3:
$entry = new $typeClass($name, $varArgs[0], $varArgs[1], $varArgs[2]);
break;
default:
throw new InvalidCatalogEntryException('Failed to create catalog entry');
}
$this->addEntry($entry);
return $entry;
}
/**
* Retrieve an entry
*
* @param string $type Type of an entry
* @param string $name Name of an entry
*
* @return Entry|null
*/
public function getEntry($type, $name)
{
$entryName = $this->buildCatalogKey($type, $name);
if ($this->offsetExists($entryName)) {
return $this->offsetGet($entryName);
} else {
$default = null;
return $default;
}
}
/**
* Retrieve a set of entries for a type
*
* @param string $type Type of an entry
* @param \ArrayObject|string[] $names Name of an entry
*
* @return Entry[]
*/
public function getEntries($type, $names)
{
$return = array();
foreach ($names as $name) {
$return[$name] = $this->getEntry($type, $name);
}
return $return;
}
/**
* Set a catalog entry
*
* @param Entry $entry Catalog Entry object
* @param boolean $replace Replace any existing object of this type
*
* @return void
*
* @throws InvalidCatalogEntryException
*/
public function addEntry(Entry $entry, $replace = false)
{
$entryName = $this->buildCatalogKey($entry->getEntryType(), $entry->getEntryName());
if ($replace === false && $this->offsetExists($entryName)) {
throw new InvalidCatalogEntryException(sprintf('Duplicate entry %s', $entryName));
}
$this->offsetSet($entryName, $entry);
}
/**
* Determine if an entry exists.
*
* @param string $type Type of an entry
* @param string $name An entry name.
*
* @return boolean TRUE if the named entry exists, otherwise FALSE.
*/
public function hasEntry($type, $name)
{
$entryName = $this->buildCatalogKey($type, $name);
return $this->offsetExists($entryName);
}
/**
* Build a key for a type and name
*
* @param string $type Type of an entry
* @param string $name An entry name.
*
* @return string internal catalog key
*/
public function buildCatalogKey($type, $name)
{
$entryName = $type . '/' . $name;
return $entryName;
}
/* DomainInterface */
/**
* initialize the domain - called automatically by DomainManger
*
* concrete implementations should build the catalog here
*
* @return bool true if domain has initialized, otherwise false
*/
public function initialize()
{
return true;
}
/**
* cleanup the domain - called automatically by DomainManger
*
* concrete implementations should cleanly close the domain
*
* @return bool true if domain has closed cleanly, otherwise false
*/
public function cleanup()
{
return true;
}
/**
* @var DomainState $state catalog state
*/
protected $state = null;
/**
* Access the DomainState object for this Domain
*
* @return DomainState object
*/
public function state()
{
if ($this->state === null) {
$this->state = new Catalog\State($this->context());
}
return $this->state;
}
/* ArrayAccess interface */
/**
* @var array $cataglog where the catalog is stored
*/
protected $catalog = array();
/**
* store value
*
* @param string $offset catalog entry name
* @param Entry $value entry to store
*
* @return void
*
* @throws InvalidCatalogEntryException
*/
public function offsetSet($offset, $value)
{
if (!(empty($offset)) && ($value instanceof \Xmf\Xadr\Catalog\Entry)) {
$value->catalog($this);
$this->catalog[$offset] = $value;
} else {
throw new InvalidCatalogEntryException('Attempt to store invalid entry in the catalog');
}
}
/**
* test if there is a value at an offset
*
* @param string $offset catalog entry name
*
* @return boolean true if offset issset, false otherwise
*/
public function offsetExists($offset)
{
return isset($this->catalog[$offset]);
}
/**
* remove an entry
*
* @param string $offset catalog entry name
*
* @return void
*/
public function offsetUnset($offset)
{
unset($this->catalog[$offset]);
}
/**
* get value
*
* @param string $offset catalog entry name
*
* @return Entry|null
*/
public function offsetGet($offset)
{
return isset($this->catalog[$offset]) ? $this->catalog[$offset] : null;
}
/* IteratorAggregate interface */
/**
* get an iterator for the catalog
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->catalog);
}
/* Countable interface */
/**
* get count of catalog entries
*
* @return integer
*/
public function count()
{
return count($this->catalog);
}
}