compono-kit/types 问题修复 & 功能扩展

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

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

compono-kit/types

Composer 安装命令:

composer require compono-kit/types

包简介

Basic type classes wrapping scalar values to create types in applications. Implementations of compono-kit/types-interfaces

README 文档

README

Strongly typed value objects for PHP 8+

This package provides a lightweight foundation for building domain-driven, immutable, and type-safe value objects in PHP implementing compono-kit/types-interfaces. Each type implements its own interfaces and can be used directly via traits or abstract classes. It is designed for type safety, validation, and consistent methods for comparison, conversion, and manipulation.

100% code coverage without senseless LLM generated tests!

Requirements

  • PHP 8+
  • compono-kit/types-interfaces

🚀 Installation

composer require compono-kit/types

Type Overview

Type Interface Description
BooleanType RepresentsBoolean Represents a boolean value with conversion and comparison methods
IntegerType RepresentsInteger Represents an integer with mathematical and comparison operations
FloatType RepresentsFloat Represents a float with mathematical and comparison operations
StringType RepresentsString Represents a string with extensive methods for manipulation, comparison, and regex
DateType RepresentsDate Represents a date/time with comparison, addition/subtraction, and formatting
Uuid4 RepresentsString Represents a Uuid v4 value
Uuid7 RepresentsString Represents a Uuid v7 value

All types implement the base interface RepresentsType, which includes methods such as equals() and toNativeType().

Architecture

Each type can be used in three ways:

  1. Trait – Implements all interface methods.
    Advantage: You can use traits in your own classes without inheriting the abstract class.

    class MyString {
        use RepresentingString;
    }
    
    $str = new MyString("Hello World");
    echo $str->toUpperCase(); // "HELLO WORLD"
  2. Abstract Class – Uses the trait and adds an abstract isValid() method for validation.
    Advantage: Easily extendable for specific type rules.

    abstract class AbstractEmail extends AbstractString {
        public static function isValid(string $value): bool {
            return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
        }
    }
  3. Default Implementation – Standard implementation without special validation.
    Advantage: Quick usage for generic types.

    $string = new StringType("Hello World");
    echo $string->toUpperCase(); // "HELLO WORLD"

Usage Examples

String

use ComponoKit\Types\StringType;

$string = new StringType("Hello World");

echo $string->toLowerCase();   // "hello world"
echo $string->capitalizeFirst(); // "Hello World"
echo $string->contains("World");  // true

Integer

use ComponoKit\Types\IntegerType;

$int = new IntegerType(42);

$int2 = $int->add(8); // 50
var_dump($int2->isPositive()); // true

Float

use ComponoKit\Types\FloatType;

$float = new FloatType(3.14);

$float2 = $float->multiply(2); // 6.28
var_dump($float2->isGreaterThan(5.0)); // true

Boolean

use ComponoKit\Types\BooleanType;

$bool = new BooleanType(true);

var_dump($bool->isTrue()); // true
var_dump($bool->toInteger()); // 1

Date

use ComponoKit\Types\DateType;

$date = new DateType(new \DateTimeImmutable("2025-11-22"));

$tomorrow = $date->add(new \DateInterval('P1D'));
var_dump($tomorrow->isGreaterThan($date)); // true

$diff = $date->diff($tomorrow, false);
echo $diff->format('%d days'); // "1 days"

UUID4

use ComponoKit\Types\Uuid4;

$uuid = Uuid4::generate();
echo $uuid->toString(); // e.g., "550e8400-e29b-41d4-a716-446655440000"

Exceptions

All types use their own exceptions derived from RepresentsTypeException.

Summary

  • Traits: Full interface implementation, usable without abstract classes.
  • Abstract Classes: Traits + isValid() method for custom validation logic.
  • Default Types: Quick start without custom validation.
  • UUID4: Special string type with generated UUID.
  • Benefits: Type safety, consistent methods, easy extensibility and validation.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2026-06-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固