Commit 69d8163e authored by Avris's avatar Avris

generic accessors

parent 2da22cf5
Pipeline #7084791 passed with stage
in 15 seconds
......@@ -25,6 +25,8 @@ class FormObject
}
/**
* TODO test & push
*
* @param string $key
* @param mixed $value
*/
......@@ -37,6 +39,12 @@ class FormObject
}
if (!BagHelper::isArray($value) || !$this->hasAccessor('add', $key) || !$this->hasAccessor('remove', $key)) {
if ($this->hasGenericAccessor('set')) {
$this->callGenericAccessor('set', $key, $value);
return;
}
$this->original->$key = $value;
return;
......@@ -83,6 +91,10 @@ class FormObject
return $this->callAccessor('has', $key);
}
if ($this->hasGenericAccessor('get')) {
return $this->callGenericAccessor('get', $key);
}
return isset($this->original->$key) ? $this->original->$key : null;
}
......@@ -120,6 +132,26 @@ class FormObject
return call_user_func([$this->original, $type . ucfirst($key)], $value);
}
/**
* @param string $type
* @return bool
*/
protected function hasGenericAccessor($type)
{
return method_exists($this->original, $type);
}
/**
* @param string $type
* @param string $key
* @param mixed|null $value
* @return mixed
*/
protected function callGenericAccessor($type, $key, $value = null)
{
return call_user_func([$this->original, $type], $key, $value);
}
/**
* @param object $object
* @param string $key
......
......@@ -3,6 +3,7 @@ namespace Avris\Micrus\Forms;
use App\Model\TestObjectApp;
use Avris\Micrus\Test\TestObject;
use Avris\Micrus\Test\TestObjectGen;
class FormObjectTest extends \PHPUnit_Framework_TestCase
{
......@@ -81,6 +82,16 @@ class FormObjectTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($obj->manyFoo);
}
public function testFormObjectGenericAccessors()
{
$obj = new FormObject(new TestObjectGen());
$obj->foo = 'bar';
$this->assertEquals('bar', $obj->foo);
$this->assertInstanceOf(TestObjectGen::class, $obj->getOriginal());
$this->assertEquals('Avris_Micrus_Test_TestObjectGen', $obj->getName());
}
public function testStatic()
{
$obj = new \stdClass();
......
<?php
namespace Avris\Micrus\Test;
class TestObjectGen
{
public $values = [];
public function set($key, $value)
{
$this->values[$key] = $value;
}
public function get($key)
{
return isset($this->values[$key])
? $this->values[$key]
: null;
}
public function has($key)
{
return isset($this->values[$key]);
}
public function is($key)
{
return isset($this->values[$key]) && $this->values[$key];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment