Skip to content

Commit

Permalink
Add filter for currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Mar 11, 2020
1 parent c60cf88 commit 21c56e5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Sylius/Behat/Client/ApiClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function isUpdateSuccessful(): bool;
/** @param string|int $value */
public function responseHasValue(string $key, $value): bool;

/** @param string|int $value */
public function relatedResourceHasValue(string $resource, string $key, $value): bool;

/** @param string|float $value */
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Client/ApiPlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function buildFilter(array $filters): void
$this->filters = $filters;
}

/** @param string|int */
/** @param string|int $value */
public function addRequestData(string $key, $value): void
{
$this->request['body'][$key] = $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Resources/config/suites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
imports:
- suites/api/admin/login.yml
- suites/api/currency/managing_currencies.yml
- suites/api/payment/managing_payments.yml
- suites/api/currency/managing_exchange_rates.yml
- suites/api/payment/managing_payments.yml
- suites/api/product/managing_product_options.yml
- suites/api/product/managing_product_reviews.yml
- suites/api/product/managing_product_variants.yml
Expand Down
54 changes: 54 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Filters/ExchangeRateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Filters;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Webmozart\Assert\Assert;

final class ExchangeRateFilter extends AbstractContextAwareFilter
{
protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
string $operationName = null
) {
if ($property === 'currencyCode') {
$queryBuilder
->innerJoin('o.sourceCurrency', 'sourceCurrency')
->innerJoin('o.targetCurrency', 'targetCurrency')
->where('sourceCurrency.code LIKE CONCAT(\'%\', :code, \'%\')')
->orWhere('targetCurrency.code LIKE CONCAT(\'%\', :code, \'%\')')
->setParameter('code', $value)
;
}
}

public function getDescription(string $resourceClass): array
{
$description = [];

$description['currencyCode'] = [
'type' => 'string',
'required' => false,
'property' => 'currencyCode',
];

return $description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
</attribute>
</attribute>
<collectionOperations>
<collectionOperation name="get" />
<collectionOperation name="get" >
<attribute name="filters">
<attribute>exchange_rate_filter</attribute>
</attribute>
</collectionOperation>
<collectionOperation name="post">
<attribute name="denormalization_context">
<attribute name="groups">exchange_rate:create</attribute>
Expand Down
17 changes: 17 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/filters.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
This file is part of the Sylius package.
(c) Paweł Jędrzejewski
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.api.filter.exchange_rate" class="Sylius\Bundle\ApiBundle\Filters\ExchangeRateFilter">
<tag name="api_platform.filter" id="exchange_rate_filter" />
<argument type="service" id="doctrine" />
</service>
</services>
</container>
4 changes: 4 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<imports>
<import resource="filters.xml"/>
</imports>

<services>
<service id="sylius.api.state_machine_transition_applicator" class="Sylius\Bundle\ApiBundle\Applicator\ProductReviewStateMachineTransitionApplicator" public="true">
<argument id="sm.factory" type="service" />
Expand Down

0 comments on commit 21c56e5

Please sign in to comment.