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\Factory;
  13. use Predis\Profile\ProfileInterface;
  14. use Predis\Profile\RedisProfile;
  15. /**
  16. * Configures the server profile to be used by the client to create command
  17. * instances depending on the specified version of the Redis server.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class ProfileOption implements OptionInterface
  22. {
  23. /**
  24. * Sets the commands processors that need to be applied to the profile.
  25. *
  26. * @param OptionsInterface $options Client options.
  27. * @param ProfileInterface $profile Server profile.
  28. */
  29. protected function setProcessors(OptionsInterface $options, ProfileInterface $profile)
  30. {
  31. if (isset($options->prefix) && $profile instanceof RedisProfile) {
  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 = Factory::get($value);
  42. $this->setProcessors($options, $value);
  43. } else if (!$value instanceof ProfileInterface) {
  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 = Factory::getDefault();
  54. $this->setProcessors($options, $profile);
  55. return $profile;
  56. }
  57. }