-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.php
More file actions
executable file
·86 lines (73 loc) · 1.71 KB
/
quickstart.php
File metadata and controls
executable file
·86 lines (73 loc) · 1.71 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
#!/usr/bin/php -q
<?php
use vijinho\Enums\Enum;
require_once "../lib/autoload.php";
$e = new Enum(); // new empty enum
$e([
'mercedes' => 'luxury',
'ferrari' => 'sports',
'BMW'
]);
echo $e; // outputs to JSON serialized string by magic!
/*
{
"mercedes": "luxury",
"ferrari": "sports",
"BMW": "BMW"
}
*/
$e->add(['BMW' => 'Bob Marley & The Wailers']); // cannot override existing value
echo $e->value('BMW'); // BMW
$e->capitalize(true);
$e->add('Audi');
echo $e;
/*
{
"MERCEDES": "luxury",
"FERRARI": "sports",
"BMW": "BMW",
"AUDI": "Audi"
}
*/
echo $e->MERCEDES; // luxury
echo $e->FERRARI(); // sports
echo Enum::AUDI(); // Audi
class Fruits extends Enum
{
protected static $values = [
'apple' => 'Apple',
'pear' => 'Pear',
'banana' => 'Banana',
'orange' => 'Orange',
'grapefruit' => 'Grapefruit',
'tomato' => 'Cucumber',
];
}
// get an enum value by key
echo Fruits::apple(); // Apple
echo Fruits::APPLE(); // Apple
// add a key => value to the enum
Fruits::add(['STRAWBERRY' => 'Strawberry', 'Avocado' => 'Avocado']);
// alternative way to fetch a value by key
echo Fruits::value('strawberry'); // Strawberry
// return the key for a value
echo Fruits::key('cucumber'); // tomato
// return all fruits
print_r(Fruits::values());
/*
(
[apple] => Apple
[pear] => Pear
[banana] => Banana
[orange] => Orange
[grapefruit] => Grapefruit
[tomato] => Cucumber
[STRAWBERRY] => Strawberry
[Avocado] => Avocado
)
*/
$f = new Fruits;
$f(['mango']); // add a new fruit - magic!
$f(['pineapple' => 'Pineapple']); // add another new fruit
$f->add(['potato' => 'Not a fruit']);
var_dump($f); // special var_dump magic!