sevaske/support 问题修复 & 功能扩展

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

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

sevaske/support

最新稳定版本:1.0.0

Composer 安装命令:

composer require sevaske/support

包简介

Reusable traits and utility helpers for PHP

README 文档

README

Packagist PHPUnit PHPStan License: MIT

PHP Support

Lightweight and minimalistic PHP library providing a set of foundational tools for development.
Designed to be practical, extendable, and easy to integrate into projects, offering a flexible base for building more complex functionality.

Installation

composer require sevaske/support

Features

Dynamic Attributes (HasAttributes)

  • Store attributes dynamically with magic methods: __get, __set, __isset, __unset.
  • Supports array-style access via ArrayAccess.
  • Utility methods:
    • fill(array $attributes) — bulk set attributes.
    • has(string $key) — check if an attribute exists.
    • keys() — list all attribute keys.
    • replicate() — clone object with same attributes.
    • toArray(), jsonSerialize() — export attributes.

Example:

use Sevaske\Support\Traits\HasAttributes;

class User {
    use HasAttributes;
}

$user = new User();
$user->fill(['name' => 'John', 'age' => 30]);
$user->age = 31;
$user->age; // 31
$user['age']; // 31
unset($user->name);

$copy = $user->replicate();

Read-Only Attributes (HasReadOnlyAttributesContract)

  • Implements a contract to define read-only behavior.
  • readOnlyAttributes can be true (all) or an array of keys.
  • Modifying locked attributes throws LogicException.

Example:

class User implements HasReadOnlyAttributesContract {
    use HasAttributes;

    public function getReadOnlyAttributes(): bool|array 
    {
        return ['age'];
    }
}

$user = new User();
$user->fill(['name' => 'John', 'age' => 30]);
$user->name = 'Alice'; // allowed
$user->age = 32; // throws LogicException

Contextable Exceptions (HasContext + ContextableException)

  • Attach metadata to exceptions.
  • Fluent withContext() method and context() retrieval.

Example:

use Sevaske\Support\Exceptions\ContextableException;

    $ex = new ContextableException('Error', ['user_id' => 123]);
    $ex->withContext(['ip' => '127.0.0.1']);
    print_r($ex->context());

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.
Please follow PSR-12 coding standards and include tests for any new features or fixes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-04