ProfileOption.php 1.7 KB

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