-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.php
executable file
·87 lines (70 loc) · 1.96 KB
/
static.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
#!/usr/bin/php -q
<?php
use vijinho\Enums\Enum;
require_once "../lib/autoload.php";
class FilmRatings extends Enum
{
protected static $capitalize = true;
protected static $values = [
'Blade Runner' => 10,
'Dark Star' => 7,
'Indiana Jones' => 8,
'Species' => 6
];
}
// note that:
// 1) the spaces still exist!
// 2) the keys are not capitalised
echo "Example A\n";
var_dump(FilmRatings::values());
// SO use fix keys when calling statically
echo "Example B\n";
FilmRatings::fixKeys();
var_dump(FilmRatings::values());
// get an enum value statically, directly
// case-insensitive
echo "Example C\n";
echo FilmRatings::BLADE_Runner();
echo FilmRatings::INDIANA_JONES();
// output as JSON-encoded string
echo "Example D\n";
echo FilmRatings::toString();
// dump values (same as for var_dump on object)
echo "Example E\n";
var_dump(FilmRatings::var_dump());
// add a new value
echo "Example F\n";
var_dump(FilmRatings::add(['Star Wars' => 8]));
// you can't change a value once it's set!
echo "Example G\n";
FilmRatings::add(['Star Wars' => 10]);
// remember the values are static so NEW will return the same
echo "Example H\n";
$f = new FilmRatings();
echo $f;
// set new keys to not be capitalised
echo "Example I\n";
FilmRatings::capitalize(false);
FilmRatings::add(['Battle Beyond The Stars' => 6.5]);
echo $f; // remember this has the same data still!
// show all keys
echo "Example J\n";
print_r(FilmRatings::keys());
// get a value by key
echo "Example K\n";
print_r(FilmRatings::value('INDIANA_JONES'));
// get a key by value
echo "Example L\n";
print_r(FilmRatings::key(6));
// get a key by duplicate value
echo "Example M\n";
FilmRatings::add(['Space Hunter' => 6.5]);
// we now have 2 films rated 6.5 so an array is returned of 2 elements with each key
print_r(FilmRatings::key(6.5));
// get an invalid key throws an exception
echo "Example P\n";
try {
FilmRatings::key(2);
} catch (\InvalidArgumentException $ex) {
print_r($ex);
}