ProfileOption.php 1.7 KB

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