Skip to content

Commit 7d23bca

Browse files
committed
Refactor Callback assert to use only callbacks
1 parent fb3f154 commit 7d23bca

2 files changed

Lines changed: 12 additions & 23 deletions

File tree

src/Assert/Callback.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Harp\Validate\Assert;
44

55
use Harp\Validate\Error;
6-
use InvalidArgumentException;
6+
use Closure;
77

88
/**
99
* Assert that the result of a given callback is true. You can thus use php's native validation functions.
@@ -25,12 +25,8 @@ class Callback extends AbstractAssertion
2525
* @param mixed $callback
2626
* @param string $message
2727
*/
28-
public function __construct($name, $callback, $message = ':name is invalid')
28+
public function __construct($name, Closure $callback, $message = ':name is invalid')
2929
{
30-
if (! is_callable($callback)) {
31-
throw new InvalidArgumentException('Callback should be callable');
32-
}
33-
3430
$this->callback = $callback;
3531

3632
parent::__construct($name, $message);
@@ -53,7 +49,9 @@ public function execute($subject)
5349
if ($this->issetProperty($subject, $this->getName())) {
5450
$value = $this->getProperty($subject, $this->getName());
5551

56-
if (! call_user_func($this->callback, $value)) {
52+
$callback = $this->callback;
53+
54+
if (! $callback($subject, $value)) {
5755
return new Error($this->getMessage(), $this->getName());
5856
}
5957
}

tests/src/Assert/CallbackTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ class CallbackTest extends AbstractTestCase
1212
{
1313
public function dataExecute()
1414
{
15-
$closure = function($value) {
16-
return $value === 'test12';
17-
};
18-
1915
return [
20-
['10', 'is_numeric', true],
21-
['black', 'is_numeric', 'test is invalid'],
22-
[10, 'is_int', true],
23-
['black', 'is_int', 'test is invalid'],
24-
['test12', $closure, true],
25-
['test122', $closure, 'test is invalid'],
16+
['10', function ($subject) {return $subject->test === '10';}, true],
17+
['black', function ($subject, $value) {return $value === 'black';}, true],
18+
['black', function () {return true;}, true],
19+
['black', function () {return false;}, 'test is invalid'],
2620
];
2721
}
2822

@@ -43,14 +37,11 @@ public function testExecute($value, $callback, $expected)
4337
*/
4438
public function testConstruct()
4539
{
46-
$assertion = new Callback('test', 'is_numeric', 'custom message');
40+
$closure = function ($subject) {return $subject['test'] === '10';};
41+
$assertion = new Callback('test', $closure, 'custom message');
4742

48-
$this->assertEquals('is_numeric', $assertion->getCallback());
43+
$this->assertEquals($closure, $assertion->getCallback());
4944
$this->assertEquals('test', $assertion->getName());
5045
$this->assertEquals('custom message', $assertion->getMessage());
51-
52-
$this->setExpectedException('InvalidArgumentException');
53-
54-
new Callback('test', 'invalid callback');
5546
}
5647
}

0 commit comments

Comments
 (0)