| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Exchange Rate Bundle, an RunOpenCode project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) 2017 RunOpenCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace RunOpenCode\Bundle\ExchangeRate\Validator\Constraints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use RunOpenCode\Bundle\ExchangeRate\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use RunOpenCode\ExchangeRate\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use RunOpenCode\ExchangeRate\Contract\RateInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Validator\Constraint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Validator\ConstraintValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class ExchangeRateValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Exchange rate validator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @package RunOpenCode\Bundle\ExchangeRate\Validator\Constraints | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class ExchangeRateValidator extends ConstraintValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var RatesConfigurationRegistryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $ratesConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 13 |  |     public function __construct(RatesConfigurationRegistryInterface $ratesConfiguration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 13 |  |         $this->ratesConfiguration = $ratesConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 13 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 41 | 13 |  |     public function validate($rate, Constraint $constraint) | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 43 | 13 |  |         if (!$constraint instanceof ExchangeRate) { | 
            
                                                        
            
                                    
            
            
                | 44 | 1 |  |             throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', ExchangeRate::class, get_class($constraint))); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 | 12 |  |         if (null === $rate) { | 
            
                                                        
            
                                    
            
            
                | 48 | 1 |  |             return; | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 | 11 |  |         if (!$rate instanceof RateInterface) { | 
            
                                                        
            
                                    
            
            
                | 52 | 1 |  |             throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', RateInterface::class, get_class($rate))); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 | 10 |  |         if ($rate->getCurrencyCode() && $rate->getRateType()) { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             /** | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |              * @var Configuration $rateConfiguration | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |              */ | 
            
                                                        
            
                                    
            
            
                | 60 | 10 |  |             foreach ($this->ratesConfiguration as $rateConfiguration) { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |                 if ( | 
            
                                                        
            
                                    
            
            
                | 63 | 10 |  |                     $rateConfiguration->getRateType() === $rate->getRateType() | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |                     && | 
            
                                                        
            
                                    
            
            
                | 65 | 10 |  |                     $rateConfiguration->getCurrencyCode() === $rate->getCurrencyCode() | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |                     && | 
            
                                                        
            
                                    
            
            
                | 67 | 10 |  |                     $rateConfiguration->getSourceName() === $rate->getSourceName() | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                 ) { | 
            
                                                        
            
                                    
            
            
                | 69 | 10 |  |                     return; | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |             /** | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |              * @var ExchangeRate $constraint | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |              */ | 
            
                                                        
            
                                    
            
            
                | 75 | 2 |  |             $this->context | 
            
                                                        
            
                                    
            
            
                | 76 | 2 |  |                 ->buildViolation($constraint->message) | 
            
                                                        
            
                                    
            
            
                | 77 | 2 |  |                 ->setParameter('{{ rate_type }}', $rate->getRateType()) | 
            
                                                        
            
                                    
            
            
                | 78 | 2 |  |                 ->setParameter('{{ currency_code }}', $rate->getCurrencyCode()) | 
            
                                                        
            
                                    
            
            
                | 79 | 2 |  |                 ->setParameter('{{ source_name }}', $rate->getSourceName()) | 
            
                                                        
            
                                    
            
            
                | 80 | 2 |  |                 ->setTranslationDomain('runopencode_exchange_rate') | 
            
                                                        
            
                                    
            
            
                | 81 | 2 |  |                 ->addViolation(); | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 83 | 2 |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |