initial commit
[namibia] / module / Utility / src / Utility / FileStore.php
1 <?php
2 namespace Utility;
3
4
5 /**
6  * File data storage functionality.
7  * @author andre.fourie
8  */
9 class FileStore
10 {
11
12
13     /**
14      * Store data to file.
15      * @param  string  $key  Storage key
16      * @param  unknown $data Data to store
17      * @return void
18      */
19     static public function existsJson($key)
20     {
21         return file_exists(getcwd() . '/data/file/' . $key . '.json');
22     }
23
24     /**
25      * Store data to file.
26      * @param  string $key  Storage key
27      * @param  array  $data Data to store
28      * @return void
29      */
30     static public function storeJson($key, $data)
31     {
32         file_put_contents(getcwd() . '/data/file/' . $key . '.json', json_encode($data));
33     }
34
35     /**
36      * Retrieve data from file.
37      * @param  string  $key Storage key
38      * @return unknown      Stored data or null
39      */
40     static public function fetchJson($key)
41     {
42         if (!file_exists(getcwd() . '/data/file/' . $key . '.json'))
43         {
44             return null;
45         }
46         $result = file_get_contents(getcwd() . '/data/file/' . $key . '.json');
47         return json_decode($result, true);
48     }
49
50     /**
51      * Delete data file.
52      * @param  string  $key Storage key
53      * @return void
54      */
55     static public function deleteJson($key)
56     {
57         if (file_exists(getcwd() . '/data/file/' . $key . '.json'))
58         {
59             unlink(getcwd() . '/data/file/' . $key . '.json');
60         }
61     }
62
63     /**
64      * Store data to file.
65      * @param  string  $key  Storage key
66      * @param  unknown $data Data to store
67      * @return void
68      */
69     static public function storeText($key, $data)
70     {
71         file_put_contents(getcwd() . '/data/file/' . $key . '.txt', $data);
72     }
73
74     /**
75      * Retrieve data from file.
76      * @param  string  $key Storage key
77      * @return unknown      Stored data or null
78      */
79     static public function fetchText($key)
80     {
81         if (!file_exists(getcwd() . '/data/file/' . $key . '.txt'))
82         {
83             return null;
84         }
85         return file_get_contents(getcwd() . '/data/file/' . $key . '.txt');
86     }
87
88     /**
89      * Delete data file.
90      * @param  string  $key Storage key
91      * @return void
92      */
93     static public function deleteText($key)
94     {
95         if (file_exists(getcwd() . '/data/file/' . $key . '.txt'))
96         {
97             unlink(getcwd() . '/data/file/' . $key . '.txt');
98         }
99     }
100
101
102
103 }