Skip to content

Commit be67ec1

Browse files
committed
Callback Functionality
1 parent a1b35c9 commit be67ec1

File tree

3 files changed

+101
-13
lines changed

3 files changed

+101
-13
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ In what order to validate the classes (methods or properties first), and what t
116116
- [x] Custom property and method names for the exceptions
117117
- [x] Severity levels
118118
- [x] Debug explorer
119+
- [x] Callback execution*
119120

120121

121122
### *The custom validation should be declared as static in a validation class
@@ -149,6 +150,13 @@ You can pass additional arguments to use in the validation function, but the fir
149150
150151
```
151152

153+
### *Callback execution
154+
155+
Call backs are executed when the validation does not trigger and Exception (based on SEVERITY_LEVEL)
156+
157+
```$forceCallback``` can be used and has an effect only with EXCEPTION_SEVERITY_LEVEL to force callback after an EXCEPTION trigger
158+
159+
152160
### Severity
153161
Add option to make the failed validation throw NOTICE|WARNING|ERROR
154162
```

RMValidator/Validators/MasterValidator.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,40 @@
1818

1919
final class MasterValidator {
2020

21-
public static function validate(object $target, OptionsModel $passedOptionsModel = null) {
21+
public static function validate(object $target, OptionsModel $passedOptionsModel = null, callable $callback = null, $forceCallback = false) {
2222
$optionsModel = (empty($passedOptionsModel)) ? new OptionsModel() : $passedOptionsModel;
23+
if (count($optionsModel->getOrAttributes()) != count(array_unique($optionsModel->getOrAttributes()))) {
24+
throw new Exception("There is a duplicate OR attribute, the array of the OR attributes must be unique");
25+
}
2326
$className = get_class($target);
2427
$reflectionClass = new ReflectionClass($className);
2528
$methods = $reflectionClass->getMethods();
2629
$properties = $reflectionClass->getProperties();
2730
$constants = $reflectionClass->getConstants();
28-
foreach($optionsModel->getOrderOfValidation() as $order) {
29-
switch($order) {
30-
case ValidationOrderEnum::METHODS:
31-
MasterValidator::validateMethods($methods, $className, $target, $optionsModel->getExcludedMethods(), $passedOptionsModel->getOrAttributes(), $passedOptionsModel->getGlobalSeverityLevel());
32-
break;
33-
case ValidationOrderEnum::PROPERTIES:
34-
MasterValidator::validateProperties($properties, $className, $target, $optionsModel->getExcludedProperties(), $passedOptionsModel->getOrAttributes(), $passedOptionsModel->getGlobalSeverityLevel());
35-
break;
36-
case ValidationOrderEnum::CONSTANTS:
37-
MasterValidator::validateConstants($constants, $className, $target, $optionsModel->getExcludedProperties(), $passedOptionsModel->getOrAttributes(), $passedOptionsModel->getGlobalSeverityLevel());
38-
break;
39-
31+
try {
32+
foreach($optionsModel->getOrderOfValidation() as $order) {
33+
switch($order) {
34+
case ValidationOrderEnum::METHODS:
35+
MasterValidator::validateMethods($methods, $className, $target, $optionsModel->getExcludedMethods(), $optionsModel->getOrAttributes(), $optionsModel->getGlobalSeverityLevel());
36+
break;
37+
case ValidationOrderEnum::PROPERTIES:
38+
MasterValidator::validateProperties($properties, $className, $target, $optionsModel->getExcludedProperties(), $optionsModel->getOrAttributes(), $optionsModel->getGlobalSeverityLevel());
39+
break;
40+
case ValidationOrderEnum::CONSTANTS:
41+
MasterValidator::validateConstants($constants, $className, $target, $optionsModel->getExcludedProperties(), $optionsModel->getOrAttributes(), $optionsModel->getGlobalSeverityLevel());
42+
break;
43+
}
44+
}
45+
if (!$forceCallback && !is_null($callback)) {
46+
$callback();
47+
}
48+
}
49+
catch(Exception $e) {
50+
throw $e;
51+
}
52+
finally {
53+
if ($forceCallback && !is_null($callback)) {
54+
$callback();
4055
}
4156
}
4257

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
4+
use PHPUnit\Framework\TestCase;
5+
use RMValidator\Attributes\PropertyAttributes\Numbers\RangeAttribute;
6+
use RMValidator\Validators\MasterValidator;
7+
8+
class ValidationCallbacksTest extends TestCase {
9+
10+
/**
11+
* @doesNotPerformAssertions
12+
*/
13+
public function testMasterValidator_shouldExecuteCallbackOnSuccessfullValidation()
14+
{
15+
$controll = 0;
16+
MasterValidator::validate(new SuccessfullValidityTest(), null, function() use (&$controll) {
17+
$controll++;
18+
});
19+
$this->assertEquals(1, $controll);
20+
}
21+
22+
public function testMasterValidator_shouldNotExecuteCallbackOnUnuccessfullValidation()
23+
{
24+
$controll = 0;
25+
try {
26+
MasterValidator::validate(new UnsuccessfullValidityTest(), null, function() use (&$controll) {
27+
$controll++;
28+
});
29+
}
30+
catch(Exception $e) {
31+
$this->assertEquals(0, $controll);
32+
}
33+
34+
}
35+
36+
public function testMasterValidator_shouldExecuteCallbackOnUnuccessfullValidationButForce()
37+
{
38+
$controll = 0;
39+
try {
40+
MasterValidator::validate(new UnsuccessfullValidityTest(), null, function() use (&$controll) {
41+
$controll++;
42+
}, true);
43+
}
44+
catch(Exception $e) {
45+
$this->assertEquals(1, $controll);
46+
}
47+
48+
}
49+
}
50+
51+
class SuccessfullValidityTest {
52+
53+
#[RangeAttribute(from:10, to:50)]
54+
public static function successfulMethod() {
55+
return 40;
56+
}
57+
}
58+
59+
class UnsuccessfullValidityTest {
60+
61+
#[RangeAttribute(from:10, to:50)]
62+
public static function unSuccessfulMethod() {
63+
return 60;
64+
}
65+
}

0 commit comments

Comments
 (0)