PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Symfony OptionResolver: how to check that of two options only one is set

 March 09, 2022     symfony     No comments   

Issue

I have to validate the configuration of the Amazon MWS ListOrders call.

This call accepts both CreatedAfter and LastUpdatedAfter, but only one of the two, not both at the same time.

So, how can I check this condition with OptionResolver?

I think I can do something like this:

$resolver = new OptionsResolver();
$resolver->setDefined(['CreatedAfter', 'LastUpdatedAfter']);
$resolver->setAllowedTypes([...]);

But at this point how can I check the condition that only one is set?

I would like to do this during the $resolver->resolve() call.

Or should have I first resolve the options and then check that only one of them is set using the is*() methods?


Solution

... only one of the two, not both at the same time.

For validate that before you can use them, use setNormalizer():

$resolver->setDefined(['CreatedAfter', 'LastUpdatedAfter']);

$resolver->setNormalizer('CreatedAfter', function (Options $options, $value) {
    if (null === $value xor null === $options['LastUpdatedAfter']) {
        return value;
    }

    throw new \InvalidArgumentException('Both are null or both are provided');
});


Answered By - yceruto
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing