https://github.com/iqomp/enum
Static data enum provider.
https://github.com/iqomp/enum
Last synced: 6 months ago
JSON representation
Static data enum provider.
- Host: GitHub
- URL: https://github.com/iqomp/enum
- Owner: iqomp
- License: mit
- Created: 2020-11-24T08:11:22.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-29T04:55:40.000Z (about 5 years ago)
- Last Synced: 2025-05-26T20:10:00.511Z (about 1 year ago)
- Language: PHP
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# iqomp/enum
Static data enum provider. This module provide list of enums that may used by
application.
## Installation
```bash
composer require iqomp/enum
```
## Publishing Config
```bash
php bin/hyperf.php vendor:publish iqomp/enum
```
## Configuration
To add new enum data or modify exists one, modify file on
`/config/autoload/enum.php` to return data as below:
```php
[
'gender' => [
'0' => 'Unknown',
'1' => 'Male',
'2' => 'Female',
'3' => 'Non-Binary'
]
]
];
```
Or if you prefer to use file `ConfigProvider.php`, add data as below:
```php
[
'enums' => [
'gender' => [
'0' => 'Unknown',
'1' => 'Male',
'2' => 'Female',
'3' => 'Non-Binary'
]
]
]
];
}
}
```
Make sure to update your `composer.json` file to let hyperf identify the config file:
```json
"extra": {
"hyperf": {
"config": "Vendor\\Module\\ConfigProvider"
}
}
```
## Translation
If you want the enum label to be translated, make sure to install module
[hyperf/translation](https://github.com/hyperf/translation). Add new translation
on folder `storage/languages/vendor/enum/{locale}/{enum-name}.php`.
```php
'Unknown',
'Female' => 'Female',
'Male' => 'Male',
'Non-Binary' => 'Non Binary'
];
```
## Usage
Use class `Iqomp\Enum\Enum` to create enum object with specified value.
```php
value;
$enum->options;
$enum->label;
```
## Method
List of method defined by this class object:
### __construct(string $name, $value=null, string $type=null): Enum
Create new Enum object. This method accept parameters:
1. `name::string` The name of enum datalist.
1. `value::int|str` The value of enum item.
1. `type::string` Convert provided value as `int` or `str`.
### __get($name): mixed
Get enum data property, accepted name are `value`, `label`, and `options`.
### __toString(): string
Return string enum label.
### jsonSerialize()
Convert the enum to json_encode ready parameter.
## Form Validator
If you're using validator [iqomp/validator](https://github.com/iqomp/validator/)
for your object or form validator, this module add new form validator rule named
`enum` to validate if user provided data is in registered enum.
### enum => name
Make sure user provided value is in registered enum, the value of user posted data
can be single int/str or array of it.
```php
// ...
'rules' => [
'enum' => 'std-gender'
]
// ...
```
## Linter
Run below script to run psr-12 linter:
```bash
composer lint
```
## Formatter
If you're using formatter [iqomp/formatter](https://github.com/iqomp/formatter/)
for your object formatter, this module add new object format type named `enum`
and `multiple-enum` that can be use to convert object property value to enum
object.
### enum
Convert current object property value to `Iqomp\Enum\Enum` object.
```php
// ...
'/field/' => [
'type' => 'enum',
'enum' => 'std-gender'
]
// ...
```
### multiple-enum
Convert current object property value to array of `Iqomp\Enum\Enum` with custom
separator:
```php
// ...
'/field/' => [
'type' => 'multiple-enum',
'enum' => 'std-gender',
'separator' => ',' // PHP_EOL, 'json'
]
// ...
```
If property `separator` is not set, `PHP_EOL` will be used. It also accept
separator `'json'` that will use `json_decode` for separation. If the value of
object property is already array, no separation/explode will used.