Skip to content

Commit 7fc7785

Browse files
committed
Enable syntax highlighting for code blocks
1 parent 1afe8f6 commit 7fc7785

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ It's an abstract class that needs to be extended to use it.
2828

2929
## Basics
3030

31+
```php
3132
use MabeEnum\Enum;
3233

3334
// define an own enumeration class
@@ -44,19 +45,19 @@ It's an abstract class that needs to be extended to use it.
4445
const STR = 'string';
4546
const FLOAT = 0.123;
4647
}
47-
48+
4849
// different ways to instantiate an enumerator
4950
$status = UserStatus::get(UserStatus::ACTIVE);
5051
$status = UserStatus::ACTIVE();
5152
$status = UserStatus::getByName('ACTIVE');
5253
$status = UserStatus::getByOrdinal(1);
53-
54+
5455
// available methods to get the selected entry
5556
$status->getValue(); // returns the selected constant value
5657
$status->getName(); // returns the selected constant name
5758
$status->getOrdinal(); // returns the ordinal number of the selected constant
5859
(string) $status; // returns the selected constant name
59-
60+
6061
// same enumerators (of the same enumeration class) holds the same instance
6162
UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
6263
UserStatus::get(UserStatus::DELETED) != UserStatus::INACTIVE()
@@ -66,10 +67,11 @@ It's an abstract class that needs to be extended to use it.
6667
UserStatus::ACTIVE()->is(UserStatus::ACTIVE()); // true
6768
UserStatus::ACTIVE()->is(UserStatus::DELETED); // false
6869
UserStatus::ACTIVE()->is(UserStatus::DELETED()); // false
69-
70+
```
7071

7172
## Type-Hint
72-
73+
74+
```php
7375
use MabeEnum\Enum;
7476

7577
class User
@@ -90,23 +92,27 @@ It's an abstract class that needs to be extended to use it.
9092
return $this->status;
9193
}
9294
}
95+
```
9396

9497
### Type-Hint issue
9598

9699
Because in normal OOP the above example allows `UserStatus` and types inherited from it.
97100

98101
Please think about the following example:
99102

103+
```php
100104
class ExtendedUserStatus
101105
{
102106
const EXTENDED = 'extended';
103107
}
104108

105109
$user->setStatus(ExtendedUserStatus::EXTENDED());
110+
```
106111

107112
Now the setter receives a status it doesn't know about but allows it.
108113
If your `User` class doesn't allow it the following is the recommanded way:
109114

115+
```php
110116
class User
111117
{
112118
// ...
@@ -116,6 +122,7 @@ If your `User` class doesn't allow it the following is the recommanded way:
116122
}
117123
// ...
118124
}
125+
```
119126

120127
Now you are 100% sure to work with an exact instace of `UserStatus`.
121128

@@ -128,6 +135,7 @@ An ```EnumMap``` maps enumerators of the same type to data assigned to.
128135

129136
Internally the ```EnumMap``` is based of ```SplObjectStorage```.
130137

138+
```php
131139
use MabeEnum\EnumMap;
132140

133141
// create a new EnumMap
@@ -148,7 +156,7 @@ Internally the ```EnumMap``` is based of ```SplObjectStorage```.
148156
// define key and value used for iteration
149157
$enumSet->setFlags(EnumMap::KEY_AS_NAME | EnumMap::CURRENT_AS_DATA);
150158
var_dump(iterator_to_array($enumSet)); // array('ACTIVE' => 'aktiv');
151-
159+
```
152160

153161
## EnumSet
154162

@@ -160,6 +168,7 @@ The maximun number of enumerators are limited by the size of an integer.
160168

161169
Enumerators will be ordered by the ordinal number.
162170

171+
```php
163172
use MabeEnum\EnumSet;
164173

165174
// create a new EnumSet
@@ -176,7 +185,7 @@ Enumerators will be ordered by the ordinal number.
176185

177186
// iterate
178187
var_dump(iterator_to_array($enumSet)); // array(0 => UserStatus{$value=1});
179-
188+
```
180189

181190
# Why not ```SplEnum```
182191

0 commit comments

Comments
 (0)