ReplicationOption.php 1.7 KB

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