class-learndash-dto-property-validator-possible-values.php
1.36 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* This class provides the easy way to validate DTO properties.
*
* @since 4.5.0
*
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Learndash_DTO_Property_Validator' ) ) {
/**
* DTO property validator class.
*
* @since 4.5.0
*/
class Learndash_DTO_Property_Validator_Possible_Values implements Learndash_DTO_Property_Validator {
/**
* Possible values.
*
* @since 4.5.0
*
* @var array<mixed>
*/
private $valid_values;
/**
* Constructor.
*
* @since 4.5.0
*
* @param array<mixed> $valid_values Possible valid values.
*
* @return void
*/
public function __construct( array $valid_values ) {
$this->valid_values = $valid_values;
}
/**
* Validates if a property value is one of the possible values.
*
* @since 4.5.0
*
* @param mixed $value Value.
*
* @return Learndash_DTO_Property_Validation_Result
*/
public function validate( $value ): Learndash_DTO_Property_Validation_Result {
if ( ! in_array( $value, $this->valid_values, true ) ) {
return Learndash_DTO_Property_Validation_Result::invalid(
sprintf(
'Value %s must be equal to one of the following: "%s".',
wp_json_encode( $value ),
implode( ', ', $this->valid_values )
)
);
}
return Learndash_DTO_Property_Validation_Result::valid();
}
}
}