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,19 +225,20 @@ $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
-
## Accessing Enums from Rust Code
228
+
## Using EnumCase for Direct Access
229
229
230
-
After registering an enum, you might need to access it from Rust code. `PHPER` provides two ways to do this:
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
231
232
-
1. Using the returned `Enum` instance when registering the enum
233
-
2. Using `Enum::from_name` to look up an enum by name
234
-
235
-
### Using the Returned `Enum` Instance
236
-
237
-
When you register an enum using `module.add_enum()`, it returns an `Enum` instance that you can save and use later.
232
+
Here's an example showing how to use EnumCase within static methods of the enum:
238
233
239
234
```rust,no_run
240
-
use phper::{modules::Module, php_get_module, enums::{EnumEntity, Enum}};
This creates PHP enums with static methods that can access specific cases:
270
296
271
-
If you don't have the original `Enum` instance, you can use `Enum::from_name` to look up an enum by its name.
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:
272
320
273
321
```rust,no_run
274
-
use phper::{enums::Enum, modules::Module, php_get_module};
let case_name = args[0].expect_z_str()?.to_string_lossy();
288
338
289
-
// Get the case name from the function arguments
290
-
let case_name = args[0].as_z_str()?.to_str()?;
339
+
// Use Enum::from_name to get the enum
340
+
let mut enum_obj = Enum::from_name("DynamicEnum");
291
341
292
-
// Get the requested enum case
293
-
let case = status_enum.get_case(case_name)?;
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();
294
345
295
-
Ok::<_, phper::Error>(case)
346
+
phper::ok(result)
296
347
});
297
-
298
-
module
348
+
349
+
enum_entity
299
350
}
300
351
```
301
352
302
-
## Getting Enum Cases
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.
303
354
304
-
Once you have an `Enum` instance, you can use the `get_case` method to access specific enum cases:
355
+
## Bound Enum
305
356
306
-
```rust,no_run
307
-
// Get a case from a pure enum
308
-
let status_enum = Enum::from_name("Status");
309
-
let active_case = status_enum.get_case("ACTIVE")?;
310
-
311
-
// Get a case from a backed enum
312
-
let level_enum = Enum::from_name("Level");
313
-
let high_level = level_enum.get_case("HIGH")?;
314
-
```
315
-
316
-
If you need to modify an enum case's properties, you can use `get_case_mut` to get a mutable reference:
357
+
You can use the `bound_enum()` method to get a reference to the enum that can be used in methods or functions:
317
358
318
359
```rust,no_run
319
-
// Get a mutable reference to an enum case
320
-
let mut status_enum = Enum::from_name("Status");
321
-
let mut active_case = status_enum.get_case_mut("ACTIVE")?;
360
+
use phper::{enums::EnumEntity, classes::Visibility, alloc::ToRefOwned};
322
361
323
-
// Now you can modify the case object if needed
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