CallbackComparator::compare()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the phpcommon/comparison package.
5
 *
6
 * (c) Marcos Passos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace PhpCommon\Comparison\Comparator;
13
14
use PhpCommon\Comparison\Comparator;
15
16
/**
17
 * A comparator the uses a callback to compare values.
18
 *
19
 * This comparator is useful when simple, one-off comparators need to be
20
 * created. It can be used to change or extend existing comparators in order to
21
 * achieve new ones that better fit to the underlying needs.
22
 *
23
 * @author Marcos Passos <[email protected]>
24
 */
25
class CallbackComparator implements Comparator
26
{
27
    /**
28
     * @var callable
29
     */
30
    protected $callback;
31
32
    /**
33
     * CallbackComparator constructor.
34
     *
35
     * @param callable $callback
36
     */
37 2
    public function __construct(callable $callback)
38
    {
39 2
        $this->callback = $callback;
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function compare($left, $right)
46
    {
47 2
        return call_user_func_array($this->callback, [$left, $right]);
48
    }
49
}
50