nayleen/attribute

Provides accessors to single-value PHP Attributes.

3.1.0 2023-03-06 22:42 UTC

This package is auto-updated.

Last update: 2024-04-26 17:47:17 UTC


README

Provides accessors to single-value PHP Attributes.

Installation

composer require nayleen/attribute

Flavors

For ease of use the library provides two identical ways of accessing attribute values:

namespace Nayleen\Attribute;

// function
get(string|object $class, string $attribute, mixed $default = null): mixed;

// public static method
AttributeValueGetter::get(string|object $class, string $attribute, mixed $default = null): mixed;

Usage

Works on both instances and class names:

use function Nayleen\Attribute\get;

#[Attribute]
class SomeAttribute
{
    public function __construct(private string $value) {}
}

#[SomeAttribute('foo')]
class MyClass {}

$value = get(MyClass::class, 'SomeAttribute'); // "foo"
$value = get(new MyClass(), 'SomeAttribute'); // "foo"

Throws a MissingAttributeException if the attribute is not set:

get(MyClass::class, 'UnknownAttribute');
// uncaught Nayleen\Attribute\Exception\MissingAttributeException

Unless you provide a default value as a third argument:

get(MyClass::class, 'UnknownAttribute', 'foo'); // "foo"
get(MyClass::class, 'UnknownAttribute', 'bar'); // "bar"
get(MyClass::class, 'UnknownAttribute', 'baz'); // "baz"

For heavy lifting or lazy evaluation, a default value can be a callable:

get(MyClass::class, 'UnknownAttribute', fn () => 'bar'); // "bar"

If the attribute is repeatable, it'll return an array of that attribute's values:

use function Nayleen\Attribute\get;

#[Attribute(Attribute::IS_REPEATABLE)]
final class RepeatableAttribute
{
    public function __construct(private string $value) {}
}

#[RepeatableAttribute('foo')]
#[RepeatableAttribute('bar')]
class MyClass {}

$value = get(MyClass::class, 'RepeatableAttribute'); // ["foo", "bar"]