ReplicationOption.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\MasterSlaveReplication;
  13. use Predis\Connection\ReplicationConnectionInterface;
  14. /**
  15. * Configures an aggregate connection used for master/slave
  16. * replication between multiple 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()
  26. * as discussed here https://bugs.php.net/bug.php?id=49510.
  27. */
  28. public function filter(OptionsInterface $options, $value)
  29. {
  30. if ($value instanceof ReplicationConnectionInterface) {
  31. return $value;
  32. }
  33. if (is_bool($value)) {
  34. return $value ? $this->getDefault($options) : null;
  35. }
  36. if (!is_object($value) && null !== $asbool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) {
  37. return $asbool ? $this->getDefault($options) : null;
  38. }
  39. throw new InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getDefault(OptionsInterface $options)
  45. {
  46. return new MasterSlaveReplication();
  47. }
  48. }