Skip to content

Commit

Permalink
Merge branch '1.1' into 1.2
Browse files Browse the repository at this point in the history
* 1.1:
  Apply coding standard fixes
  Update "Configuring taxation" docs
  [GridBundle] Do not put unnecessary "andWhere" in ExpressionBuilder
  [Grid] Fix getting enabled grid items
  [ThemeBundle] Replace "symfony/symfony" dependency with specific Symfony packages
  Keep the existing pagination when changing sorting on product list page
  Fix API validation tests
  [ShippingBundle] Add validation for ShippingMethod calculator
  Narrow down selectors to prevent unexpected bugs
  [CoreBundle] Make sure promotion action/rule amount is an integer
  set gender `u` as default value - resolves Sylius#8493
  [Order] Fixed sylius:remove-expired-carts help
  Make ArrayGridProvider more performant & suitable for PHP-PM
  [Documentation] Fix API example for creating a taxon
  • Loading branch information
pamil committed Oct 3, 2018
2 parents 8d339e2 + d707c22 commit 056f30a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 134 deletions.
50 changes: 21 additions & 29 deletions Definition/Grid.php
Expand Up @@ -146,19 +146,21 @@ public function setLimits(array $limits): void
}

/**
* @return array
* @return array|Field[]
*/
public function getFields(): array
{
return $this->fields;
}

/**
* @return array
* @return array|Field[]
*/
public function getEnabledFields(): array
{
return $this->getEnabledItems($this->getFields());
return array_filter($this->getFields(), function (Field $field): bool {
return $field->isEnabled();
});
}

/**
Expand Down Expand Up @@ -220,19 +222,22 @@ public function hasField(string $name): bool
}

/**
* @return array
* @return array|ActionGroup[]
*/
public function getActionGroups(): array
{
return $this->actionGroups;
}

/**
* @return array
* @return array|ActionGroup[]
*/
public function getEnabledActionGroups(): array
{
return $this->getEnabledItems($this->getActionGroups());
return array_filter($this->getActionGroups(), function (ActionGroup $actionGroup): bool {
// TODO: There's no `isEnabled` method on ActionGroup, so we assume all of them are enabled
return true;
});
}

/**
Expand Down Expand Up @@ -284,19 +289,21 @@ public function setActionGroup(ActionGroup $actionGroup): void
/**
* @param string $groupName
*
* @return Action[]
* @return array|Action[]
*/
public function getActions(string $groupName): array
{
return $this->getActionGroup($groupName)->getActions();
}

/**
* @return array
* @return array|Action[]
*/
public function getEnabledActions($groupName): array
{
return $this->getEnabledItems($this->getActions($groupName));
return array_filter($this->getActions($groupName), function (Action $action): bool {
return $action->isEnabled();
});
}

/**
Expand All @@ -310,19 +317,21 @@ public function hasActionGroup(string $name): bool
}

/**
* @return array
* @return array|Filter[]
*/
public function getFilters(): array
{
return $this->filters;
}

/**
* @return array
* @return array|Filter[]
*/
public function getEnabledFilters(): array
{
return $this->getEnabledItems($this->getFilters());
return array_filter($this->getFilters(), function (Filter $filter): bool {
return $filter->isEnabled();
});
}

/**
Expand Down Expand Up @@ -380,21 +389,4 @@ public function hasFilter(string $name): bool
{
return array_key_exists($name, $this->filters);
}

/**
* @param array $items
*
* @return array
*/
private function getEnabledItems(array $items): array
{
$filteredItems = [];
foreach ($items as $item) {
if ($item->isEnabled()) {
$filteredItems[] = $item;
}
}

return $filteredItems;
}
}
8 changes: 4 additions & 4 deletions Filter/DateFilter.php
Expand Up @@ -35,19 +35,19 @@ public function apply(DataSourceInterface $dataSource, string $name, $data, arra
if (null !== $from) {
$inclusive = (bool) $this->getOption($options, 'inclusive_from', self::DEFAULT_INCLUSIVE_FROM);
if (true === $inclusive) {
$expressionBuilder->greaterThanOrEqual($field, $from);
$dataSource->restrict($expressionBuilder->greaterThanOrEqual($field, $from));
} else {
$expressionBuilder->greaterThan($field, $from);
$dataSource->restrict($expressionBuilder->greaterThan($field, $from));
}
}

$to = isset($data['to']) ? $this->getDateTime($data['to'], '23:59') : null;
if (null !== $to) {
$inclusive = (bool) $this->getOption($options, 'inclusive_to', self::DEFAULT_INCLUSIVE_TO);
if (true === $inclusive) {
$expressionBuilder->lessThanOrEqual($field, $to);
$dataSource->restrict($expressionBuilder->lessThanOrEqual($field, $to));
} else {
$expressionBuilder->lessThan($field, $to);
$dataSource->restrict($expressionBuilder->lessThan($field, $to));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Filter/MoneyFilter.php
Expand Up @@ -41,10 +41,10 @@ public function apply(DataSourceInterface $dataSource, string $name, $data, arra
$dataSource->restrict($expressionBuilder->equals($options['currency_field'], $data['currency']));
}
if ('' !== $greaterThan) {
$expressionBuilder->greaterThan($field, $this->normalizeAmount((float) $greaterThan, $scale));
$dataSource->restrict($expressionBuilder->greaterThan($field, $this->normalizeAmount((float) $greaterThan, $scale)));
}
if ('' !== $lessThan) {
$expressionBuilder->lessThan($field, $this->normalizeAmount((float) $lessThan, $scale));
$dataSource->restrict($expressionBuilder->lessThan($field, $this->normalizeAmount((float) $lessThan, $scale)));
}
}

Expand Down
39 changes: 15 additions & 24 deletions Provider/ArrayGridProvider.php
Expand Up @@ -19,45 +19,36 @@

final class ArrayGridProvider implements GridProviderInterface
{
/**
* @var Grid[]
*/
private $grids = [];
/** @var ArrayToDefinitionConverterInterface */
private $converter;

/** @var array[] */
private $gridConfigurations;

/**
* @param ArrayToDefinitionConverterInterface $converter
* @param array $gridConfigurations
*/
public function __construct(ArrayToDefinitionConverterInterface $converter, array $gridConfigurations)
{
foreach ($gridConfigurations as $code => $gridConfiguration) {
if (isset($gridConfiguration['extends'], $gridConfigurations[$gridConfiguration['extends']])) {
$gridConfiguration = $this->extend($gridConfiguration, $gridConfigurations[$gridConfiguration['extends']]);
}

$this->grids[$code] = $converter->convert($code, $gridConfiguration);
}
$this->converter = $converter;
$this->gridConfigurations = $gridConfigurations;
}

/**
* {@inheritdoc}
*/
public function get(string $code): Grid
{
if (!array_key_exists($code, $this->grids)) {
if (!array_key_exists($code, $this->gridConfigurations)) {
throw new UndefinedGridException($code);
}

// Need to clone grid definition in case of displaying on one page two grids using the same grid definition
return clone $this->grids[$code];
$gridConfiguration = $this->gridConfigurations[$code];

if (isset($gridConfiguration['extends'], $this->gridConfigurations[$gridConfiguration['extends']])) {
$gridConfiguration = $this->extend($gridConfiguration, $this->gridConfigurations[$gridConfiguration['extends']]);
}

return $this->converter->convert($code, $gridConfiguration);
}

/**
* @param array $gridConfiguration
* @param array $parentGridConfiguration
*
* @return array
*/
private function extend(array $gridConfiguration, array $parentGridConfiguration): array
{
unset($parentGridConfiguration['sorting']); // Do not inherit sorting.
Expand Down
20 changes: 20 additions & 0 deletions spec/Definition/GridSpec.php
Expand Up @@ -207,6 +207,26 @@ function it_can_replace_action_group(ActionGroup $firstActionGroup, ActionGroup
$this->getActionGroup('row')->shouldReturn($secondActionGroup);
}

function it_can_return_action_groups(ActionGroup $firstActionGroup, ActionGroup $secondActionGroup): void
{
$firstActionGroup->getName()->willReturn('first');
$secondActionGroup->getName()->willReturn('second');
$this->addActionGroup($firstActionGroup);
$this->addActionGroup($secondActionGroup);

$this->getActionGroups()->shouldHaveCount(2);
}

function it_can_return_only_enabled_action_groups(ActionGroup $firstActionGroup, ActionGroup $secondActionGroup): void
{
$firstActionGroup->getName()->willReturn('first');
$secondActionGroup->getName()->willReturn('second');
$this->addActionGroup($firstActionGroup);
$this->addActionGroup($secondActionGroup);

$this->getEnabledActionGroups()->shouldHaveCount(2);
}

function it_returns_actions_for_given_group(ActionGroup $actionGroup, Action $action): void
{
$actionGroup->getName()->willReturn('row');
Expand Down
48 changes: 16 additions & 32 deletions spec/Filter/DateFilterSpec.php
Expand Up @@ -31,10 +31,8 @@ function it_filters_date_from(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->greaterThanOrEqual('checkoutCompletedAt', '2016-12-05 08:00')
->shouldBeCalled()
;
$expressionBuilder->greaterThanOrEqual('checkoutCompletedAt', '2016-12-05 08:00')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply(
$dataSource,
Expand All @@ -55,10 +53,8 @@ function it_filters_date_from_not_inclusive(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->greaterThan('checkoutCompletedAt', '2016-12-05 08:00')
->shouldBeCalled()
;
$expressionBuilder->greaterThan('checkoutCompletedAt', '2016-12-05 08:00')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply(
$dataSource,
Expand All @@ -83,10 +79,8 @@ function it_filters_date_from_with_default_time(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->greaterThanOrEqual('checkoutCompletedAt', '2016-12-05 00:00')
->shouldBeCalled()
;
$expressionBuilder->greaterThanOrEqual('checkoutCompletedAt', '2016-12-05 00:00')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply(
$dataSource,
Expand All @@ -111,10 +105,8 @@ function it_filters_date_to(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->lessThan('checkoutCompletedAt', '2016-12-06 08:00')
->shouldBeCalled()
;
$expressionBuilder->lessThan('checkoutCompletedAt', '2016-12-06 08:00')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply(
$dataSource,
Expand All @@ -135,10 +127,8 @@ function it_filters_date_to_inclusive(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->lessThanOrEqual('checkoutCompletedAt', '2016-12-06 08:00')
->shouldBeCalled()
;
$expressionBuilder->lessThanOrEqual('checkoutCompletedAt', '2016-12-06 08:00')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply(
$dataSource,
Expand All @@ -163,10 +153,8 @@ function it_filters_date_to_with_default_time(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->lessThan('checkoutCompletedAt', '2016-12-06 23:59')
->shouldBeCalled()
;
$expressionBuilder->lessThan('checkoutCompletedAt', '2016-12-06 23:59')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply(
$dataSource,
Expand All @@ -187,15 +175,11 @@ function it_filters_date_from_to(
): void {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder
->greaterThanOrEqual('checkoutCompletedAt', '2016-12-05 08:00')
->shouldBeCalled()
;
$expressionBuilder->greaterThanOrEqual('checkoutCompletedAt', '2016-12-05 08:00')->willReturn('EXPR1');
$dataSource->restrict('EXPR1')->shouldBeCalled();

$expressionBuilder
->lessThan('checkoutCompletedAt', '2016-12-06 08:00')
->shouldBeCalled()
;
$expressionBuilder->lessThan('checkoutCompletedAt', '2016-12-06 08:00')->willReturn('EXPR2');
$dataSource->restrict('EXPR2')->shouldBeCalled();

$this->apply(
$dataSource,
Expand Down

0 comments on commit 056f30a

Please sign in to comment.