1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xmf\Key;
13:
14: /**
15: * Xmf\Key\ArrayStorage
16: *
17: * A non-persisting key storage, mainly for testing
18: *
19: * @category Xmf\Key\StorageInterface
20: * @package Xmf
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2018 XOOPS Project (https://xoops.org)
23: * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24: * @link https://xoops.org
25: */
26: class ArrayStorage extends \ArrayObject implements StorageInterface
27: {
28: /**
29: * Save key data by name
30: *
31: * @param string $name key name
32: * @param string $data key data, serialized to string if required
33: *
34: * @return boolean true if key saved, otherwise false
35: */
36: public function save($name, $data)
37: {
38: $this->offsetSet($name, $data);
39: return true;
40: }
41:
42: /**
43: * Fetch key data by name
44: *
45: * @param string $name key name
46: *
47: * @return string|false key data (possibly serialized) or false on error
48: */
49: public function fetch($name)
50: {
51: if ($this->offsetExists($name)) {
52: return $this->offsetGet($name);
53: }
54: return false;
55: }
56:
57: /**
58: * Fetch key data by name
59: *
60: * @param string $name key name
61: *
62: * @return boolean true if key exists, otherwise false
63: */
64: public function exists($name)
65: {
66: return $this->offsetExists($name);
67: }
68:
69: /**
70: * Delete a key
71: *
72: * @param string $name key name
73: *
74: * @return boolean true if key deleted, otherwise false
75: */
76: public function delete($name)
77: {
78: if ($this->offsetExists($name)) {
79: $this->offsetUnset($name);
80: return true;
81: }
82: return false;
83: }
84: }
85: