Skip to content

Commit

Permalink
Refactor tab imp.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdwesten committed Feb 11, 2024
1 parent 79f4e37 commit 8aa40f0
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 130 deletions.
31 changes: 19 additions & 12 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions src/FieldTypes/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -48,28 +48,28 @@ 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);
});

if ($fields->isNotEmpty()) {
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();
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Helpers/EmptyTestBlueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Helpers;

use Tdwesten\StatamicBuilder\Blueprint;

class EmptyTestBlueprint extends Blueprint
{
public $title = 'Test Blueprint';

public $handle = 'test_blueprint';

public $hidden = false;

public function registerTabs(): array
{
return [];
}
}
28 changes: 28 additions & 0 deletions tests/Helpers/TestBlueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Helpers;

use Tdwesten\StatamicBuilder\Blueprint;
use Tdwesten\StatamicBuilder\FieldTypes\Section;
use Tdwesten\StatamicBuilder\FieldTypes\Tab;
use Tdwesten\StatamicBuilder\FieldTypes\Text;

class TestBlueprint extends Blueprint
{
public $title = 'Test Blueprint';

public $handle = 'test_blueprint';

public $hidden = false;

public function registerTabs(): array
{
return [
Tab::make('main', [
Section::make('General', [
Text::make('title'),
]),
])->displayName('Main'),
];
}
}
135 changes: 37 additions & 98 deletions tests/Unit/BlueprintTest.php
Original file line number Diff line number Diff line change
@@ -1,127 +1,42 @@
<?php

use Tdwesten\StatamicBuilder\Exceptions\BlueprintRenderException;
use Tdwesten\StatamicBuilder\FieldTypes\Section;
use Tdwesten\StatamicBuilder\FieldTypes\Tab;
use Tdwesten\StatamicBuilder\FieldTypes\Text;
use Tests\Helpers\TestBlueprint;

test('All fields are renderd', function () {
$blueprint = new \Tdwesten\StatamicBuilder\Blueprint('article');
test('Has a title', function () {
$blueprint = TestBlueprint::make('test_blueprint');

$blueprint
->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',
Expand All @@ -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');
});
Loading

0 comments on commit 8aa40f0

Please sign in to comment.