jeyroik/i-have-attributes

此包最新版本(0.2.0)没有可用的许可证信息。

简单属性实现

0.2.0 2023-04-07 16:13 UTC

This package is auto-updated.

Last update: 2024-09-07 19:10:24 UTC


README

简单属性实现。

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

使用方法

查看测试

$ 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()
);