-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7372333
Showing
17 changed files
with
642 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github: sakanjo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: PHPStan | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
phpstan: | ||
name: phpstan | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
coverage: none | ||
|
||
- name: Install composer dependencies | ||
uses: ramsey/composer-install@v2 | ||
|
||
- name: Run PHPStan | ||
run: ./vendor/bin/phpstan --error-format=github | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
tools: composer:v2 | ||
extensions: mbstring, pdo, pdo_sqlite | ||
coverage: xdebug | ||
|
||
- name: Install Dependencies | ||
run: composer install --no-interaction --prefer-dist --optimize-autoloader | ||
|
||
- name: Run pest | ||
run: ./vendor/bin/pest | ||
|
||
- name: Run Pint | ||
run: ./vendor/bin/pint --test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor | ||
/composer.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Salah Kanjo | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
<h1 align="center">🔥 Easy enum</h1> | ||
|
||
<p align="center"> | ||
<a href="https://github.com/sakanjo/laravel-easy-enum/actions"><img alt="Workflow status" src="https://img.shields.io/github/actions/workflow/status/sakanjo/laravel-easy-enum/tests.yml?style=for-the-badge"></a> | ||
<a href="https://laravel.com"><img alt="Laravel v11.x" src="https://img.shields.io/badge/Laravel-v11.x-FF2D20?style=for-the-badge&logo=laravel"></a> | ||
<a href="https://php.net"><img alt="PHP 8.2" src="https://img.shields.io/badge/PHP-8.2-777BB4?style=for-the-badge&logo=php"></a> | ||
</p> | ||
|
||
<p align="center">Easily work with enums.</p> | ||
|
||
> ✨ Help support the maintenance of this package by [sponsoring me](https://github.com/sponsors/sakanjo). | ||
> Designed to work with **Laravel**, **Filament**, and more. | ||
![Preview](./art/preview.png) | ||
|
||
Table of Contents | ||
================= | ||
|
||
* [Install](#-install) | ||
* [Usage](#-usage) | ||
* [1. Create enum](#1-create-enum) | ||
* [2. Create lang file](#2-create-lang-file) | ||
* [Methods](#-methods) | ||
* [getLabel](#getlabel) | ||
* [is](#is) | ||
* [isNot](#isnot) | ||
* [tryFromName](#tryfromname) | ||
* [fromName](#fromname) | ||
* [names](#names) | ||
* [values](#values) | ||
* [options](#options) | ||
* [toHtml](#tohtml) | ||
* [Practical examples](#-practical-examples) | ||
* [Filamentphp](#filamentphp) | ||
* [Enum](#enum) | ||
* [Resource](#resource) | ||
* [Laravel blade](#laravel-blade) | ||
* [Support the development](#-support-the-development) | ||
* [Credits](#%EF%B8%8F-credits) | ||
* [License](#-license) | ||
|
||
## 📦 Install | ||
|
||
``` | ||
composer require sakanjo/laravel-easy-enum | ||
``` | ||
|
||
## 🦄 Usage | ||
|
||
### 1. Create enum | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
use SaKanjo\EasyEnum; | ||
|
||
enum ExampleEnum: int | ||
{ | ||
use EasyEnum; | ||
|
||
case Active = 0; | ||
case NOPE = 1; | ||
} | ||
``` | ||
|
||
### 2. Create lang file | ||
|
||
```php | ||
// lang/en/enums.php | ||
|
||
<?php | ||
|
||
use App\Enums; | ||
|
||
return [ | ||
Enums\ExampleEnum::class => [ | ||
Enums\ExampleEnum::NOPE->name => 'Nope', | ||
// ... | ||
], | ||
|
||
// ... | ||
]; | ||
``` | ||
|
||
That's it! | ||
|
||
## 📚 Methods | ||
|
||
### getLabel | ||
|
||
Returns the label of the enum value. | ||
|
||
```php | ||
Status::Active->getLabel(); // Active | ||
``` | ||
|
||
### is | ||
|
||
Checks if the enum is equal to another one. | ||
|
||
```php | ||
$enum1->is($enum2); // boolean | ||
``` | ||
|
||
### isNot | ||
|
||
Checks if the enum is not equal to another one. | ||
|
||
```php | ||
$enum1->isNot($enum2); // boolean | ||
``` | ||
|
||
### tryFromName | ||
|
||
Safely converts a string to its corresponding enum value (returns null if not found). | ||
|
||
```php | ||
Status::tryFromName('Active'); // Status::Active | ||
Status::tryFromName('Oops'); // null | ||
``` | ||
|
||
Converts a string to its corresponding enum value (throws exception if not found). | ||
|
||
### fromName | ||
|
||
```php | ||
Status::fromName('Active'); // Status::Active | ||
Status::fromName('Oops'); // Throws ValueError exception | ||
``` | ||
|
||
### names | ||
|
||
Returns a list of case names. | ||
|
||
```php | ||
Status::names(); // ['Active', 'NOPE'] | ||
``` | ||
|
||
### values | ||
|
||
Returns a list of case values . | ||
|
||
```php | ||
Status::values(); // [0, 1] | ||
``` | ||
|
||
### options | ||
|
||
Returns an associative array of case names and values. | ||
|
||
```php | ||
Status::options(); // ['Active' => 0, 'NOPE' => 1] | ||
Status::options(true); // ['Active' => 0, 'Nope' => 1] | ||
``` | ||
|
||
### toHtml | ||
|
||
alias for `getLabel`, useful in blade. | ||
|
||
```php | ||
Status::Active->toHtml(); // Active | ||
``` | ||
|
||
## 🔥 Practical examples | ||
|
||
#### Filamentphp | ||
|
||
##### Enum | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
use Filament\Support\Contracts\HasLabel; | ||
use SaKanjo\EasyEnum; | ||
|
||
enum Status: int implements HasLabel | ||
{ | ||
use EasyEnum; | ||
|
||
case Active = 0; | ||
case Disabled = 1; | ||
} | ||
``` | ||
|
||
##### Resource | ||
|
||
```php | ||
<?php | ||
|
||
use Filament\Forms; | ||
use App\Enums; | ||
|
||
Forms\Components\Select::make('status') | ||
->options(Enums\Status::class); | ||
``` | ||
|
||
#### Laravel blade | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
use Illuminate\Contracts\Support\Htmlable; | ||
use SaKanjo\EasyEnum; | ||
|
||
enum Status: int implements Htmlable | ||
{ | ||
use EasyEnum; | ||
|
||
case Active = 0; | ||
case Disabled = 1; | ||
} | ||
``` | ||
|
||
```php | ||
<div> | ||
Current status: {{ auth()->user()->status }} | ||
</div> | ||
``` | ||
|
||
## 💖 Support the development | ||
|
||
**Do you like this project? Support it by donating** | ||
|
||
Click the ["💖 Sponsor"](https://github.com/sponsors/sakanjo) at the top of this repo. | ||
|
||
## ©️ Credits | ||
|
||
- [Salah Kanjo](https://github.com/sakanjo) | ||
- [All Contributors](../../contributors) | ||
|
||
## 📄 License | ||
|
||
[MIT License](https://github.com/sakanjo/laravel-easy-metrics/blob/master/LICENSE) © 2023-PRESENT [Salah Kanjo](https://github.com/sakanjo) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "sakanjo/laravel-easy-enum", | ||
"description": "Easily work with enum.", | ||
"license": "MIT", | ||
"keywords": [ | ||
"laravel", | ||
"enums", | ||
"laravel-easy" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Salah Kanjo", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.2", | ||
"illuminate/support": "^10.0 || ^11.0" | ||
}, | ||
"require-dev": { | ||
"laravel/pint": "^1.1", | ||
"pestphp/pest": "^2.3", | ||
"phpstan/phpstan": "^1.1", | ||
"orchestra/testbench": "^9.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SaKanjo\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"SaKanjo\\EasyEnum\\Tests\\": "tests/src/" | ||
} | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"@php vendor/bin/pint --test", | ||
"@php vendor/bin/phpstan", | ||
"@php vendor/bin/pest --parallel" | ||
] | ||
}, | ||
"minimum-stability": "stable", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
parameters: | ||
level: 5 | ||
paths: | ||
- src |
Oops, something went wrong.