synortix / laravel-dictionary
Laravel API dictionary support and validation library. Humanize your Enum's in API resource responses.
Installs: 2 121
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.0
- laravel/framework: >=5.0
- myclabs/php-enum: ^1.6
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2025-06-07 21:57:30 UTC
README
Laravel API enum dictionary support and validation library.
Humanize your Enum's in API resource responses.
Installing
Composer
To get started install package by requiring it through composer CLI
composer require synortix/laravel-dictionary:1.*
Usage
Define Dictionary
You can define your dictionary by extending Synortix\Dictionary\Dictionary class
use Synortix\Dictionary\Dictionary;
class CustomerTypeDictionary extends Dictionary
{
const FREE = 1;
const PAID = 2;
}
Create Dictionary Instance
You can create object from string representation and pass that further as object. Dictionary string case-insensitive.
try {
$customerType = new CustomerTypeDictionary($request->get('customer_type'));
} catch (UnexpectedValueException $e) {
// invalid value, handle it here
}
Use in Model Resources
class CustomerResource extends Resource
{
public function toArray($request)
{
return [
'id' => $this->resource->id,
'type' => (string) new CustomerTypeDictionary($this->resource->type)
];
}
}
Validate request parameter
$this->validate($request, [
'cusomer_type' => ['required', new DictionaryRule(CustomerTypeDictionary::class)],
]);
Running the tests
For testing purposes this library use PHPUnit. To run tests on your own execute the following command.
clone git@github.com:synortix/laravel-dictionary.git
cd laravel-dictionary
composer install
php vendor/bin/phpunit --bootstrap vendor/autoload.php tests/
License
This project is licensed under the MIT License - see the LICENSE.md file for details