|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GrahamCampbell\Parse; |
| 4 | + |
| 5 | +use Parse\ParseStorageInterface; |
| 6 | + |
| 7 | +class ParseSessionStorage implements ParseStorageInterface |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Parse will store its values in a specific key. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + private $storageKey = 'parseData'; |
| 15 | + |
| 16 | + public function __construct() |
| 17 | + { |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Sets a key-value pair in storage. |
| 23 | + * |
| 24 | + * @param string $key The key to set |
| 25 | + * @param mixed $value The value to set |
| 26 | + * |
| 27 | + * @return null |
| 28 | + */ |
| 29 | + public function set($key, $value) |
| 30 | + { |
| 31 | + \Session::put($this->storageKey . '.' . $key, $value); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Remove a key from storage. |
| 36 | + * |
| 37 | + * @param string $key The key to remove. |
| 38 | + * |
| 39 | + * @return null |
| 40 | + */ |
| 41 | + public function remove($key) |
| 42 | + { |
| 43 | + \Session::forget($this->storageKey . '.' . $key); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the value for a key from storage. |
| 48 | + * |
| 49 | + * @param string $key The key to get the value for |
| 50 | + * |
| 51 | + * @return mixed |
| 52 | + */ |
| 53 | + public function get($key) |
| 54 | + { |
| 55 | + if (\Session::has($this->storageKey . '.' . $key)) { |
| 56 | + return \Session::get($this->storageKey . '.' . $key); |
| 57 | + } |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Clear all the values in storage. |
| 63 | + * |
| 64 | + * @return null |
| 65 | + */ |
| 66 | + public function clear() |
| 67 | + { |
| 68 | + \Session::forget($this->storageKey); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Save the data, if necessary. This would be a no-op when using the |
| 73 | + * $_SESSION implementation, but could be used for saving to file or |
| 74 | + * database as an action instead of on every set. |
| 75 | + * |
| 76 | + * @return null |
| 77 | + */ |
| 78 | + public function save() |
| 79 | + { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get all keys in storage. |
| 85 | + * |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + public function getKeys() |
| 89 | + { |
| 90 | + return array_keys(\Session::get($this->storageKey)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get all key-value pairs from storage. |
| 95 | + * |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + public function getAll() |
| 99 | + { |
| 100 | + return \Session::get($this->storageKey); |
| 101 | + } |
| 102 | +} |
0 commit comments