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
|
|
|
* @access private |
15
|
|
|
* |
16
|
|
|
* @param string $storeType The type of store to load. |
17
|
|
|
* |
18
|
|
|
* @return object The store object. Throws an exception on error. |
19
|
|
|
*/ |
20
|
|
|
public static function load($storeType) |
21
|
|
|
{ |
22
|
|
|
self::loadAbstractStoreFile(); |
23
|
|
|
self::loadStoreTypeFile($storeType); |
24
|
|
|
self::instantiateStoreClass($storeType); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Load the abstract store file. |
30
|
|
|
* |
31
|
|
|
* @access private |
32
|
|
|
* @static |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
* |
36
|
|
|
* @throws \Exception is the abstract store file couldn't be loaded. |
37
|
|
|
*/ |
38
|
|
|
private static function loadAbstractStoreFile() |
39
|
|
|
{ |
40
|
|
|
$abstractStoreFile = dirname(__FILE__) . "/Store/AbstractStore.php"; |
41
|
|
|
|
42
|
|
|
if (!file_exists($abstractStoreFile) || !is_readable($abstractStoreFile)) { |
43
|
|
|
throw new \Exception("Could not load the abstract store file."); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
include_once $abstractStoreFile; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load the store file for the specified type. |
52
|
|
|
* |
53
|
|
|
* @access private |
54
|
|
|
* @static |
55
|
|
|
* |
56
|
|
|
* @param string $storeType The type of store to load. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
* |
60
|
|
|
* @throws \Exception is the store file couldn't be loaded. |
61
|
|
|
*/ |
62
|
|
|
private static function loadStoreTypeFile($storeType) |
63
|
|
|
{ |
64
|
|
|
$storeType = ucwords($storeType); |
65
|
|
|
$storeFile = dirname(__FILE__) . "/Store/$storeType" . "Store.php"; |
66
|
|
|
|
67
|
|
|
if (!file_exists($storeFile) || !is_readable($storeFile)) { |
68
|
|
|
throw new \Exception("Could not load the store file for $storeType."); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
include_once $storeFile; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Instantiate the store class for the specified type. |
77
|
|
|
* |
78
|
|
|
* @access private |
79
|
|
|
* @static |
80
|
|
|
* |
81
|
|
|
* @param string $storeType The type of store to load. |
82
|
|
|
* |
83
|
|
|
* @return object The store class instance. |
84
|
|
|
* |
85
|
|
|
* @throws \Exception is the store class couldn't be found. |
86
|
|
|
*/ |
87
|
|
|
private static function instantiateStoreClass($storeType) |
88
|
|
|
{ |
89
|
|
|
$storeClass = "\\JsonTable\\Store\\$storeType" . "Store"; |
90
|
|
|
|
91
|
|
|
if (!class_exists($storeClass)) { |
92
|
|
|
throw new \Exception("Could not find the store class $storeClass"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new $storeClass(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|