Skip to content

Commit

Permalink
Added helper getlabel
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-trush committed Oct 22, 2024
1 parent 5616cf2 commit 086d606
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
86 changes: 82 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ php artisan vendor:publish --tag="enum-helpers"
```

Default config

```php
return [
'enum_locations' => [
'app/Enums' => '\\App\\Enums\\',
],

'label' => [
'prefix' => null,
'namespace' => null,
],

'post_migrate' => true,

'js_objects_file' => 'resources/js/enums.js',
Expand All @@ -33,9 +39,11 @@ return [
1. `enum_locations` - path where enums located. Key is directory with enums, value - namespace for specified directory
2. `post_migrate` - enable or disable post migrate event listener
3. `js_objects_file` - path for generated js output
4. `label.prefix` - get default prefix for translations of enum fields
5. `label.namespace` - get default prefix for translations of enum namespace (when using as part of own package)


## Migration helper
## Available helpers
### Migration helper

The way easy to add all enums to database column.

Expand Down Expand Up @@ -80,7 +88,7 @@ return new class extends Migration
}
```

## Update enum columns in DB
### Update enum columns in DB

Artisan command allows update available(possible) values for specific enum column.

Expand Down Expand Up @@ -132,7 +140,7 @@ return [
```


## Convert PHP Enums to JS objects
### Convert PHP Enums to JS objects

Artisan command allows generate js objects based on enums

Expand Down Expand Up @@ -178,6 +186,76 @@ return [
]
```

### Label helper

Label helper allows to transform an enum instance into a textual label.
This is useful for displaying human-readable translatable enum values in your UI.

```php
<?php

namespace App\Enums

use IsapOu\EnumHelpers\Concerns\HasLabel;

enum ExampleEnum: string
{
use HasLabel

case ENUM_ONE = 'enum_one';
case ENUM_TWO = 'enum_two';
}
```

and get textual label

```php
ExampleEnum::ENUM_ONE->getLabel()
```

By default it will try to find translation with key e.g. `ExampleEnum.ENUM_ONE`
1. `ExampleEnum` - class name
2. `ENUM_ONE` - enum item name

Method `getLabel` has two optional parameters:
1. `prefix` - Allows prepend prefix for translation key
2. `namespace` - Allows to prepend namespace. Suitable during own package development

There is possibility to define `prefix` and `namespace` globally via `enum-helpers.config` or on enum level via methods

```php
...

protected function getPrefix(): ?string
{
return 'prefix';
}

...

protected function getNamespace(): ?string
{
return 'namespace';
}
...
```

> Option. Can be added interface `\IsapOu\EnumHelperes\Contracts\HasLabel` that can solve ide autocomplete and tips
This helper compatible with Enums in [FilamentPHP](https://filamentphp.com/docs/3.x/support/enums)

```php

use Filament\Support\Contracts\HasLabel;

enum ExampleEnum: string implements HasLabel
{
use HasLabel

case ENUM_ONE = 'enum_one';
case ENUM_TWO = 'enum_two';
}
```

## Contributing

Expand Down
7 changes: 6 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

return [
'enum_locations' => [
Expand All @@ -11,5 +11,10 @@
'enum-helpers:migrate:enums',
],

'label' => [
'prefix' => null,
'namespace' => null,
],

'js_objects_file' => 'resources/js/enums.js',
];
42 changes: 42 additions & 0 deletions src/Concerns/HasLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace IsapOu\EnumHelpers\Concerns;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

use function get_class;
use function rtrim;
use function trans;

trait HasLabel
{
public function getLabel(?string $prefix = null, ?string $namespace = null): string
{
if (empty($prefix)) {
$prefix = $this->getPrefix();
}
if (empty($namespace)) {
$namespace = $this->getNamespace();
}

if (! empty($prefix)) {
$prefix = rtrim($prefix, '.') . '.';
}
if (! empty($namespace) && ! Str::endsWith($namespace, '::')) {
$namespace .= '::';
}

return trans(vsprintf('%s%s%s.%s', [$namespace, $prefix, get_class($this), $this->name]));
}

protected function getPrefix(): ?string
{
return Config::get('enum-helpers.label.prefix');
}

protected function getNamespace(): ?string
{
return Config::get('enum-helpers.label.namespace');
}
}
8 changes: 8 additions & 0 deletions src/Contracts/HasLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace IsapOu\EnumHelpers\Contracts;

interface HasLabel
{
public function getLabel(): ?string;
}

0 comments on commit 086d606

Please sign in to comment.