flavioheleno/dslib 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

flavioheleno/dslib

Composer 安装命令:

composer require flavioheleno/dslib

包简介

An ordinary typed data structure library

README 文档

README

DsLib is a PHP library that provides strictly typed collections (lists and sets) for primitive data types (float, integer and string).

It ensures type safety when handling arrays by enforcing strict type constraints at the collection level, reducing runtime errors caused by type mismatches.

Features

  • Strictly typed lists and sets for primitive types.
  • Prevents unintended type coercion in PHP arrays.
  • Provides type-safe filtering and mapping with defined interfaces.
  • Supports immutable and mutable collection operations.
  • Compatible with modern PHP versions.

Installation

To install DsLib via Composer:

composer require flavioheleno/dslib

Interfaces & Implementation

Type Interface List Set
Float FloatCollectionInterface FloatList FloatSet
Integer IntCollectionInterface IntList IntSet
String StringCollectionInterface StringList StringSet

Usage

An example of how to use StringList.

<?php

use DsLib\Contract\StringFilterInterface;
use DsLib\List\StringList;

// Creating a StringList
$list = new StringList('hello', 'world');
// or using fromArray
$list = StringList::fromArray(['hello', 'world']);

// Check if the list is empty
$list->isEmpty(); // false

// Check if all elements have more than 4 characters
$list->all(
  new class implements StringFilterInterface {
    public function __invoke(string $value, int $index): bool {
      return strlen($value) > 4;
    }
  }
);
// true

// Check if 'hello' is in the list
$list->has('hello'); // true

// Remove and return the last element
$list->pop(); // "world"

// Remove and return the first element
$list->shift(); // "hello"

// Check if the list is empty
$list->isEmpty(); // true

// Push elements into the list
$list
  ->push('hello')
  ->push('world');

// Convert to a PHP array
$list->toArray();
// Output: ['hello', 'world']

Filtering and Mapping

To ensure strict type safety in filtering (all, any and filter) and mapping (map) operations, DsLib provides the following interfaces:

Filtering (__invoke(<type> $value, int $index): bool)

Mapping (__invoke(<type> $value): <type>)

These interfaces enforce strict callback signatures, ensuring only compatible types are processed.

License

This library is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

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