ProfileOption.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  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 Predis\Configuration;
  11. use InvalidArgumentException;
  12. use Predis\Profile\ServerProfile;
  13. use Predis\Profile\ServerProfileInterface;
  14. /**
  15. * Configures the server profile to be used by the client
  16. * to create command instances depending on the specified
  17. * version of the Redis server.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class ProfileOption implements OptionInterface
  22. {
  23. /**
  24. * Sets the needed commands processors that should be applied to the profile.
  25. *
  26. * @param OptionsInterface $options Client options.
  27. * @param ServerProfileInterface $profile Server profile.
  28. */
  29. protected function setProcessors(OptionsInterface $options, ServerProfileInterface $profile)
  30. {
  31. if (isset($options->prefix)) {
  32. $profile->setProcessor($options->prefix);
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function filter(OptionsInterface $options, $value)
  39. {
  40. if (is_string($value)) {
  41. $value = ServerProfile::get($value);
  42. $this->setProcessors($options, $value);
  43. } else if (!$value instanceof ServerProfileInterface) {
  44. throw new InvalidArgumentException('Invalid value for the profile option');
  45. }
  46. return $value;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getDefault(OptionsInterface $options)
  52. {
  53. $profile = ServerProfile::getDefault();
  54. $this->setProcessors($options, $profile);
  55. return $profile;
  56. }
  57. }