Skip to content

Lightweight and minimalistic PHP library providing a set of foundational tools for development.

License

Notifications You must be signed in to change notification settings

sevaske/support

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Lightweight and minimalistic PHP library providing a set of foundational tools for development.

Resources

License

Stars

Watchers

Forks

Languages