-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
339 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/RunOpenCode/Bundle/ExchangeRate/Validator/Constraints/BaseCurrency.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/RunOpenCode/Bundle/ExchangeRate/Validator/Constraints/BaseCurrencyValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.