ReplicationOption.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Connection\Aggregate\MasterSlaveReplication;
  13. use Predis\Connection\Aggregate\ReplicationInterface;
  14. /**
  15. * Configures an aggregate connection used for master/slave replication among
  16. * multiple Redis nodes.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class ReplicationOption implements OptionInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @todo There's more code than needed due to a bug in filter_var() as
  26. * discussed here https://bugs.php.net/bug.php?id=49510 and different
  27. * behaviours when encountering NULL values on PHP 5.3.
  28. */
  29. public function filter(OptionsInterface $options, $value)
  30. {
  31. if ($value instanceof ReplicationInterface) {
  32. return $value;
  33. }
  34. if (is_bool($value) || $value === null) {
  35. return $value ? $this->getDefault($options) : null;
  36. }
  37. if (
  38. !is_object($value) &&
  39. null !== $asbool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
  40. ) {
  41. return $asbool ? $this->getDefault($options) : null;
  42. }
  43. throw new InvalidArgumentException(
  44. "An instance of type 'Predis\Connection\Aggregate\ReplicationInterface' was expected."
  45. );
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getDefault(OptionsInterface $options)
  51. {
  52. return new MasterSlaveReplication();
  53. }
  54. }