korriganmaster/lite-collection
最新稳定版本:v1.0.0
Composer 安装命令:
composer require korriganmaster/lite-collection
包简介
PHP SQLlite collection
README 文档
README
A lightweight PHP library for managing data collections with a persistent storage system using SQLite.
Features
- Lightweight Collection: Implements
Countable,ArrayAccess, andIteratorAggregateinterfaces - SQLite Storage: Data persistence in memory or on disk
- Two Operating Modes:
MODE_NORMAL: Uses auto-incremented keys (0, 1, 2...)MODE_ASSOCIATIVE: Uses a custom primary key
- Simple Interface: Manipulate data like a standard PHP array
Installation
composer require korriganmaster/lite-collection
Usage
Basic Example
use Korriganmaster\LiteCollection\LiteCollection; use Korriganmaster\LiteCollection\Storage\SqliteStorage; // Create in-memory storage $storage = new SqliteStorage(); $collection = new LiteCollection($storage); // Add items $collection[] = ['id' => 1, 'name' => 'Item 1']; $collection[] = ['id' => 2, 'name' => 'Item 2']; // Access items echo $collection[0]['name']; // "Item 1" // Count items echo count($collection); // 2 // Iterate over the collection foreach ($collection as $item) { echo $item['name']; }
Associative Mode with Custom Key
use Korriganmaster\LiteCollection\Storage\SqliteStorage; use Korriganmaster\LiteCollection\Storage\StorageInterface; // Create storage with a custom primary key $storage = new SqliteStorage( StorageInterface::MODE_ASSOCIATIVE, 'custom_id' ); $storage->insert(['custom_id' => 100, 'name' => 'Item 100']); // Access by custom key $item = $storage->findById(100);
Persistent Disk Storage
// Create disk storage $storage = new SqliteStorage( StorageInterface::MODE_NORMAL, 'id', 'path/to/database.sqlite' );
API
LiteCollection
The LiteCollection class provides a collection interface:
count(): int: Returns the number of itemsoffsetGet($offset): mixed: Retrieves an item by its indexoffsetSet($offset, $value): void: Sets or updates an itemoffsetUnset($offset): void: Removes an itemoffsetExists($offset): bool: Checks if an item existsgetIterator(): Traversable: Returns an iterator to traverse the collection
SqliteStorage
The SqliteStorage class implements StorageInterface:
insert(mixed $item): void: Inserts a new itemfindById(int $id): mixed: Finds an item by its IDexists(int $id): bool: Checks if an item existsupdate(int $id, mixed $item): void: Updates an existing itemdelete(int $id): void: Deletes an itemcount(): int: Returns the number of items- Implements
Iteratorto traverse items
StorageInterface
The StorageInterface interface defines the contract for storage systems:
Storage Modes:
StorageInterface::MODE_NORMAL: Normal mode with auto-incremented keysStorageInterface::MODE_ASSOCIATIVE: Associative mode with custom primary key
Testing
The project includes unit tests using PHPUnit:
composer test
Tests cover:
LiteCollectionTest: Collection testsSqliteStorageTest: SQLite storage tests
Development
Requirements
- PHP 8.0 or higher
- SQLite3 extension
- Composer
Development Tools
# Run tests vendor/bin/phpunit # Static analysis vendor/bin/phpstan analyse # Code style fixing vendor/bin/php-cs-fixer fix
Architecture
The project follows a simple architecture:
LiteCollection: Main collection classAbstractStorage: Abstract class for storage systemsSqliteStorage: SQLite storage implementationStorageInterface: Interface defining the storage contract
License
This project is licensed under the MIT License.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-20