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