定制 korriganmaster/lite-collection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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, and IteratorAggregate interfaces
  • 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 items
  • offsetGet($offset): mixed: Retrieves an item by its index
  • offsetSet($offset, $value): void: Sets or updates an item
  • offsetUnset($offset): void: Removes an item
  • offsetExists($offset): bool: Checks if an item exists
  • getIterator(): Traversable: Returns an iterator to traverse the collection

SqliteStorage

The SqliteStorage class implements StorageInterface:

  • insert(mixed $item): void: Inserts a new item
  • findById(int $id): mixed: Finds an item by its ID
  • exists(int $id): bool: Checks if an item exists
  • update(int $id, mixed $item): void: Updates an existing item
  • delete(int $id): void: Deletes an item
  • count(): int: Returns the number of items
  • Implements Iterator to traverse items

StorageInterface

The StorageInterface interface defines the contract for storage systems:

Storage Modes:

  • StorageInterface::MODE_NORMAL: Normal mode with auto-incremented keys
  • StorageInterface::MODE_ASSOCIATIVE: Associative mode with custom primary key

Testing

The project includes unit tests using PHPUnit:

composer test

Tests cover:

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:

License

This project is licensed under the MIT License.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-20