korriganmaster / lite-collection
PHP SQLlite collection
v1.0.0
2025-12-22 10:22 UTC
Requires
- php: >=8.0
- ext-sqlite3: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.92
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.5
This package is auto-updated.
Last update: 2026-03-06 10:58:10 UTC
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.