| 1: | <?php |
| 2: | /* |
| 3: | * SimpleID |
| 4: | * |
| 5: | * Copyright (C) Kelvin Mo 2014-2026 |
| 6: | * |
| 7: | * This program is free software; you can redistribute it and/or |
| 8: | * modify it under the terms of the GNU General Public |
| 9: | * License as published by the Free Software Foundation; either |
| 10: | * version 2 of the 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 GNU |
| 15: | * General Public License for more details. |
| 16: | * |
| 17: | * You should have received a copy of the GNU General Public |
| 18: | * License along with this program; if not, write to the Free |
| 19: | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20: | * |
| 21: | */ |
| 22: | |
| 23: | namespace SimpleID\Store; |
| 24: | |
| 25: | use SimpleID\Module; |
| 26: | |
| 27: | /** |
| 28: | * An abstract class for data store modules. |
| 29: | */ |
| 30: | abstract class StoreModule extends Module { |
| 31: | /** |
| 32: | * Creates an instance of the store module. |
| 33: | * |
| 34: | * The default constructor registers the store module with the |
| 35: | * store manager by calling {@link StoreManager::addStore()}. |
| 36: | */ |
| 37: | public function __construct() { |
| 38: | parent::__construct(); |
| 39: | $store_manager = StoreManager::instance(); |
| 40: | $store_manager->addStore($this, $this->getStores()); |
| 41: | } |
| 42: | |
| 43: | /** |
| 44: | * Returns the stores that are implemented by this |
| 45: | * module. |
| 46: | * |
| 47: | * @return array<string> stores that are implemented by this |
| 48: | * module |
| 49: | */ |
| 50: | abstract public function getStores(); |
| 51: | |
| 52: | /** |
| 53: | * Finds an item with the specified type and search criteria. |
| 54: | * The search criteria is defined by the store. |
| 55: | * |
| 56: | * @param string $type the item type |
| 57: | * @param string $criteria the criteria name |
| 58: | * @param mixed $value the criteria value |
| 59: | * @return string|null the item identifier or null if no item matches the |
| 60: | * specified criteria |
| 61: | */ |
| 62: | abstract public function find($type, $criteria, $value); |
| 63: | |
| 64: | /** |
| 65: | * Reads an item. |
| 66: | * |
| 67: | * @param string $type the item type |
| 68: | * @param string $id the item identifier |
| 69: | * @return Storable|null the item or null if no item matches the |
| 70: | * specified criteria |
| 71: | */ |
| 72: | abstract public function read($type, $id); |
| 73: | |
| 74: | /** |
| 75: | * Writes an item. |
| 76: | * |
| 77: | * @param string $type the item type |
| 78: | * @param string $id the item identifier |
| 79: | * @param Storable $value the item |
| 80: | * @return void |
| 81: | */ |
| 82: | abstract public function write($type, $id, $value); |
| 83: | |
| 84: | /** |
| 85: | * Deletes an item. |
| 86: | * |
| 87: | * @param string $type the item type |
| 88: | * @param string $id the item identifier |
| 89: | * @return void |
| 90: | */ |
| 91: | abstract public function delete($type, $id); |
| 92: | |
| 93: | /** |
| 94: | * Returns whether an item exists. |
| 95: | * |
| 96: | * @param string $type the item type |
| 97: | * @param string $id the item identifier |
| 98: | * @return bool true if the item exists |
| 99: | */ |
| 100: | abstract public function exists($type, $id); |
| 101: | |
| 102: | /** |
| 103: | * Returns whether $haystack contains or equals to $needle. |
| 104: | * |
| 105: | * If $haystack is a scalar, then this function returns whether $needle equals |
| 106: | * $haystack. |
| 107: | * |
| 108: | * If $haystack is a list (i.e. an array of sequential integer keys), this |
| 109: | * function returns whether a value of $haystack contains $needle. |
| 110: | * |
| 111: | * If $haystack is an associative array, this function contains whether a key |
| 112: | * of $haystack contains $needle. |
| 113: | * |
| 114: | * @param mixed $needle the value to find |
| 115: | * @param mixed $haystack the item to search for the value |
| 116: | * @param ?string $id the id to save cache information in |
| 117: | * @param array<mixed, mixed> &$index cache |
| 118: | */ |
| 119: | protected function equalsOrContainsValue($needle, $haystack, $id, &$index): bool { |
| 120: | if (is_array($haystack)) { |
| 121: | $elements = array_keys($haystack); |
| 122: | if ($elements == range(0, count($haystack) - 1)) { |
| 123: | // $haystack is a list (i.e. sequential integer keys), |
| 124: | // so we test the values rather than the keys |
| 125: | $elements = $haystack; |
| 126: | } |
| 127: | |
| 128: | foreach ($elements as $element) { |
| 129: | if ((trim($element) != '') && ($id != null)) $index[$element] = $id; |
| 130: | if ($element == $needle) return true; |
| 131: | } |
| 132: | } else { |
| 133: | if (trim($haystack) != '') { |
| 134: | if ($id != null) $index[$haystack] = $id; |
| 135: | if ($haystack == $needle) return true; |
| 136: | } |
| 137: | } |
| 138: | return false; |
| 139: | } |
| 140: | } |
| 141: | |
| 142: | ?> |