selcukmart / data-store-library-y42
该包最新版本(1.0.0)没有可用的许可信息。
1.0.0
2022-02-17 09:21 UTC
Requires
- ext-mbstring: *
- ext-pdo: *
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-17 15:49:23 UTC
README
composer require selcukmart/data-store-library-y42
它是做什么的?
它可以存储/获取/更改任何数据。
它以以下方式处理数据;
- 记录插入 & 批量插入
- 记录查询/检索
- 查询过滤器(仅等价操作),限制 & 偏移
- 更新和删除操作
代码包含单元测试。克隆后可以测试。
tests/SessionStorageTest.php
如何使用
例如,我们选择使用Session Storage;
插入示例;
$storage = $_SESSION['DataStoreLibrary']; $storage_factory = SessionFactory::create($storage); $expected = [ 'a' => [ 'b' => 'Test' ] ]; $storage_factory->insert($expected); $hash = $storage_factory->getHash();
批量插入示例;
$storage = $_SESSION['DataStoreLibrary']; $storage_factory = SessionFactory::create($storage); $batch_insert_array = [ 'Contrary to popular belief, Lorem Ipsum is not simply random text.', 'Lorem Ipsum is simply dummy text of the printing and typesetting', 'There are many variations of passages of Lorem Ipsum available', 'It is a long established fact that a reader will be distracted by the', ]; $results = $storage_factory->insertBatch($batch_insert_array);
更新示例;
$storage = $_SESSION['DataStoreLibrary']; $storage_factory = SessionFactory::create($storage); $str1 = 'Test String'; $str2 = 'Test String2'; $storage_factory->insert($str1); $old_hash = $storage_factory->getHash(); $storage_factory->update($str1,$str2);
过滤器示例;
$storage = $_SESSION['DataStoreLibrary']; $storage_factory = SessionFactory::create($storage); $str = 'Contrary to popular belief, Lorem Ipsum is not simply random text.'; $str2 = 'Lorem Ipsum is simply dummy text of the printing and typesetting'; $str3 = 'There are many variations of passages of Lorem Ipsum available'; $str4 = 'It is a long established fact that a reader will be distracted by the'; $storage_factory->insert($str); $storage_factory->insert($str2); $storage_factory->insert($str3); $storage_factory->insert($str4); $results = $storage_factory->filter($str3); echo $results[0]->getHash(); echo $results[0]->getItem();
列表示例;
$storage = $_SESSION['DataStoreLibrary']; $storage_factory = SessionFactory::create($storage); $batch_insert_array = [ 'Contrary to popular belief, Lorem Ipsum is not simply random text.', 'Lorem Ipsum is simply dummy text of the printing and typesetting', 'There are many variations of passages of Lorem Ipsum available', 'It is a long established fact that a reader will be distracted by the', ]; $results = $storage_factory->insertBatch($batch_insert_array); $result = $storage_factory->lists(limit: 2, offset: 2);
删除示例;
$storage = $_SESSION['DataStoreLibrary']; $storage_factory = SessionFactory::create($storage); $expected = 'Test String'; $storage_factory->insert($expected); $hash = $storage_factory->getHash(); $storage_factory->delete($hash, is_data: false); // OR $storage_factory->delete($expected, is_data: true);
如果您想使用其他提供者
PDOFactory创建名为"data_storage"的自己的表并使用它。如果您想设置自己的表名,请添加配置:'TABLE'=>'abc_table'
$config = [ 'SQLDRIVER'=>'mysql', 'DBNAME'=>'data_store', 'SQLHOST'=>'localhost', 'DBUSER'=>'root', 'DBPASS'=>'' ]; $storage_factory = PDOFactory::create($config);
如果您想创建自己的提供者
例如 Aws S3;
class AWSS3Factory extends AbstractDataStoreFactory implements FactoryInterface { public function __construct(private readonly array $sc_config) { } public static function create(array $config): AWSS3Factory { return new self($config); } public function delete($data, bool $is_data) { // TODO: Implement delete() method. } public function insert($data) { // TODO: Implement insert() method. } public function insertBatch(array $data) { // TODO: Implement insertBatch() method. } public function filter(string $str): array { // TODO: Implement filter() method. } public function update($old_data, $new_data) { // TODO: Implement update() method. } public function retrieve(string $hash) { // TODO: Implement retrieve() method. } public function lists(int $limit, int $offset): array { // TODO: Implement lists() method. } }
AbstractDataStoreFactory
abstract class AbstractDataStoreFactory { use ErrorMessagesWithResultTrait; protected string $hash; protected mixed $data; protected DataStorageConvert $DataStorageConvert; protected function setData($data): void { $this->data = $data; } /** * @return string * @author selcukmart * 16.02.2022 * 15:52 */ protected function getConvertedData(): string { $this->DataStorageConvert = DataStorageConvert::getInstance($this->data); $this->DataStorageConvert->execute(); $this->hash = $this->DataStorageConvert->getHash(); return $this->hash; } /** * @return string */ public function getHash(): string { return $this->hash; } /** * @param int|string $hash * @param array $results * @return array * @author selcukmart * 17.02.2022 * 10:54 */ protected function setResults(int|string $hash,mixed $item, array $results): array { $filter_object = new FilterResult(); $filter_object->setHash($hash); $filter_object->setItem($item); $results[] = $filter_object; return $results; } }