Completed
Push — master ( c6c2cb...c52bd8 )
by Marcos
05:41
created

CallbackComparator::compare()   A

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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace PhpCommon\Comparison\Comparator;
4
5
use PhpCommon\Comparison\Comparator;
6
7
/**
8
 * A comparator the uses a callback to compare values.
9
 *
10
 * This comparator is useful when simple, one-off comparators need to be
11
 * created. It can be used to change or extend existing comparators in order to
12
 * achieve new ones that better fit to the underlying needs.
13
 *
14
 * @author Marcos Passos <[email protected]>
15
 */
16
class CallbackComparator implements Comparator
17
{
18
    /**
19
     * @var callable
20
     */
21
    protected $callback;
22
23
    /**
24
     * CallbackComparator constructor.
25
     *
26
     * @param callable $callback
27
     */
28 2
    public function __construct(callable $callback)
29
    {
30 2
        $this->callback = $callback;
31 2
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function compare($left, $right)
37
    {
38 2
        return call_user_func_array($this->callback, [$left, $right]);
39
    }
40
}
41