ReplicationOption.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * and a different behaviour with NULL values on PHP 5.3.
  28. */
  29. public function filter(OptionsInterface $options, $value)
  30. {
  31. if ($value instanceof ReplicationConnectionInterface) {
  32. return $value;
  33. }
  34. if (is_bool($value) || $value === null) {
  35. return $value ? $this->getDefault($options) : null;
  36. }
  37. if (!is_object($value) && null !== $asbool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) {
  38. return $asbool ? $this->getDefault($options) : null;
  39. }
  40. throw new InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getDefault(OptionsInterface $options)
  46. {
  47. return new MasterSlaveReplication();
  48. }
  49. }