Skip to content

Commit

Permalink
feature #142 Add FieldSetWithView interface (sstok)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.0-dev branch.

Discussion
----------

This is the first step for introducing the new Condition Builder UI library.
A FieldSetView holds the view configuration for the builder UI, and can be easily customized.

Note that Condition Builder UI will be kept in a separate package, as this is build using a JavaScript library.

Commits
-------

7d5d2e2 Add FieldSetWithView interface
  • Loading branch information
sstok committed Jan 28, 2017
2 parents d779afd + 7d5d2e2 commit 464adb2
Show file tree
Hide file tree
Showing 23 changed files with 276 additions and 90 deletions.
4 changes: 3 additions & 1 deletion src/FieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ public function getOption(string $name, $default = null);
/**
* Returns a new SearchFieldView for the SearchField.
*
* @param FieldSetView $fieldSet
*
* @return SearchFieldView
*/
public function createView(): SearchFieldView;
public function createView(FieldSetView $fieldSet): SearchFieldView;

/**
* Sets the value for an attribute.
Expand Down
34 changes: 34 additions & 0 deletions src/FieldSetView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\Search;

/**
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*/
class FieldSetView
{
/**
* The variables assigned to this view.
*
* @var array
*/
public $vars = [
'attr' => [],
];

/**
* @var SearchFieldView[]
*/
public $fields;
}
27 changes: 27 additions & 0 deletions src/FieldSetWithView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\Search;

/**
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*/
interface FieldSetWithView extends FieldSet
{
/**
* Create a new FieldSetView instance of the FieldSet.
*
* @return FieldSetView
*/
public function createView(): FieldSetView;
}
31 changes: 28 additions & 3 deletions src/GenericFieldSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*/
final class GenericFieldSet implements FieldSet
final class GenericFieldSet implements FieldSetWithView
{
private $fields = [];
private $name;

/**
* @var callable
*/
private $viewBuilder;

/**
* Constructor.
*
* @param FieldConfig[] $fields
* @param string|null $name FQCN of the FieldSet configurator
* @param string|null $name FQCN of the FieldSet configurator
* @param callable $viewBuilder A callable to finalize the FieldSetView
*/
public function __construct(array $fields, string $name = null)
public function __construct(array $fields, string $name = null, callable $viewBuilder = null)
{
$this->fields = $fields;
$this->name = $name;
$this->viewBuilder = $viewBuilder;
}

/**
Expand Down Expand Up @@ -72,4 +79,22 @@ public function has(string $name): bool
{
return isset($this->fields[$name]);
}

/**
* {@inheritdoc}
*/
public function createView(): FieldSetView
{
$view = new FieldSetView();

foreach ($this->fields as $name => $field) {
$view->fields[$name] = $field->createView($view);
}

if (null !== $this->viewBuilder) {
call_user_func($this->viewBuilder, $view);
}

return $view;
}
}
10 changes: 5 additions & 5 deletions src/GenericResolvedFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public function buildType(FieldConfig $config, array $options)
/**
* {@inheritdoc}
*/
public function createFieldView(FieldConfig $config)
public function createFieldView(FieldConfig $config, FieldSetView $view)
{
$view = $this->newFieldView($config);
$view = $this->newView($view);
$view->vars = array_merge($view->vars, [
'name' => $config->getName(),
'accept_ranges' => $config->supportValueType(Range::class),
Expand Down Expand Up @@ -190,12 +190,12 @@ protected function newField($name, array $options): FieldConfig
*
* Override this method if you want to customize the view class.
*
* @param FieldConfig $config The search field
* @param FieldSetView $view
*
* @return SearchFieldView The new view instance
*/
protected function newFieldView(FieldConfig $config): SearchFieldView
protected function newView(FieldSetView $view): SearchFieldView
{
return new SearchFieldView($config);
return new SearchFieldView($view);
}
}
5 changes: 3 additions & 2 deletions src/ResolvedFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ public function buildType(FieldConfig $config, array $options);
/**
* Creates a new SearchFieldView for a field of this type.
*
* @param FieldConfig $config
* @param FieldConfig $config
* @param FieldSetView $view
*
* @return SearchFieldView
*/
public function createFieldView(FieldConfig $config);
public function createFieldView(FieldConfig $config, FieldSetView $view);

/**
* Configures a SearchFieldView for the type hierarchy.
Expand Down
4 changes: 2 additions & 2 deletions src/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ public function getOption(string $name, $default = null)
/**
* {@inheritdoc}
*/
public function createView(): SearchFieldView
public function createView(FieldSetView $fieldSet): SearchFieldView
{
if (!$this->locked) {
throw new BadMethodCallException(
'Unable to create SearchFieldView when configuration is not locked.'
);
}

$view = new SearchFieldView();
$view = new SearchFieldView($fieldSet);

$this->type->buildFieldView($view, $this, $this->options);

Expand Down
15 changes: 15 additions & 0 deletions src/SearchFieldView.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ class SearchFieldView
public $vars = [
'attr' => [],
];

/**
* @var FieldSetView
*/
public $fieldSet;

/**
* Constructor.
*
* @param FieldSetView $fieldSet
*/
public function __construct(FieldSetView $fieldSet)
{
$this->fieldSet = $fieldSet;
}
}
15 changes: 8 additions & 7 deletions tests/Extension/Core/Type/ChoiceTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rollerworks\Component\Search\Extension\Core\ChoiceList\View\ChoiceGroupView;
use Rollerworks\Component\Search\Extension\Core\ChoiceList\View\ChoiceView;
use Rollerworks\Component\Search\Extension\Core\Type\ChoiceType;
use Rollerworks\Component\Search\FieldSetView;
use Rollerworks\Component\Search\Test\FieldTransformationAssertion;
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
Expand Down Expand Up @@ -115,7 +116,7 @@ public function testChoiceListWithScalarValues()
->andReverseTransformsTo('n/a', '');

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

$this->assertSame('1', $view->vars['choices'][0]->value);
$this->assertSame('0', $view->vars['choices'][1]->value);
Expand Down Expand Up @@ -145,7 +146,7 @@ public function testChoiceListWithScalarValuesAndNormFormatLabel()
->andReverseTransformsTo('n/a', 'n/a');

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

$this->assertSame('1', $view->vars['choices'][0]->value);
$this->assertSame('0', $view->vars['choices'][1]->value);
Expand All @@ -160,7 +161,7 @@ public function testChoiceListWithScalarValuesAndFalseAsPreferredChoice()
]);

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

$this->assertEquals('No', $view->vars['preferred_choices'][1]->label, 'False value should be preferred.');
}
Expand Down Expand Up @@ -194,7 +195,7 @@ public function testValueViewFormatIsValue()
->andReverseTransformsTo($this->objectChoices[2]->id);

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

// Ensure widget views still have the label.
$this->assertEquals([
Expand Down Expand Up @@ -226,7 +227,7 @@ public function testPassChoicesToView()
]);

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

$this->assertEquals([
new ChoiceView('a', 'a', 'A'),
Expand All @@ -245,7 +246,7 @@ public function testPassPreferredChoicesToView()
]);

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

$this->assertEquals([
0 => new ChoiceView('a', 'a', 'A'),
Expand All @@ -266,7 +267,7 @@ public function testPassHierarchicalChoicesToView()
]);

$field->finalizeConfig();
$view = $field->createView();
$view = $field->createView(new FieldSetView());

$this->assertEquals([
'Symfony' => new ChoiceGroupView('Symfony', [
Expand Down
5 changes: 3 additions & 2 deletions tests/Extension/Core/Type/CountryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Rollerworks\Component\Search\Extension\Core\ChoiceList\View\ChoiceView;
use Rollerworks\Component\Search\Extension\Core\Type\CountryType;
use Rollerworks\Component\Search\FieldSetView;
use Rollerworks\Component\Search\Test\FieldTransformationAssertion;
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase;
use Symfony\Component\Intl\Util\IntlTestHelper;
Expand All @@ -38,7 +39,7 @@ public function testCountriesAreSelectable()
->successfullyTransformsTo('DE')
->andReverseTransformsTo('Germany', 'DE');

$view = $field->createView();
$view = $field->createView(new FieldSetView());
$choices = $view->vars['choices'];

// Don't check objects for identity
Expand All @@ -54,7 +55,7 @@ public function testUnknownCountryIsNotIncluded()
$field = $field = $this->getFactory()->createField('choice', CountryType::class);
$field->finalizeConfig();

$view = $field->createView();
$view = $field->createView(new FieldSetView());

$choices = $view->vars['choices'];

Expand Down
3 changes: 2 additions & 1 deletion tests/Extension/Core/Type/CurrencyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Rollerworks\Component\Search\Extension\Core\ChoiceList\View\ChoiceView;
use Rollerworks\Component\Search\Extension\Core\Type\CurrencyType;
use Rollerworks\Component\Search\FieldSetView;
use Rollerworks\Component\Search\Test\FieldTransformationAssertion;
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase;
use Symfony\Component\Intl\Util\IntlTestHelper;
Expand All @@ -39,7 +40,7 @@ public function testCurrenciesAreSelectable()
->successfullyTransformsTo('EUR')
->andReverseTransformsTo('EUR');

$view = $field->createView();
$view = $field->createView(new FieldSetView());

$choices = $view->vars['choices'];

Expand Down
3 changes: 2 additions & 1 deletion tests/Extension/Core/Type/DateTimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Tests\Extension\Core\Type;

use Rollerworks\Component\Search\Extension\Core\Type\DateTimeType;
use Rollerworks\Component\Search\FieldSetView;
use Rollerworks\Component\Search\Test\FieldTransformationAssertion;
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase;
use Symfony\Component\Intl\Util\IntlTestHelper;
Expand Down Expand Up @@ -90,7 +91,7 @@ public function testViewIsConfiguredProperly()
]);

$field->finalizeConfig();
$fieldView = $field->createView();
$fieldView = $field->createView(new FieldSetView());

self::assertArrayHasKey('timezone', $fieldView->vars);
self::assertArrayHasKey('pattern', $fieldView->vars);
Expand Down
5 changes: 3 additions & 2 deletions tests/Extension/Core/Type/DateTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Tests\Extension\Core\Type;

use Rollerworks\Component\Search\Extension\Core\Type\DateType;
use Rollerworks\Component\Search\FieldSetView;
use Rollerworks\Component\Search\Test\FieldTransformationAssertion;
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase;
use Symfony\Component\Intl\Util\IntlTestHelper;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function testViewIsConfiguredProperlyWithoutExplicitPattern()
]);

$field->finalizeConfig();
$fieldView = $field->createView();
$fieldView = $field->createView(new FieldSetView());

self::assertArrayHasKey('timezone', $fieldView->vars);
self::assertArrayHasKey('pattern', $fieldView->vars);
Expand All @@ -72,7 +73,7 @@ public function testViewIsConfiguredProperly()
]);

$field->finalizeConfig();
$fieldView = $field->createView();
$fieldView = $field->createView(new FieldSetView());

self::assertArrayHasKey('timezone', $fieldView->vars);
self::assertArrayHasKey('pattern', $fieldView->vars);
Expand Down
3 changes: 2 additions & 1 deletion tests/Extension/Core/Type/IntegerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\Search\Tests\Extension\Core\Type;

use Rollerworks\Component\Search\Extension\Core\Type\IntegerType;
use Rollerworks\Component\Search\FieldSetView;
use Rollerworks\Component\Search\Test\FieldTransformationAssertion;
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase;
use Symfony\Component\Intl\Util\IntlTestHelper;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function testViewIsConfiguredProperly()
);

$field->finalizeConfig();
$fieldView = $field->createView();
$fieldView = $field->createView(new FieldSetView());

self::assertArrayHasKey('grouping', $fieldView->vars);
self::assertArrayNotHasKey('precision', $fieldView->vars);
Expand Down

0 comments on commit 464adb2

Please sign in to comment.