You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -225,18 +225,171 @@ $colorFromValue = Color::from("FF0000"); // Returns RED
225
225
$colorOrNull = Color::tryFrom("INVALID"); // Returns null (when the value doesn't exist)
226
226
```
227
227
228
+
## Using EnumCase for Direct Access
229
+
230
+
When you call `add_case()` on an enum entity, it returns an `EnumCase` instance that you can use for direct access to that case later. This is more efficient than looking up the enum case by name each time you need it.
231
+
232
+
Here's an example showing how to use EnumCase within static methods of the enum:
233
+
234
+
```rust,no_run
235
+
use phper::{
236
+
modules::Module,
237
+
php_get_module,
238
+
enums::EnumEntity,
239
+
classes::Visibility,
240
+
alloc::ToRefOwned,
241
+
};
242
+
243
+
#[php_get_module]
244
+
pub fn get_module() -> Module {
245
+
let mut module = Module::new(
246
+
env!("CARGO_CRATE_NAME"),
247
+
env!("CARGO_PKG_VERSION"),
248
+
env!("CARGO_PKG_AUTHORS"),
249
+
);
250
+
251
+
// Create a pure enum
252
+
let mut pure_enum_entity = EnumEntity::new("PureEnum");
253
+
254
+
// Store references to enum cases when adding them
255
+
let one_case = pure_enum_entity.add_case("ONE", ());
256
+
let _two_case = pure_enum_entity.add_case("TWO", ());
This creates PHP enums with static methods that can access specific cases:
296
+
297
+
```php
298
+
enum PureEnum {
299
+
case ONE;
300
+
case TWO;
301
+
302
+
public static function getOneCase(): self {
303
+
return self::ONE;
304
+
}
305
+
}
306
+
307
+
enum IntEnum: int {
308
+
case LOW = 10;
309
+
case HIGH = 100;
310
+
311
+
public static function getLowCase(): self {
312
+
return self::LOW;
313
+
}
314
+
}
315
+
```
316
+
317
+
## Using Enum::from_name
318
+
319
+
If you don't have direct access to the EnumCase, you can use `Enum::from_name()` to get an enum by its name, and then use `get_case()` or `get_mut_case()` to access specific cases:
let case_name = args[0].expect_z_str()?.to_string_lossy();
338
+
339
+
// Use Enum::from_name to get the enum
340
+
let mut enum_obj = Enum::from_name("DynamicEnum");
341
+
342
+
// Try to get the requested case
343
+
let case = unsafe { enum_obj.get_mut_case(&case_name)? };
344
+
let result = case.to_ref_owned();
345
+
346
+
phper::ok(result)
347
+
});
348
+
349
+
enum_entity
350
+
}
351
+
```
352
+
353
+
> **Important**: The `get_case()` and `get_mut_case()` methods on `Enum` are marked as unsafe because they can cause segmentation faults if the case doesn't exist.
354
+
355
+
## Bound Enum
356
+
357
+
You can use the `bound_enum()` method to get a reference to the enum that can be used in methods or functions:
358
+
359
+
```rust,no_run
360
+
use phper::{enums::EnumEntity, classes::Visibility, alloc::ToRefOwned};
361
+
362
+
pub fn make_status_enum() -> EnumEntity {
363
+
let mut enum_entity = EnumEntity::new("Status");
364
+
enum_entity.add_case("Active", ());
365
+
enum_entity.add_case("Inactive", ());
366
+
367
+
// Get a reference to the enum that will be created
0 commit comments