]> git.mxchange.org Git - friendica.git/blob - tests/Util/SampleStorageBackend.php
Merge pull request #10294 from annando/http-input-data
[friendica.git] / tests / Util / SampleStorageBackend.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\Util;
23
24 use Friendica\Core\Hook;
25 use Friendica\Model\Storage\IStorage;
26
27 use Friendica\Core\L10n;
28
29 /**
30  * A backend storage example class
31  */
32 class SampleStorageBackend implements IStorage
33 {
34         const NAME = 'Sample Storage';
35
36         /** @var L10n */
37         private $l10n;
38
39         /** @var array */
40         private $options = [
41                 'filename' => [
42                         'input',    // will use a simple text input
43                         'The file to return',    // the label
44                         'sample',    // the current value
45                         'Enter the path to a file', // the help text
46                         // no extra data for 'input' type..
47                 ],
48         ];
49         /** @var array Just save the data in memory */
50         private $data = [];
51
52         /**
53          * SampleStorageBackend constructor.
54          *
55          * @param L10n $l10n The configuration of Friendica
56          *
57          * You can add here every dynamic class as dependency you like and add them to a private field
58          * Friendica automatically creates these classes and passes them as argument to the constructor
59          */
60         public function __construct(L10n $l10n)
61         {
62                 $this->l10n = $l10n;
63         }
64
65         public function get(string $reference)
66         {
67                 // we return always the same image data. Which file we load is defined by
68                 // a config key
69                 return $this->data[$reference] ?? null;
70         }
71
72         public function put(string $data, string $reference = '')
73         {
74                 if ($reference === '') {
75                         $reference = 'sample';
76                 }
77
78                 $this->data[$reference] = $data;
79
80                 return $reference;
81         }
82
83         public function delete(string $reference)
84         {
85                 if (isset($this->data[$reference])) {
86                         unset($this->data[$reference]);
87                 }
88
89                 return true;
90         }
91
92         public function getOptions()
93         {
94                 return $this->options;
95         }
96
97         public function saveOptions(array $data)
98         {
99                 $this->options = $data;
100
101                 // no errors, return empty array
102                 return $this->options;
103         }
104
105         public function __toString()
106         {
107                 return self::NAME;
108         }
109
110         public static function getName()
111         {
112                 return self::NAME;
113         }
114
115         /**
116          * This one is a hack to register this class to the hook
117          */
118         public static function registerHook()
119         {
120                 Hook::register('storage_instance', __DIR__ . '/SampleStorageBackendInstance.php', 'create_instance');
121         }
122 }
123