Skip to content

Commit

Permalink
Added better validation on DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCelavi committed May 22, 2017
1 parent 13d0859 commit a382782
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 4 deletions.
66 changes: 65 additions & 1 deletion src/RunOpenCode/Bundle/ExchangeRate/Form/Dto/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@
use RunOpenCode\ExchangeRate\Contract\RateInterface;
use RunOpenCode\ExchangeRate\Model\Rate as ExchangeRate;
use Symfony\Component\Validator\Constraints as Assert;
use RunOpenCode\Bundle\ExchangeRate\Validator\Constraints as ExchangeRateAssert;

/**
* Class Rate
*
* @package RunOpenCode\Bundle\ExchangeRate\Form\Dto
*
* @ExchangeRateAssert\ExchangeRate()
*/
class Rate
class Rate implements RateInterface
{
/**
* @var string
*
* @Assert\NotBlank()
*/
private $rate;

/**
* @var \DateTime
*
* @Assert\NotBlank()
* @Assert\DateTime()
*/
private $date;

Expand All @@ -40,6 +48,9 @@ class Rate

/**
* @var string
*
* @Assert\NotBlank()
* @ExchangeRateAssert\BaseCurrency()
*/
private $baseCurrencyCode;

Expand Down Expand Up @@ -130,6 +141,59 @@ public function setBaseCurrencyCode($baseCurrencyCode)
return $this;
}

/**
* {@inheritdoc}
*/
public function getSourceName()
{
if (false !== strpos($this->getRate(), '.')) {
return explode('.', $this->getRate())[0];
}

return null;
}

/**
* {@inheritdoc}
*/
public function getCurrencyCode()
{
if (false !== strpos($this->getRate(), '.')) {
return explode('.', $this->getRate())[2];
}

return null;
}

/**
* {@inheritdoc}
*/
public function getRateType()
{
if (false !== strpos($this->getRate(), '.')) {
return explode('.', $this->getRate())[1];
}

return null;
}

/**
* {@inheritdoc}
*/
public function getCreatedAt()
{
return null; // unknown
}

/**
* {@inheritdoc}
*/
public function getModifiedAt()
{
return null; // unknown
}


/**
* Build \RunOpenCode\ExchangeRate\Model\Rate from DTO object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

<parameter key="runopencode.exchange_rate.validator.exchange_rate.class">RunOpenCode\Bundle\ExchangeRate\Validator\Constraints\ExchangeRateValidator</parameter>

<parameter key="runopencode.exchange_rate.validator.base_currency.class">RunOpenCode\Bundle\ExchangeRate\Validator\Constraints\BaseCurrencyValidator</parameter>

</parameters>

<services>
Expand All @@ -18,6 +20,11 @@
<tag name="validator.constraint_validator" alias="runopencode.exchange_rate.rate_validator" />
</service>

<service id="runopencode.exchange_rate.validator.base_currency" class="%runopencode.exchange_rate.validator.base_currency.class%">
<argument>%runopencode.exchange_rate.base_currency%</argument>
<tag name="validator.constraint_validator" alias="runopencode.exchange_rate.base_currency_validator" />
</service>

</services>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ flash:
success: You have successfully modified exchange rate.
error.unknown: Could not save exchange rate for unknown reason. Contact administrator.


validator:
rate.invalid: Unknown rate provided ({{ rate_type }}, {{ currency_code }}, {{ source_name }}).
baseCurrency.invalid: Invalid base currency code "{{ value }}" provided, expected "{{ base_currency_code }}".


#exchange_rate:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Exchange Rate Bundle, an RunOpenCode project.
*
* (c) 2017 RunOpenCode
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RunOpenCode\Bundle\ExchangeRate\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* Class BaseCurrency
*
* @package RunOpenCode\Bundle\ExchangeRate\Validator\Constraints
*
* {@inheritdoc}
*
* @Annotation
*/
class BaseCurrency extends Constraint
{
public $message = 'validator.baseCurrency.invalid';

/**
* {@inheritdoc}
*/
public function validatedBy()
{
return 'runopencode.exchange_rate.base_currency_validator';
}

/**
* {@inheritdoc}
*/
public function getTargets()
{
return self::PROPERTY_CONSTRAINT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Exchange Rate Bundle, an RunOpenCode project.
*
* (c) 2017 RunOpenCode
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RunOpenCode\Bundle\ExchangeRate\Validator\Constraints;

use RunOpenCode\Bundle\ExchangeRate\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* Class BaseCurrencyValidator
*
* @package RunOpenCode\Bundle\ExchangeRate\Validator\Constraints
*/
class BaseCurrencyValidator extends ConstraintValidator
{
/**
* @var string
*/
private $baseCurrencyCode;

public function __construct($baseCurrencyCode)
{
$this->baseCurrencyCode = $baseCurrencyCode;
}

public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof BaseCurrency) {
throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', BaseCurrency::class, get_class($constraint)));
}

if (null === $value) {
return;
}

if ($this->baseCurrencyCode !== $value) {

/**
* @var BaseCurrency $constraint
*/
$this->context
->buildViolation($constraint->message)
->setParameter('{{ base_currency_code }}', $this->baseCurrencyCode)
->setParameter('{{ value }}', $value)
->setTranslationDomain('runopencode_exchange_rate')
->addViolation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
use Symfony\Component\Validator\Constraint;

/**
* Class ExchangeRate
*
* @package RunOpenCode\Bundle\ExchangeRate\Validator\Constraints
*
* {@inheritdoc}
*
* @Annotation
*/
class ExchangeRate extends Constraint
{
public $message = 'runopencode.exchange_rate.rate_validator.invalid';
public $message = 'validator.rate.invalid';

/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function validate($rate, Constraint $constraint)
*/
$this->context
->buildViolation($constraint->message)
->setParameter('{{ rate_type }}', $rate->getRateType())
->setParameter('{{ currency_code }}', $rate->getCurrencyCode())
->setParameter('{{ source_name }}', $rate->getSourceName())
->setTranslationDomain('runopencode_exchange_rate')
->addViolation();
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/Validator/Constraints/BaseCurrencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Exchange Rate Bundle, an RunOpenCode project.
*
* (c) 2017 RunOpenCode
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RunOpenCode\Bundle\ExchangeRate\Tests\Validator\Constraints;

use PHPUnit\Framework\TestCase;
use RunOpenCode\Bundle\ExchangeRate\Validator\Constraints\BaseCurrency;

/**
* Class BaseCurrencyTest
*
* @package RunOpenCode\Bundle\ExchangeRate\Tests\Validator\Constraints
*/
class BaseCurrencyTest extends TestCase
{
/**
* @test
*/
public function constraintSettings()
{
$constraint = new BaseCurrency();

$this->assertEquals(BaseCurrency::PROPERTY_CONSTRAINT, $constraint->getTargets());
$this->assertEquals('runopencode.exchange_rate.base_currency_validator', $constraint->validatedBy());
}
}

0 comments on commit a382782

Please sign in to comment.