-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCallbackComparer.php
More file actions
45 lines (36 loc) · 899 Bytes
/
CallbackComparer.php
File metadata and controls
45 lines (36 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
declare( strict_types = 1 );
namespace Diff\Comparer;
/**
* Adapter around a comparison callback that implements the ValueComparer
* interface.
*
* @since 0.6
*
* @license BSD-3-Clause
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class CallbackComparer implements ValueComparer {
/** @var callable */
private $callback;
/**
* @since 0.6
*
* @param callable $callback
*/
public function __construct( $callback ) {
$this->callback = $callback;
}
/**
* @param mixed $firstValue
* @param mixed $secondValue
* @return bool
*/
public function valuesAreEqual( $firstValue, $secondValue ): bool {
$valuesAreEqual = call_user_func_array( $this->callback, [ $firstValue, $secondValue ] );
if ( !is_bool( $valuesAreEqual ) ) {
throw new \RuntimeException( 'ValueComparer callback needs to return a boolean' );
}
return $valuesAreEqual;
}
}