Skip to content

Commit

Permalink
Implement default attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Feb 26, 2021
1 parent e621791 commit bf6d31b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Tatter\Handlers\Interfaces\HandlerInterface;

class BaseHandler implements HandlerInterface
abstract class BaseHandler implements HandlerInterface
{
use \Tatter\Handlers\Traits\HandlerTrait;

Expand All @@ -11,5 +11,16 @@ class BaseHandler implements HandlerInterface
*
* @var array
*/
protected $attributes;
protected $attributes = [];

/**
* Checks for and merges default attributes.
*/
public function __construct()
{
if (property_exists($this, 'defaults') && is_array($this->defaults))
{
$this->attributes = array_merge($this->defaults, $this->attributes);
}
}
}
1 change: 0 additions & 1 deletion src/Interfaces/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* Note:
* This interface will always be compatible with CodeIgniter\Entity.
*/

interface HandlerInterface
{
/**
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/BaseHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Tatter\Handlers\BaseHandler;
use Tests\Support\HandlerTestCase;

class BaseHandlerTest extends HandlerTestCase
{
public function testUsesDefaults()
{
$handler = new class extends BaseHandler {
protected $defaults = [
'foo' => 'bar',
];
};

$this->assertEquals('bar', $handler->foo);
}
}

0 comments on commit bf6d31b

Please sign in to comment.