class-learndash-dto-property-validation-result.php
1.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* The object to return the result of a DTO validation.
*
* @since 4.5.0
*
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Learndash_DTO_Property_Validation_Result' ) ) {
/**
* DTO property validation result.
*
* @since 4.5.0
*/
class Learndash_DTO_Property_Validation_Result {
/**
* True if validation passed, false otherwise.
*
* @since 4.5.0
*
* @var bool
*/
private $is_valid;
/**
* Error message if validation is not passed, an empty string otherwise.
*
* @since 4.5.0
*
* @var string
*/
private $message;
/**
* Constructor. Overriding the constructor in child classes and direct instantiating is disallowed.
*
* @since 4.5.0
*
* @param bool $is_valid Is validation passed.
* @param string $message Error message if validation is not passed.
*
* @return void
*/
final protected function __construct( bool $is_valid, string $message = '' ) {
$this->is_valid = $is_valid;
$this->message = $message;
}
/**
* Returns true if validation passed, false otherwise.
*
* @since 4.5.0
*
* @return bool
*/
public function is_valid(): bool {
return $this->is_valid;
}
/**
* Returns a message.
*
* @since 4.5.0
*
* @return string
*/
public function get_message(): string {
return $this->message;
}
/**
* Creates a valid result.
*
* @since 4.5.0
*
* @return self
*/
public static function valid(): self {
return new self( true );
}
/**
* Creates an invalid result.
*
* @since 4.5.0
*
* @param string $message Error message.
*
* @return self
*/
public static function invalid( string $message ): self {
return new self( false, $message );
}
}
}