diff --git a/src/Blueprint.php b/src/Blueprint.php index 9bc92c4..9e1171f 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -3,9 +3,10 @@ namespace Tdwesten\StatamicBuilder; use Tdwesten\StatamicBuilder\Contracts\Blueprint as BlueprintInterface; +use Tdwesten\StatamicBuilder\Exceptions\BlueprintRenderException; use Tdwesten\StatamicBuilder\FieldTypes\Tab; -class Blueprint implements BlueprintInterface +abstract class Blueprint implements BlueprintInterface { protected $tabs; @@ -18,14 +19,13 @@ class Blueprint implements BlueprintInterface public function __construct(string $handle) { $this->handle = $handle; - $this->tabs = collect(); - - $this->register(); + $this->tabs = collect($this->registerTabs()); } public static function make(string $handle) { return new static($handle); + } public function toArray() @@ -41,24 +41,31 @@ public function toArray() public function tabsToArray() { + if ($this->tabs->isEmpty()) { + return []; + } + + $tabs = $this->tabs->filter(function ($field) { + return ! ($field instanceof Tab); + }); + + if ($tabs->isNotEmpty()) { + throw new BlueprintRenderException('Only tabs are allowed in the register function of a blueprint'); + } + return $this->tabs->mapWithKeys(function (Tab $tab) { return [$tab->getHandle() => $tab->toArray()]; })->toArray(); } - public function register() {} + abstract public function registerTabs(): array; - public function addTab($handle, $content = [], $displayName = null) + public function addTab(Tab $tab) { - $tab = new Tab($handle, $content); - - if ($displayName) { - $tab->displayName($displayName); - } - $this->tabs->push($tab); return $this; + } public function getHandle() diff --git a/src/FieldTypes/Tab.php b/src/FieldTypes/Tab.php index fa21684..07804b7 100644 --- a/src/FieldTypes/Tab.php +++ b/src/FieldTypes/Tab.php @@ -11,20 +11,20 @@ class Tab protected $displayName; - protected $content; + protected $sections; protected $instructions; - public function __construct($handle, $content = []) + public function __construct($handle, $sections = []) { $this->handle = $handle; - $this->content = collect($content); + $this->sections = collect($sections); } - public static function make($handle) + public static function make($handle, $sections = []) { - return new static($handle); + return new static($handle, $sections); } public function getHandle() @@ -48,11 +48,11 @@ public function instructions($instructions) public function sectionsToArray(): ?array { - if ($this->content->isEmpty()) { + if ($this->sections->isEmpty()) { return []; } - $fields = $this->content->filter(function ($field) { + $fields = $this->sections->filter(function ($field) { return ! ($field instanceof Section); }); @@ -60,16 +60,16 @@ public function sectionsToArray(): ?array throw new BlueprintRenderException('Only sections are allowed in tabs'); } - return $this->content->map(function (Section $section) { + return $this->sections->map(function (Section $section) { return $section->toArray(); })->toArray(); } public function fieldsToArray(): array { - $this->content = FieldParser::parseMixedFieldsToFlatCollection($this->content); + $this->sections = FieldParser::parseMixedFieldsToFlatCollection($this->sections); - return $this->content->map(function ($field) { + return $this->sections->map(function ($field) { return $field->toArray(); })->toArray(); } diff --git a/tests/Helpers/EmptyTestBlueprint.php b/tests/Helpers/EmptyTestBlueprint.php new file mode 100644 index 0000000..8bd4a8a --- /dev/null +++ b/tests/Helpers/EmptyTestBlueprint.php @@ -0,0 +1,19 @@ +displayName('Main'), + ]; + } +} diff --git a/tests/Unit/BlueprintTest.php b/tests/Unit/BlueprintTest.php index 5eaf343..ba7ca35 100644 --- a/tests/Unit/BlueprintTest.php +++ b/tests/Unit/BlueprintTest.php @@ -1,127 +1,42 @@ title('Article') - ->hidden(true); - - expect($blueprint->toArray())->toBe([ - 'title' => 'Article', - 'hide' => true, - 'tabs' => [], - ]); + expect($blueprint->toArray()['title'])->toBe( + 'Test Blueprint' + ); }); it('can be set to hidden', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('article'); - + $blueprint = TestBlueprint::make('test_blueprint'); $blueprint->hidden(true); - expect($blueprint->toArray())->toBe([ - 'title' => null, - 'hide' => true, - 'tabs' => [], - ]); + expect($blueprint->toArray()['hide'])->toBe(true); }); test('Tabs are renderd', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('school'); - $blueprint - ->title('School') - ->addTab('main', [], 'Main') - ->addTab('meta', [], 'Meta'); + $blueprint = TestBlueprint::make('test_blueprint'); $expected = [ - 'title' => 'School', - 'hide' => false, - 'tabs' => [ - 'main' => [ - 'display' => 'Main', - 'sections' => [], - ], - 'meta' => [ - 'display' => 'Meta', - 'sections' => [], - ], - ], - ]; - - expect($blueprint->toArray())->toBe($expected); -}); - -it('throws an exception when adding a field to a tab', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('school'); - $blueprint - ->title('School') - ->addTab('main', []) - ->addTab('meta', [ - Text::make('description')->displayName('Description'), - ], 'Meta'); - - $blueprint->toArray(); -})->throws(BlueprintRenderException::class, 'Only sections are allowed in tabs'); - -test('Fields in sections are renderd', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('school'); - $blueprint - ->title('School') - ->addTab('main', [ - Section::make('main', [ - Text::make('name')->displayName('Name'), - ]), - ], 'Main') - ->addTab('meta', [ - Section::make('meta', [ - Text::make('description')->displayName('Description'), - ]), - ], 'Meta'); - - $expected = [ - 'title' => 'School', + 'title' => 'Test Blueprint', 'hide' => false, 'tabs' => [ 'main' => [ 'display' => 'Main', 'sections' => [ [ - 'display' => 'main', + 'display' => 'General', 'fields' => [ [ - 'handle' => 'name', + 'handle' => 'title', 'field' => [ 'antlers' => false, - 'display' => 'Name', - 'duplicate' => true, - 'hide_display' => false, - 'input_type' => 'text', - 'instructions_position' => 'above', - 'listable' => 'hidden', - 'replicator_preview' => true, - 'type' => 'text', - 'visibility' => 'visible', - ], - ], - ], - ], - ], - ], - 'meta' => [ - 'display' => 'Meta', - 'sections' => [ - [ - 'display' => 'meta', - 'fields' => [ - [ - 'handle' => 'description', - 'field' => [ - 'antlers' => false, - 'display' => 'Description', 'duplicate' => true, 'hide_display' => false, 'input_type' => 'text', @@ -141,3 +56,27 @@ expect($blueprint->toArray())->toBe($expected); }); + +it('throws an exception when adding a field to a tab', function () { + $blueprint = TestBlueprint::make('test_blueprint'); + $blueprint + ->title('School') + ->addTab(Tab::make('main', [ + Text::make('name')->displayName('Name'), + ])); + + $blueprint->toArray(); +})->throws(BlueprintRenderException::class, 'Only sections are allowed in tabs'); + +test('you can set a title', function () { + $blueprint = TestBlueprint::make('test_blueprint'); + $blueprint->title('School'); + + expect($blueprint->toArray()['title'])->toBe('School'); +}); + +test('you can get the handle', function () { + $blueprint = TestBlueprint::make('test_blueprint'); + + expect($blueprint->getHandle())->toBe('test_blueprint'); +}); diff --git a/tests/Unit/FieldsetTest.php b/tests/Unit/FieldsetTest.php index c726d6d..6eac824 100644 --- a/tests/Unit/FieldsetTest.php +++ b/tests/Unit/FieldsetTest.php @@ -2,7 +2,9 @@ use Tdwesten\StatamicBuilder\FieldTypes\Group; use Tdwesten\StatamicBuilder\FieldTypes\Section; +use Tdwesten\StatamicBuilder\FieldTypes\Tab; use Tdwesten\StatamicBuilder\FieldTypes\Text; +use Tests\Helpers\EmptyTestBlueprint; use Tests\Helpers\TestFieldset; it('can be instantiated', function () { @@ -25,15 +27,16 @@ }); test('A fieldset can be used in a blueprint', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('school'); + $blueprint = EmptyTestBlueprint::make('school'); $blueprint ->title('School') - ->addTab('main', [ + ->addTab(Tab::make('main', [ Section::make('main', [ Text::make('name')->displayName('Name'), TestFieldset::make('test'), ]), - ], 'Main'); + ]) + ); $fields = $blueprint->toArray(); @@ -43,17 +46,17 @@ }); test('A fieldset can be used in group', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('school'); + $blueprint = EmptyTestBlueprint::make('school'); $blueprint ->title('School') - ->addTab('main', [ + ->addTab(Tab::make('main', [ Section::make('main', [ Text::make('name')->displayName('Name'), Group::make('test', [ TestFieldset::make('test'), ]), - ]), - ], 'Main'); + ])]) + ); $fields = $blueprint->toArray(); @@ -63,14 +66,15 @@ }); test('A fieldset can be used in a fieldset', function () { - $blueprint = new \Tdwesten\StatamicBuilder\Blueprint('school'); + $blueprint = EmptyTestBlueprint::make('school'); $blueprint - ->addTab('main', [ + ->addTab(Tab::make('main', [ Section::make('main', [ Text::make('name')->displayName('Name'), TestFieldset::make('test'), ]), - ]); + ]) + ); $fields = $blueprint->toArray();