33namespace ProgrammatorDev \YetAnotherPhpValidator \Rule ;
44
55use ProgrammatorDev \YetAnotherPhpValidator \Exception \RangeException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \UnexpectedComparableException ;
67use ProgrammatorDev \YetAnotherPhpValidator \Exception \UnexpectedValueException ;
7- use ProgrammatorDev \YetAnotherPhpValidator \Rule \Util \AssertIsComparableTrait ;
8+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \Util \ComparableTrait ;
89use ProgrammatorDev \YetAnotherPhpValidator \Validator ;
910use Symfony \Component \OptionsResolver \OptionsResolver ;
1011
1112class Range extends AbstractRule implements RuleInterface
1213{
13- use AssertIsComparableTrait ;
14+ use ComparableTrait ;
1415
1516 private array $ options ;
1617
@@ -31,12 +32,21 @@ public function __construct(
3132
3233 public function assert (mixed $ value , string $ name ): void
3334 {
34- $ this ->assertIsComparable ($ this ->minConstraint , $ this ->maxConstraint );
35+ $ minConstraint = $ this ->convertToComparable ($ this ->minConstraint );
36+ $ maxConstraint = $ this ->convertToComparable ($ this ->maxConstraint );
37+ $ value = $ this ->convertToComparable ($ value );
38+
39+ if (!$ this ->isComparable ($ minConstraint , $ maxConstraint )) {
40+ throw new UnexpectedComparableException (
41+ get_debug_type ($ minConstraint ),
42+ get_debug_type ($ maxConstraint )
43+ );
44+ }
3545
3646 if (
3747 !Validator
38- ::greaterThan ($ this -> minConstraint )
39- ->validate ($ this -> maxConstraint )
48+ ::greaterThan ($ minConstraint )
49+ ->validate ($ maxConstraint )
4050 ) {
4151 throw new UnexpectedValueException (
4252 'Max constraint value must be greater than min constraint value. '
@@ -45,17 +55,17 @@ public function assert(mixed $value, string $name): void
4555
4656 if (
4757 !Validator
48- ::greaterThanOrEqual ($ this -> minConstraint )
49- ->lessThanOrEqual ($ this -> maxConstraint )
58+ ::greaterThanOrEqual ($ minConstraint )
59+ ->lessThanOrEqual ($ maxConstraint )
5060 ->validate ($ value )
5161 ) {
5262 throw new RangeException (
5363 message: $ this ->options ['message ' ],
5464 parameters: [
5565 'value ' => $ value ,
5666 'name ' => $ name ,
57- 'minConstraint ' => $ this -> minConstraint ,
58- 'maxConstraint ' => $ this -> maxConstraint
67+ 'minConstraint ' => $ minConstraint ,
68+ 'maxConstraint ' => $ maxConstraint
5969 ]
6070 );
6171 }
0 commit comments