From 65997df9bdaf00aaedb22ce5cac5c6d9eeba4070 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Mon, 21 Sep 2020 16:22:30 -0400 Subject: [PATCH] Add ability to blueprint section. (#2491) --- src/Fields/Blueprint.php | 16 ++++++++++++++ tests/Fields/BlueprintTest.php | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index 1760ff8f4d..b8a0dea5e0 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -260,6 +260,11 @@ public function hasField($field) return $this->fields()->has($field); } + public function hasSection($section) + { + return $this->sections()->has($section); + } + public function hasFieldInSection($field, $section) { if ($section = $this->sections()->get($section)) { @@ -371,6 +376,17 @@ public function removeField($handle, $section = null) } } + public function removeSection($handle) + { + if (! $this->hasSection($handle)) { + return $this; + } + + Arr::pull($this->contents['sections'], $handle); + + return $this->resetFieldsCache(); + } + public function removeFieldFromSection($handle, $section) { $fields = collect($this->contents['sections'][$section]['fields'] ?? []); diff --git a/tests/Fields/BlueprintTest.php b/tests/Fields/BlueprintTest.php index 4be450936a..ed641c2724 100644 --- a/tests/Fields/BlueprintTest.php +++ b/tests/Fields/BlueprintTest.php @@ -707,6 +707,46 @@ public function it_removes_a_field_from_a_specific_section() $this->assertTrue($blueprint->hasField('four')); } + /** @test */ + public function it_removes_a_specific_section() + { + $blueprint = (new Blueprint)->setHandle('test')->setContents($contents = [ + 'title' => 'Test', + 'sections' => [ + 'section_one' => [ + 'fields' => [ + ['handle' => 'one', 'field' => ['type' => 'text']], + ['handle' => 'two', 'field' => ['type' => 'text']], + ], + ], + 'section_two' => [ + 'fields' => [ + ['handle' => 'three', 'field' => ['type' => 'text']], + ['handle' => 'four', 'field' => ['type' => 'text']], + ], + ], + ], + ]); + + $this->assertTrue($blueprint->hasSection('section_one')); + $this->assertTrue($blueprint->hasField('one')); + $this->assertTrue($blueprint->hasField('two')); + $this->assertTrue($blueprint->hasSection('section_two')); + $this->assertTrue($blueprint->hasField('three')); + $this->assertTrue($blueprint->hasField('four')); + + $return = $blueprint->removeSection('section_two'); + + $this->assertEquals($blueprint, $return); + + $this->assertTrue($blueprint->hasSection('section_one')); + $this->assertTrue($blueprint->hasField('one')); + $this->assertTrue($blueprint->hasField('two')); + $this->assertFalse($blueprint->hasSection('section_two')); + $this->assertFalse($blueprint->hasField('three')); + $this->assertFalse($blueprint->hasField('four')); + } + /** @test */ public function it_validates_unique_handles() {