Skip to content
This repository was archived by the owner on Feb 26, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/AdamWathan/Form/Binding/BoundData.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ protected function objectGet($target, $keyParts, $default)
return $default;
}

if (method_exists($target, 'getFormValue')) {
return $this->dataGet($this->data->getFormValue($key), $keyParts, $default);
}

return $this->dataGet($target->{$key}, $keyParts, $default);
}

Expand Down
88 changes: 88 additions & 0 deletions src/AdamWathan/Form/Binding/FormAccessible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace AdamWathan\Form\Binding;

use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Str;

trait FormAccessible
{

/**
* A cached ReflectionClass instance for $this
*
* @var ReflectionClass
*/
protected $reflection;

/**
* @param string $key
*
* @return mixed
*/
public function getFormValue($key)
{
$value = $this->getAttributeFromArray($key);

// If the attribute is listed as a date, we will convert it to a DateTime
// instance on retrieval, which makes it quite convenient to work with
// date fields without having to create a mutator for each property.
if (in_array($key, $this->getDates())) {
if (! is_null($value)) {
$value = $this->asDateTime($value);
}
}

// If the attribute has a get mutator, we will call that then return what
// it returns as the value, which is useful for transforming values on
// retrieval from the model to a form that is more useful for usage.
if ($this->hasFormMutator($key)) {
return $this->mutateFormAttribute($key, $value);
}

// No form mutator, let the model resolve this
return data_get($this, $key);
}

/**
* @param $key
*
* @return bool
*/
protected function hasFormMutator($key)
{
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC);

$mutator = collect($methods)
->first(function (ReflectionMethod $method) use ($key) {
return $method->getName() == 'form' . Str::studly($key) . 'Attribute';
});

return (bool) $mutator;
}

/**
* @param $key
* @param $value
*
* @return mixed
*/
private function mutateFormAttribute($key, $value)
{
return $this->{'form' . Str::studly($key) . 'Attribute'}($value);
}

/**
* Get a ReflectionClass Instance
* @return ReflectionClass
*/
protected function getReflection()
{
if (! $this->reflection) {
$this->reflection = new ReflectionClass($this);
}

return $this->reflection;
}
}
42 changes: 42 additions & 0 deletions tests/BindingTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use AdamWathan\Form\FormBuilder;
use AdamWathan\Form\Binding\FormAccessible;

class BindingTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -435,6 +436,16 @@ public function testExplicitCheckOnRadioTakesPrecedenceOverBinding()
$this->assertEquals($expected, $result);
}

public function testBindFormProperty()
{
$object = new FormGetter;
$this->form->bind($object);

$expected = '<input type="text" name="test" value="Form Model Accessor">';
$result = (string) $this->form->text('test');
$this->assertEquals($expected, $result);
}

private function getStubObject()
{
$obj = new stdClass;
Expand Down Expand Up @@ -465,3 +476,34 @@ public function __get($key)
return 'bar';
}
}

class FormGetter
{
use FormAccessible;

protected $attributes = [
'test' => 'testValue'
];

public function __get($key)
{
return 'Standard Model Accessor';
}

public function formTestAttribute($key)
{
return 'Form Model Accessor';
}

protected function getAttributeFromArray($key)
{
if (array_key_exists($key, $this->attributes)) {
return $this->attributes[$key];
}
}

public function getDates()
{
return ['created_at', 'updated_at'];
}
}