Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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