-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.php
219 lines (195 loc) · 4.92 KB
/
example.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* php-enum
*
* This file provides an example implementation of crussell52/enum/TEnum in the form of an Enum
* implementation which defines available colors. Various operations using the Color enum are then
* executed.
*
* @author Chris Russell ([email protected])
* @copyright 2014 Chris Russell (https://www.github.com/crussell52/)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://www.github.com/crussell52/php-enum
*/
use crusell52\enum\TEnum;
// YOU MUST UPDATE THIS TO RUN THE EXAMPLE!!
include ('path/to/crussell52/enum/TEnum.php');
/**
* This class serves as an example of an Enum implementation. Each value represents a color which
* is available for use.
*
* @method static Color RED()
* @method static Color GREEN()
* @method static Color BLUE()
* @method static Color YELLOW()
* @method static Color WHITE()
* @method static Color BLACK()
*/
class Color
{
use TEnum;
/**
* The amount of red which is used to create this color (0-255).
*
* @var int
*/
private $_red;
/**
* The amount of green which is used to create this color (0-255).
*
* @var int
*/
private $_green;
/**
* The amount of blue which is used to create this color (0-255).
*
* @var int
*/
private $_blue;
/**
* @inheritDoc
*/
protected static function _initializeDefinitions()
{
// Definition provides red, green, and blue value (in that order)
return [
'RED' => [255, 0, 0],
'GREEN' => [0, 255, 0],
'BLUE' => [0, 0, 255],
'YELLOW' => [255, 255, 0],
'WHITE' => [255, 255, 255],
'BLACK' => [0, 0, 0],
];
}
/**
* @inheritDoc
*/
protected function _populate(array $args)
{
// Definition provides red, green, and blue value (in that order)
$this->_red = $args[0];
$this->_green = $args[1];
$this->_blue = $args[2];
}
/**
* Provides the amount of red used to create this color (0-255).
*
* @return int
*/
public function getRedValue()
{
return $this->_red;
}
/**
* Provides the amount of green used to create this color (0-255).
*
* @return int
*/
public function getGreenValue()
{
return $this->_green;
}
/**
* Provides the amount of blue used to create this color (0-255).
*
* @return int
*/
public function getBlueValue()
{
return $this->_blue;
}
/**
* Provides the html code for this color.
*
* @return string
*/
public function toHtmlCode()
{
return '#' . str_pad(dechex($this->_red), 2, '0', STR_PAD_LEFT) . str_pad(dechex($this->_green), 2, '0', STR_PAD_LEFT) . str_pad(dechex($this->_blue), 2, '0', STR_PAD_LEFT);
}
}
/**
* Demonstrate type hinting against Enum implementation by saying something nice about the
* received Color.
*
* @param Color $color The color to say something nice about.
*
* @return void
*/
function saySomethingNice(Color $color)
{
switch ($color)
{
case Color::RED():
$something_nice = ' is like the love of a rose petal.';
break;
case Color::BLUE():
$something_nice = ' is like a deep sea on a sunny day.';
break;
default:
$something_nice = ' is a wonderful color.';
break;
}
echo 'The color ' . $color->getName() . $something_nice . "<br />\n";
}
/**
* Demonstrate looping over available Enum values by listing all available colors names and their
* html code.
*
* @return void
*/
function listHtmlCodes()
{
foreach (Color::getValues() as $color)
{
/** @var Color $color */
echo $color->getName() . "({$color->getOrdinal()}: " . $color->toHtmlCode() . "<br />\n";
}
}
/**
* Demonstrate explicit equality of Enum values with the same name.
*
* @param Color $color1 The first color to compare.
* @param Color $color2 The second color to compare.
*/
function compareColors(Color $color1, Color $color2)
{
if ($color1 === $color2)
{
$comparison_result = ' is ';
}
else
{
$comparison_result = ' is not ';
}
// Leverage built-in __toString() implementation for $color2.
echo $color1->getName() . $comparison_result . $color2 . "<br />\n";
}
// Run demonstrations.
echo "<h2>Say Nice Stuff</h2>";
saySomethingNice(Color::BLUE());
saySomethingNice(Color::RED());
saySomethingNice(Color::YELLOW());
echo "<h2>List html codes</h2>";
listHtmlCodes();
echo "<h2>Compare colors</h2>";
compareColors(Color::RED(), Color::BLUE());
compareColors(Color::YELLOW(), Color::YELLOW());
$serialCopyColor = unserialize(serialize(Color::YELLOW()));
if ($serialCopyColor === Color::YELLOW()) {
echo "\nunexected === match on serialized/deserialized value\n";
}
else {
echo "\n Expected === mismatch on serialized/deserialized value\n";
}
switch ($serialCopyColor) {
case Color::YELLOW():
echo "case match!\n";
break;
case Color::BLACK():
echo "case false positive!\n";
break;
default:
echo "case false negative!\n";
break;
}