Factory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  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 Factory
  18. */
  19. private static $instance;
  20. /**
  21. * @var Comparator[]
  22. */
  23. private $customComparators = [];
  24. /**
  25. * @var Comparator[]
  26. */
  27. private $defaultComparators = [];
  28. /**
  29. * @return Factory
  30. */
  31. public static function getInstance()
  32. {
  33. if (self::$instance === null) {
  34. self::$instance = new self;
  35. }
  36. return self::$instance;
  37. }
  38. /**
  39. * Constructs a new factory.
  40. */
  41. public function __construct()
  42. {
  43. $this->registerDefaultComparators();
  44. }
  45. /**
  46. * Returns the correct comparator for comparing two values.
  47. *
  48. * @param mixed $expected The first value to compare
  49. * @param mixed $actual The second value to compare
  50. *
  51. * @return Comparator
  52. */
  53. public function getComparatorFor($expected, $actual)
  54. {
  55. foreach ($this->customComparators as $comparator) {
  56. if ($comparator->accepts($expected, $actual)) {
  57. return $comparator;
  58. }
  59. }
  60. foreach ($this->defaultComparators as $comparator) {
  61. if ($comparator->accepts($expected, $actual)) {
  62. return $comparator;
  63. }
  64. }
  65. }
  66. /**
  67. * Registers a new comparator.
  68. *
  69. * This comparator will be returned by getComparatorFor() if its accept() method
  70. * returns TRUE for the compared values. It has higher priority than the
  71. * existing comparators, meaning that its accept() method will be invoked
  72. * before those of the other comparators.
  73. *
  74. * @param Comparator $comparator The comparator to be registered
  75. */
  76. public function register(Comparator $comparator)
  77. {
  78. \array_unshift($this->customComparators, $comparator);
  79. $comparator->setFactory($this);
  80. }
  81. /**
  82. * Unregisters a comparator.
  83. *
  84. * This comparator will no longer be considered by getComparatorFor().
  85. *
  86. * @param Comparator $comparator The comparator to be unregistered
  87. */
  88. public function unregister(Comparator $comparator)
  89. {
  90. foreach ($this->customComparators as $key => $_comparator) {
  91. if ($comparator === $_comparator) {
  92. unset($this->customComparators[$key]);
  93. }
  94. }
  95. }
  96. /**
  97. * Unregisters all non-default comparators.
  98. */
  99. public function reset()
  100. {
  101. $this->customComparators = [];
  102. }
  103. private function registerDefaultComparators()
  104. {
  105. $this->registerDefaultComparator(new MockObjectComparator);
  106. $this->registerDefaultComparator(new DateTimeComparator);
  107. $this->registerDefaultComparator(new DOMNodeComparator);
  108. $this->registerDefaultComparator(new SplObjectStorageComparator);
  109. $this->registerDefaultComparator(new ExceptionComparator);
  110. $this->registerDefaultComparator(new ObjectComparator);
  111. $this->registerDefaultComparator(new ResourceComparator);
  112. $this->registerDefaultComparator(new ArrayComparator);
  113. $this->registerDefaultComparator(new DoubleComparator);
  114. $this->registerDefaultComparator(new NumericComparator);
  115. $this->registerDefaultComparator(new ScalarComparator);
  116. $this->registerDefaultComparator(new TypeComparator);
  117. }
  118. private function registerDefaultComparator(Comparator $comparator)
  119. {
  120. $this->defaultComparators[] = $comparator;
  121. $comparator->setFactory($this);
  122. }
  123. }