From 5b7668573de943258e1059c83b0886b69b576508 Mon Sep 17 00:00:00 2001 From: Adam Engebretson Date: Tue, 1 Dec 2015 03:41:47 -0600 Subject: [PATCH] Adding FormAccessible Trait Signed-off-by: Adam Engebretson --- composer.json | 1 + phpunit.php | 25 ++++++++ src/Eloquent/FormAccessible.php | 87 +++++++++++++++++++++++++ src/FormBuilder.php | 4 ++ tests/FormAccessibleTest.php | 108 ++++++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+) create mode 100644 src/Eloquent/FormAccessible.php create mode 100644 tests/FormAccessibleTest.php diff --git a/composer.json b/composer.json index b7047a9..8f3d552 100755 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "illuminate/view": "5.2.*" }, "require-dev": { + "illuminate/database": "5.2.*", "mockery/mockery": "~0.9", "phpunit/phpunit": "~4.0" }, diff --git a/phpunit.php b/phpunit.php index 25c90c8..18f6fa2 100755 --- a/phpunit.php +++ b/phpunit.php @@ -1,5 +1,9 @@ addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', +]); +$capsule->setEventDispatcher(new Dispatcher); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); + +$capsule->schema()->dropIfExists('models'); + +$capsule->schema()->create('models', function(Blueprint $table) { + $table->increments('id'); + $table->string('string'); + $table->string('email'); + $table->timestamps(); +}); \ No newline at end of file diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php new file mode 100644 index 0000000..91a24d3 --- /dev/null +++ b/src/Eloquent/FormAccessible.php @@ -0,0 +1,87 @@ +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); + } + + return $value; + } + + /** + * @param $key + * + * @return bool + */ + protected function hasFormMutator($key) + { + $methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC); + + $mutator = collect($methods) + ->first(function ($index, 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; + } +} \ No newline at end of file diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 830830e..6712d71 100755 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1088,6 +1088,10 @@ class FormBuilder */ protected function getModelValueAttribute($name) { + if (method_exists($this->model, 'getFormValue')) { + return $this->model->getFormValue($name); + } + return data_get($this->model, $this->transformKey($name)); } diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php new file mode 100644 index 0000000..a67f0fa --- /dev/null +++ b/tests/FormAccessibleTest.php @@ -0,0 +1,108 @@ +truncate(); + Model::unguard(); + + $this->modelData = [ + 'string' => 'abcdefghijklmnop', + 'email' => 'tj@tjshafer.com', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]; + + $this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); + $this->viewFactory = m::mock(Factory::class); + $this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory); + $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc'); + } + + public function testItCanMutateValuesForForms() + { + $model = new ModelThatUsesForms($this->modelData); + $this->formBuilder->setModel($model); + + $this->assertEquals($model->getFormValue('string'), 'ponmlkjihgfedcba'); + $this->assertEquals($model->getFormValue('created_at'), time()); + } + + public function testItCanStillMutateValuesForViews() + { + $model = new ModelThatUsesForms($this->modelData); + $this->formBuilder->setModel($model); + + $this->assertEquals($model->string, 'ABCDEFGHIJKLMNOP'); + $this->assertEquals($model->created_at, Carbon::now()->diffForHumans()); + } + + public function testItDoesntRequireTheUseOfThisFeature() + { + $model = new ModelThatDoesntUseForms($this->modelData); + $this->formBuilder->setModel($model); + + $this->assertEquals($model->string, 'ABCDEFGHIJKLMNOP'); + $this->assertEquals($model->created_at, Carbon::now()->diffForHumans()); + } + +} + +class ModelThatUsesForms extends Model +{ + + use FormAccessible; + + protected $table = 'models'; + + public function formStringAttribute($value) + { + return strrev($value); + } + + public function getStringAttribute($value) + { + return strtoupper($value); + } + + public function formCreatedAtAttribute(Carbon $value) + { + return $value->timestamp; + } + + public function getCreatedAtAttribute($value) + { + return '1 second ago'; + } + +} + +class ModelThatDoesntUseForms extends Model { + + protected $table = 'models'; + + public function getStringAttribute($value) + { + return strtoupper($value); + } + + public function getCreatedAtAttribute($value) + { + return '1 second ago'; + } + +} \ No newline at end of file