Factory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the Comparator package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\Comparator;
  11. /**
  12. * Factory for comparators which compare values for equality.
  13. */
  14. class Factory
  15. {
  16. /**
  17. * @var Comparator[]
  18. */
  19. private $comparators = array();
  20. /**
  21. * @var Factory
  22. */
  23. private static $instance;
  24. /**
  25. * Constructs a new factory.
  26. */
  27. public function __construct()
  28. {
  29. $this->register(new TypeComparator);
  30. $this->register(new ScalarComparator);
  31. $this->register(new NumericComparator);
  32. $this->register(new DoubleComparator);
  33. $this->register(new ArrayComparator);
  34. $this->register(new ResourceComparator);
  35. $this->register(new ObjectComparator);
  36. $this->register(new ExceptionComparator);
  37. $this->register(new SplObjectStorageComparator);
  38. $this->register(new DOMNodeComparator);
  39. $this->register(new MockObjectComparator);
  40. $this->register(new DateTimeComparator);
  41. }
  42. /**
  43. * @return Factory
  44. */
  45. public static function getInstance()
  46. {
  47. if (self::$instance === null) {
  48. self::$instance = new self;
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * Returns the correct comparator for comparing two values.
  54. *
  55. * @param mixed $expected The first value to compare
  56. * @param mixed $actual The second value to compare
  57. * @return Comparator
  58. */
  59. public function getComparatorFor($expected, $actual)
  60. {
  61. foreach ($this->comparators as $comparator) {
  62. if ($comparator->accepts($expected, $actual)) {
  63. return $comparator;
  64. }
  65. }
  66. }
  67. /**
  68. * Registers a new comparator.
  69. *
  70. * This comparator will be returned by getInstance() if its accept() method
  71. * returns TRUE for the compared values. It has higher priority than the
  72. * existing comparators, meaning that its accept() method will be tested
  73. * before those of the other comparators.
  74. *
  75. * @param Comparator $comparator The registered comparator
  76. */
  77. public function register(Comparator $comparator)
  78. {
  79. array_unshift($this->comparators, $comparator);
  80. $comparator->setFactory($this);
  81. }
  82. /**
  83. * Unregisters a comparator.
  84. *
  85. * This comparator will no longer be returned by getInstance().
  86. *
  87. * @param Comparator $comparator The unregistered comparator
  88. */
  89. public function unregister(Comparator $comparator)
  90. {
  91. foreach ($this->comparators as $key => $_comparator) {
  92. if ($comparator === $_comparator) {
  93. unset($this->comparators[$key]);
  94. }
  95. }
  96. }
  97. }