Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Oct 4, 2024
1 parent 08017b9 commit 2a39ba8
Showing 1 changed file with 90 additions and 12 deletions.
102 changes: 90 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ enum BackedEnum: int
}
```

We can see the keys belonging to an enum by calling the `keys()` method:

```php
// ['name', 'value', 'color', 'isOdd']
$keys = BackedEnum::keys();
```

Keys provide extra information for an enum and can also be leveraged for the [hydration](#-hydration), [elaboration](#-enum-operations) and [collection](#-cases-collection) of cases.


Expand Down Expand Up @@ -286,27 +279,112 @@ All the [enum operations listed above](#-enum-operations) are also available whe

### 🪄 Magic

Enums can implement magic methods to be invoked or to handle calls to inaccessible methods. By default when calling an inaccessible static method, the value of the case matching the missing method is returned:
Enums can implement magic methods to be invoked or to handle calls to inaccessible methods. By default when calling an inaccessible static method, the name or value of the case matching the missing method is returned:

```php
PureEnum::One(); // 'One'
PureEnum::isBacked(); // false

BackedEnum::isPure(); // false
BackedEnum::isBacked(); // true
BackedEnum::One(); // 1
```

To improve the autocompletion of our IDE, we can add some method annotations to our enums:

```php
/**
* @method static int One()
* @method static int Two()
* @method static int Three()
*/
enum BackedEnum: int
{
use Enumerates;

case One = 1;
case Two = 2;
case Three = 3;
}
```

By default, we can also obtain the name or value of a case by simply invoking it:

```php
$case = PureEnum::One;
$case(); // 'One'

$case = BackedEnum::One;
$case(); // 1
```

When calling an inaccessible method of a case, by default the value of the key matching the missing method is returned:

```php
PureEnum::One->color(); // 'red'

BackedEnum::One->color(); // 'red'
```

To improve the autocompletion of our IDE, we can add some method annotations to our enums:

```php
/**
* @method string color()
*/
enum BackedEnum: int
{
use Enumerates;

#[Meta(color: 'red')]
case One = 1;

#[Meta(color: 'green')]
case Two = 2;

#[Meta(color: 'blue')]
case Three = 3;
}
```

Depending on our needs, we can customize the default magic behavior of all enums in our application and run our own custom logic when invoking a case or calling inaccessible methods:

```php
use Cerbero\Enum\Enums;

// define the logic to run when calling an inaccessible method of an enum
Enums::onStaticCall(function(string $enum, string $name, array $arguments) {
// $enum is the fully qualified name of the enum that called the inaccessible method
// $name is the inaccessible method name
// $arguments are the parameters passed to the inaccessible method
});

// define the logic to run when calling an inaccessible method of a case
Enums::onCall(function(object $case, string $name, array $arguments) {
// $case is the instance of the case that called the inaccessible method
// $name is the inaccessible method name
// $arguments are the parameters passed to the inaccessible method
});

// define the logic to run when invoking a case
Enums::onInvoke(function(object $case, mixed ...$arguments) {
// $case is the instance of the case that is being invoked
// $arguments are the parameters passed when invoking the case
});
```


### 🤳 Self-awareness

These methods determine whether an enum is pure or backed:
Finally, the following methods can be useful for inspecting enums or auto-generating code:

```php
PureEnum::isPure(); // true
PureEnum::isBacked(); // false
PureEnum::keys(); // ['color', 'shape', 'isOdd']
PureEnum::One->value(); // 'One'

BackedEnum::isPure(); // false
BackedEnum::isBacked(); // true
BackedEnum::keys(); // ['color', 'shape', 'isOdd']
BackedEnum::One->value(); // 1
```

## 📆 Change log
Expand Down

0 comments on commit 2a39ba8

Please sign in to comment.