sevaske/support

Reusable traits and utility helpers for PHP

Fund package maintenance!
sevaske

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/sevaske/support

1.0.0 2025-10-04 19:12 UTC

This package is auto-updated.

Last update: 2025-10-04 19:14:03 UTC


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.