Skip to content
Snippets Groups Projects
Commit 3ba26154 authored by Hoop, Bert Jan de's avatar Hoop, Bert Jan de
Browse files

L01ALG-1072 add AttributesTrait

parent 96387230
No related branches found
No related tags found
No related merge requests found
<?php
namespace Library;
/**
* Trait that adds support for getting/setting attributes/properties.
* Useful for settings from a configuration file.
* And no need to create getter/setter for each attribute.
*/
trait AttributesTrait
{
/**
* Attributes of instance
*
* @var array with key, value pairs
*/
protected $attributes = [];
/**
* Add attributes to existing attributes
*
* @param array|\ArrayAccess $attributes
* @return $this
*/
public function addAttributes($attributes)
{
// array_merge does not work on \ArrayAccess objects (settings in Slim framework)
foreach ($attributes as $key => $value) {
$this->attributes[$key] = $value;
}
return $this;
}
/**
* Set attributes, replaces all existing attributes
*
* @param array|\ArrayAccess $attributes new attributes
* @return $this
*/
public function setAttributes($attributes)
{
$this->attributes = [];
return $this->addAttributes($attributes);
}
/**
* Return array with all attributes
*
* @return array key/value array with all attributes
*/
public function getAttributes(): array
{
return $this->attributes;
}
/**
* Set a key/value pair to the attributes
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function setAttribute(string $key, $value)
{
$this->attributes[$key] = $value;
return $this;
}
/**
* Return the value of the attribute with given key
* if the attribute is not set then return the given default value
*
* @param string $key
* @param mixed $default
* @return mixed value of attribute with given key of the default value
*/
public function getAttribute(string $key, $default = '')
{
return key_exists($key, $this->attributes) ? $this->attributes[$key] : $default;
}
}
<?php
// phpcs:disable Generic.Files.LineLength
namespace Tests\App;
class AttributesTraitTest extends \PHPUnit\Framework\TestCase
{
private $instance;
protected function setUp(): void
{
$this->instance = new class {
use \Library\AttributesTrait;
};
}
public function testDefaultAttributes()
{
$this->assertEquals([], $this->instance->getAttributes(), 'default attributes');
}
public function testAddAttributes()
{
$attribs = [
"a" => 1,
];
$this->instance->addAttributes($attribs);
$this->assertEquals($attribs, $this->instance->getAttributes(), 'first set attributes');
$attribs = [
"b" => 2,
"c" => 3,
];
$this->instance->addAttributes($attribs);
$expect = [
"a" => 1,
"b" => 2,
"c" => 3,
];
$this->assertEquals($expect, $this->instance->getAttributes(), 'second set attributes');
}
public function testSetAttributes()
{
$attribs = [
"a" => 1,
];
$this->instance->setAttributes($attribs);
$this->assertEquals($attribs, $this->instance->getAttributes(), 'first set attributes');
$attribs = [
"b" => 2,
"c" => 3,
];
$this->instance->setAttributes($attribs);
$this->assertEquals($attribs, $this->instance->getAttributes(), 'second set attributes');
}
public function testSetAttribute()
{
$this->instance->setAttribute("a", 1);
$expect = [
"a" => 1,
];
$this->assertEquals($expect, $this->instance->getAttributes(), 'attribute "a" set');
$this->instance->setAttribute("b", 2);
$expect = [
"a" => 1,
"b" => 2,
];
$this->assertEquals($expect, $this->instance->getAttributes(), 'attribute "b" set');
$this->instance->setAttribute("a", 3);
$expect = [
"a" => 3,
"b" => 2,
];
$this->assertEquals($expect, $this->instance->getAttributes(), 'attribute "a" changed');
}
public function testGetAttribute()
{
$attribs = [
"a" => 1,
"b" => 2,
"c" => 3,
];
$this->instance->addAttributes($attribs);
$this->assertEquals(1, $this->instance->getAttribute("a"), 'get attribute "a" without default');
$this->assertEquals(1, $this->instance->getAttribute("a", "x"), 'get attribute "a" with default');
$this->assertEquals(2, $this->instance->getAttribute("b"), 'get attribute "b" without default');
$this->assertEquals(3, $this->instance->getAttribute("c"), 'get attribute "c" without default');
$this->assertEquals(null, $this->instance->getAttribute("d"), 'get unknown attribute "d" without default');
$this->assertEquals("x", $this->instance->getAttribute("d", "x"), 'get unknown attribute "d" with default');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment