1
|
|
|
<?php |
2
|
|
|
namespace JsonTable; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Store the data using the JSON table schema to determine the data structure. |
6
|
|
|
* |
7
|
|
|
* @package JSON table |
8
|
|
|
*/ |
9
|
|
|
class Store extends Base |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Load and instantiate the specified store. |
13
|
|
|
* |
14
|
|
|
* @param string $storeType The type of store to load. |
15
|
|
|
* |
16
|
|
|
* @return object The store object. |
17
|
|
|
*/ |
18
|
9 |
|
public static function load($storeType) |
19
|
|
|
{ |
20
|
9 |
|
self::loadAbstractStoreFile(); |
21
|
9 |
|
self::loadStoreTypeFile($storeType); |
22
|
3 |
|
return self::instantiateStoreClass($storeType); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Load the abstract store file. |
28
|
|
|
* |
29
|
|
|
* @static |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
* |
33
|
|
|
* @throws \Exception is the abstract store file couldn't be loaded. |
34
|
|
|
*/ |
35
|
9 |
|
private static function loadAbstractStoreFile() |
36
|
|
|
{ |
37
|
9 |
|
$abstractStoreFile = dirname(__FILE__) . "/Store/AbstractStore.php"; |
38
|
|
|
|
39
|
9 |
|
if (!file_exists($abstractStoreFile) || !is_readable($abstractStoreFile)) { |
40
|
|
|
throw new \Exception("Could not load the abstract store file."); |
41
|
|
|
} |
42
|
|
|
|
43
|
9 |
|
include_once $abstractStoreFile; |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Load the store file for the specified type. |
49
|
|
|
* |
50
|
|
|
* @static |
51
|
|
|
* |
52
|
|
|
* @param string $storeType The type of store to load. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* |
56
|
|
|
* @throws \Exception is the store file couldn't be loaded. |
57
|
|
|
*/ |
58
|
9 |
|
private static function loadStoreTypeFile($storeType) |
59
|
|
|
{ |
60
|
9 |
|
$storeType = ucwords($storeType); |
61
|
9 |
|
$storeFile = dirname(__FILE__) . "/Store/$storeType" . "Store.php"; |
62
|
|
|
|
63
|
9 |
|
if (!file_exists($storeFile) || !is_readable($storeFile)) { |
64
|
6 |
|
throw new \Exception("Could not load the store file for $storeType."); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
include_once $storeFile; |
68
|
3 |
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Instantiate the store class for the specified type. |
73
|
|
|
* |
74
|
|
|
* @static |
75
|
|
|
* |
76
|
|
|
* @param string $storeType The type of store to load. |
77
|
|
|
* |
78
|
|
|
* @return object The store class instance. |
79
|
|
|
* |
80
|
|
|
* @throws \Exception is the store class couldn't be found. |
81
|
|
|
*/ |
82
|
3 |
|
private static function instantiateStoreClass($storeType) |
83
|
|
|
{ |
84
|
3 |
|
$storeClass = "\\JsonTable\\Store\\$storeType" . "Store"; |
85
|
|
|
|
86
|
3 |
|
if (!class_exists($storeClass)) { |
87
|
|
|
throw new \Exception("Could not find the store class $storeClass"); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return new $storeClass(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|