ClientProfile.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Options;
  11. use Predis\Profiles\ServerProfile;
  12. use Predis\Profiles\IServerProfile;
  13. /**
  14. * Option class that handles server profiles to be used by a client.
  15. *
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. class ClientProfile extends Option
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function filter(IClientOptions $options, $value)
  24. {
  25. if (is_string($value)) {
  26. $value = ServerProfile::get($value);
  27. if (isset($options->prefix)) {
  28. $value->setProcessor($options->prefix);
  29. }
  30. }
  31. if (is_callable($value)) {
  32. $value = call_user_func($value, $options);
  33. }
  34. if (!$value instanceof IServerProfile) {
  35. throw new \InvalidArgumentException('Invalid value for the profile option');
  36. }
  37. return $value;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getDefault(IClientOptions $options)
  43. {
  44. $profile = ServerProfile::getDefault();
  45. if (isset($options->prefix)) {
  46. $profile->setProcessor($options->prefix);
  47. }
  48. return $profile;
  49. }
  50. }