Option.php
1.65 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
101
102
103
104
<?php
/**
* SearchWP Option.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP;
/**
* Class Option is responsible for storing a value with an associated label.
*
* @since 4.0
*/
class Option implements \JsonSerializable {
/**
* The value of this Option.
*
* @since 4.0
* @var mixed
*/
private $value;
/**
* The label of this Option.
*
* @since 4.0
* @var string
*/
private $label;
/**
* The icon of this Option.
*
* @since 4.0
* @var string
*/
private $icon;
/**
* Option constructor.
*
* @since 4.0
* @param mixed $value The value to store.
* @param string $label The label to use.
*/
function __construct( $value, $label = '', $icon = '' ) {
// If no label was provided, generate one from the submitted value.
if ( empty( $label ) || ! is_string( $label ) ) {
$label = substr( (string) $value, 0, 32 );
}
$this->label = sanitize_text_field( $label );
$this->value = $value;
$this->icon = $icon;
}
/**
* Getter for label.
*
* @since 4.0
* @return string The label.
*/
public function get_label() {
return $this->label;
}
/**
* Getter for value.
*
* @since 4.0
* @return mixed The value.
*/
public function get_value() {
return $this->value;
}
/**
* Getter for icon.
*
* @since 4.0
* @return string The icon.
*/
public function get_icon() {
return $this->icon;
}
/**
* Provides the model to use when representing this Source as JSON.
*
* @since 4.0
* @return array
*/
public function jsonSerialize(): array {
return [
'label' => $this->label,
'value' => $this->value,
'icon' => $this->get_icon(),
];
}
}