jeyroik/i-have-attributes

There is no license information available for the latest version (0.2.0) of this package.

Simple attributes implementation

0.2.0 2023-04-07 16:13 UTC

This package is auto-updated.

Last update: 2024-04-07 18:11:22 UTC


README

Simple attributes implementation.

tests codecov.io PHPStan Enabled 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33663030386639373066376439353263313533322f6d61696e7461696e6162696c697479 Latest Stable Version Total Downloads Dependents

Usage

See tests

$ composer test

$something = new class ([
    'p1' => 'v1',
    'p2' => 'v2'
]) implements IHaveAttributes {
    use THasAttributes;
};

$this->assertEquals('v1', $something->getAttribute('p1'));
$this->assertEquals('v1', $something['p1']);

$this->assertEquals('{"p1":"v1","p2":"v2"}', json_encode($something));

foreach($something as $name => $value) {
    if ($name == 'p2') {
        $this->assertEquals('v2', $value);
    }
}

$this->assertTrue(isset($something['p1']));
unset($something['p1']);
$this->assertFalse(isset($something['p1']));;

$something->__merge(['p2' => 'v2.1', 'p3' => 'v3']);

$this->assertEquals(
    ['p2' => 'v2.1', 'p3' => 'v3'],
    $something->__toArray()
);