From b023d605d3f638ad7649c6df5478db9d2318f750 Mon Sep 17 00:00:00 2001 From: Guillermo Cava Date: Mon, 6 Nov 2023 21:17:22 -0500 Subject: [PATCH 1/3] feat: working state merging fillable traits --- src/HasParent.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/HasParent.php b/src/HasParent.php index 19f3ec6..0019116 100644 --- a/src/HasParent.php +++ b/src/HasParent.php @@ -116,7 +116,7 @@ public function getMorphClass(): string return (new $parentClass)->getMorphClass(); } - + /** * Get the class name for poly-type collections * @@ -140,4 +140,28 @@ protected function getParentClass(): string return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName(); } + + + /** + * Merge the fillable attributes for the model with Parent Class + * + * @return array + */ + public function getFillable() + { + // @todo currently failing when parent class is abstract + // $parentFillable = (new \ReflectionClass((new \ReflectionClass($this))->getParentClass())); + // if ($parentFillable->isAbstract()) { + // return ['*']; + // } + + try { + $parentClass = $this->getParentClass(); + $parentFillable = (new $parentClass)->getFillable(); + $arr = array_unique(array_merge($parentFillable, $this->fillable)); + return $arr; + } catch (\Throwable $th) { + return $this->fillable; + } + } } From 65e3a52336bfe8975764363122da699d165a667e Mon Sep 17 00:00:00 2001 From: Guillermo Cava Date: Tue, 7 Nov 2023 19:20:08 -0500 Subject: [PATCH 2/3] feat: fillable arrays are merged --- src/HasParent.php | 17 +++++--------- ...ablesMergeWithParentModelFillablesTest.php | 22 +++++++++++++++++++ tests/Models/Conference.php | 11 ++++++++++ tests/Models/Event.php | 12 ++++++++++ tests/Models/Workshop.php | 11 ++++++++++ tests/TestCase.php | 9 ++++++++ 6 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php create mode 100644 tests/Models/Conference.php create mode 100644 tests/Models/Event.php create mode 100644 tests/Models/Workshop.php diff --git a/src/HasParent.php b/src/HasParent.php index 0019116..860cf00 100644 --- a/src/HasParent.php +++ b/src/HasParent.php @@ -149,19 +149,12 @@ protected function getParentClass(): string */ public function getFillable() { - // @todo currently failing when parent class is abstract - // $parentFillable = (new \ReflectionClass((new \ReflectionClass($this))->getParentClass())); - // if ($parentFillable->isAbstract()) { - // return ['*']; - // } - - try { - $parentClass = $this->getParentClass(); - $parentFillable = (new $parentClass)->getFillable(); - $arr = array_unique(array_merge($parentFillable, $this->fillable)); - return $arr; - } catch (\Throwable $th) { + + $parentClass = $this->getParentClass(); + if ((new ReflectionClass($parentClass))->isAbstract()) { return $this->fillable; } + $parentFillable = (new $parentClass)->getFillable(); + return array_unique(array_merge($parentFillable, $this->fillable)); } } diff --git a/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php b/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php new file mode 100644 index 0000000..8eff61b --- /dev/null +++ b/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php @@ -0,0 +1,22 @@ + 'Scaling Laravel', + 'industry' => 'Technology', + 'skill_level' => 'Advanced', + ]); + $event = Event::first(); + $this->assertEquals($event->name, $workshop->name); + } +} diff --git a/tests/Models/Conference.php b/tests/Models/Conference.php new file mode 100644 index 0000000..8837843 --- /dev/null +++ b/tests/Models/Conference.php @@ -0,0 +1,11 @@ +string('type')->nullable(); $table->timestamps(); }); + + Schema::create('events', function ($table) { + $table->increments('id'); + $table->string('type')->nullable(); + $table->string('name'); + $table->string('industry')->nullable(); + $table->string('skill_level')->nullable(); + $table->timestamps(); + }); } } From 01424053bcaf2262c14d9cf002fec6d02d5f3135 Mon Sep 17 00:00:00 2001 From: Guillermo Cava Date: Thu, 9 Nov 2023 09:32:07 -0500 Subject: [PATCH 3/3] code spacing and formatting --- src/HasParent.php | 6 ++++-- ...ChildModelFillablesMergeWithParentModelFillablesTest.php | 2 ++ tests/Models/Conference.php | 1 + tests/Models/Event.php | 4 +--- tests/Models/Workshop.php | 1 + 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/HasParent.php b/src/HasParent.php index 860cf00..da5fa09 100644 --- a/src/HasParent.php +++ b/src/HasParent.php @@ -143,18 +143,20 @@ protected function getParentClass(): string /** - * Merge the fillable attributes for the model with Parent Class + * Merge the fillable attributes for the model with those of its Parent Class * * @return array */ public function getFillable() { - $parentClass = $this->getParentClass(); + if ((new ReflectionClass($parentClass))->isAbstract()) { + return $this->fillable; } $parentFillable = (new $parentClass)->getFillable(); + return array_unique(array_merge($parentFillable, $this->fillable)); } } diff --git a/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php b/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php index 8eff61b..c2a3584 100644 --- a/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php +++ b/tests/Features/ChildModelFillablesMergeWithParentModelFillablesTest.php @@ -16,7 +16,9 @@ function child_fillables_are_merged_with_parent_fillables() 'industry' => 'Technology', 'skill_level' => 'Advanced', ]); + $event = Event::first(); + $this->assertEquals($event->name, $workshop->name); } } diff --git a/tests/Models/Conference.php b/tests/Models/Conference.php index 8837843..c7d3c42 100644 --- a/tests/Models/Conference.php +++ b/tests/Models/Conference.php @@ -7,5 +7,6 @@ class Conference extends Event { use HasParent; + protected $fillable = ['industry']; } diff --git a/tests/Models/Event.php b/tests/Models/Event.php index b4ec4fd..5ad0d16 100644 --- a/tests/Models/Event.php +++ b/tests/Models/Event.php @@ -6,7 +6,5 @@ class Event extends Model { - protected $fillable = [ - 'name', 'type' - ]; + protected $fillable = ['name', 'type']; } diff --git a/tests/Models/Workshop.php b/tests/Models/Workshop.php index 041a01e..c3d0c17 100644 --- a/tests/Models/Workshop.php +++ b/tests/Models/Workshop.php @@ -7,5 +7,6 @@ class Workshop extends Event { use HasParent; + protected $fillable = ['industry', 'skill_level']; }