Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Translate PHP Enums for Laravel Nova with a single method call.
[![code-style](https://github.com/cable8mm/enum-getter/actions/workflows/code-style.yml/badge.svg)](https://github.com/cable8mm/enum-getter/actions/workflows/code-style.yml)
[![run-tests](https://github.com/cable8mm/enum-getter/actions/workflows/run-tests.yml/badge.svg)](https://github.com/cable8mm/enum-getter/actions/workflows/run-tests.yml)
[![Packagist Version](https://img.shields.io/packagist/v/cable8mm/enum-getter)](https://packagist.org/packages/cable8mm/enum-getter)
[![Packagist Dependency Version](https://img.shields.io/packagist/dependency-v/cable8mm/enum-getter/php?logo=PHP&logoColor=white&color=777BB4
)](https://packagist.org/packages/cable8mm/enum-getter)
[![Packagist Dependency Version](https://img.shields.io/packagist/dependency-v/cable8mm/enum-getter/php?logo=PHP&logoColor=white&color=777BB4)](https://packagist.org/packages/cable8mm/enum-getter)
[![Total Downloads](https://img.shields.io/packagist/dt/cable8mm/enum-getter)](https://packagist.org/packages/cable8mm/enum-getter/stats)
[![Total Stars](https://img.shields.io/packagist/stars/cable8mm/enum-getter)](https://github.com/cable8mm/enum-getter/stargazers)
[![License](https://img.shields.io/packagist/l/cable8mm/enum-getter)](https://github.com/cable8mm/enum-getter/blob/main/LICENSE.md)
Expand Down Expand Up @@ -119,16 +118,19 @@ Status::make(__('Status'))

## Available Methods

| Method | Description |
| ----------- | ------------------------- |
| `label()` | Get translated label |
| `labels()` | Get translated labels |
| `options()` | Get translated options |
| `keys()` | Get enum keys |
| `names()` | Get enum names |
| `reverse()` | Get reversed mapping |
| `has()` | Check existence |
| `of()` | Get enum instance by name |
| Method | Description |
| ---------------- | ----------------------------- |
| `label()` | Get translated label |
| `labels()` | Get translated labels |
| `options()` | Get translated options |
| `keys()` | Get enum keys |
| `names()` | Get enum names |
| `reverse()` | Get reversed mapping |
| `has()` | Check existence |
| `of()` | Get enum instance by name |
| `randomKey()` | Get a random enum key |
| `randomLabel()` | Get a random translated label |
| `randomOption()` | Get a random enum instance |

---

Expand All @@ -142,10 +144,10 @@ Its primary goal is to make translated enums effortless to use within Laravel No

| Feature | Enum Getter | Generic Enum Packages |
| --------------------------- | ----------- | --------------------- |
| Translation aware | ✅ | ⚠️ |
| Laravel Nova Select | ✅ | ⚠️ |
| Laravel Nova Badge | ✅ | ⚠️ |
| One-line translated options | ✅ | ❌ |
| Translation aware | ✅ | ⚠️ |
| Laravel Nova Select | ✅ | ⚠️ |
| Laravel Nova Badge | ✅ | ⚠️ |
| One-line translated options | ✅ | ❌ |

---

Expand Down Expand Up @@ -185,7 +187,7 @@ If you discover any security related issues, please email [cable8mm@gmail.com](m

## Credits

* Sam Lee
- Sam Lee

## License

Expand Down
44 changes: 44 additions & 0 deletions src/EnumGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,48 @@ public static function reverse(): array
self::keys(),
);
}

/**
* Get enum instance by key or label.
*
* @throws \InvalidArgumentException
*/
public static function randomKey(): string
{
$keys = self::keys();

if (empty($keys)) {
throw new \InvalidArgumentException(
'The enum has no keys.'
);
}

return $keys[array_rand($keys)];
}

public static function randomLabel(): string
{
$labels = self::labels();

if (empty($labels)) {
throw new \InvalidArgumentException(
'The enum has no labels.'
);
}

return $labels[array_rand($labels)];
}

public static function randomOption(): static
{
$cases = self::cases();

if (empty($cases)) {
throw new \InvalidArgumentException(
'The enum has no cases.'
);
}

return $cases[array_rand($cases)];
}
}
30 changes: 30 additions & 0 deletions tests/EnumGetterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,34 @@ public function test_has_method_with_trait(): void

$this->assertFalse(Example::has(TranslatedExample::EXAMPLE_1));
}

public function test_random_key_method(): void
{
$this->assertContains(Example::randomKey(), Example::keys());
}

public function test_random_key_method_with_values(): void
{
$this->assertContains(TranslatedExample::randomKey(), TranslatedExample::keys());
}

public function test_random_label_method(): void
{
$this->assertContains(Example::randomLabel(), Example::labels());
}

public function test_random_label_method_with_values(): void
{
$this->assertContains(TranslatedExample::randomLabel(), TranslatedExample::labels());
}

public function test_random_option_method(): void
{
$this->assertContains(Example::randomOption(), Example::cases());
}

public function test_random_option_method_with_values(): void
{
$this->assertContains(TranslatedExample::randomOption(), TranslatedExample::cases());
}
}
Loading