Skip to content

Commit 40202db

Browse files
author
Kevin Buchholz
authored
Docs for weightet feature (#13)
Docs for weightet feature
2 parents 042f035 + 222a097 commit 40202db

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Enum implementation for Laravel. Based on [eloquent/enumeration](https://github.
1515
* Enum artisan generator
1616
* Validation rules for passing enum values as input parameters
1717
* Localization support
18+
* Support to give enum Members a weight
1819
* Extensible
1920

2021
## Table of Contents
@@ -27,6 +28,7 @@ Enum implementation for Laravel. Based on [eloquent/enumeration](https://github.
2728
* [Validation](#validation)
2829
* [Localization](#localization)
2930
* [Model Trait](#model-trait)
31+
* [Weighted Enums](#weighted-enums)
3032
* [License information](#license-information)
3133

3234
## Requirements
@@ -325,6 +327,78 @@ echo $type === UserType::defaultMember(); // "true"
325327
echo $type->value; // "0"
326328
```
327329

330+
## Weighted Enums
331+
332+
It is Possible to define weights for enum members. the standard way is to define the weights a config file and them implement the `Weighted`-interface with the `IsWeighted`-trait and define the path to your config. The weigths can be defined as integer or float values.
333+
334+
``` php
335+
// The Enum
336+
<?php
337+
338+
namespace App\Enums;
339+
340+
use Sourceboat\Enumeration\Enumeration;
341+
use Sourceboat\Enumeration\Enums\Interfaces\Weighted;
342+
use Sourceboat\Enumeration\Enums\Traits\IsWeighted;
343+
344+
/**
345+
* @method static \App\Enums\UserType Administrator() // These are only for autocompletion etc.
346+
* @method static \App\Enums\UserType Moderator()
347+
* @method static \App\Enums\UserType Subscriber()
348+
* @method static \App\Enums\UserType SuperAdministrator()
349+
*/
350+
final class UserType extends Enumeration implements Weighted
351+
{
352+
use IsWeighted;
353+
354+
const Administrator = 0;
355+
const Moderator = 1;
356+
const Subscriber = 2;
357+
const SuperAdministrator = 3;
358+
359+
protected static $weightOptionsKey = 'test'; // optional, by standard its `sprintf('enums.%s.weights', static::class)`
360+
}
361+
```
362+
363+
For further customization the `Weighted::weight(): int|float`-method can be overridden to use your own way of defining the weights.
364+
365+
The following methods for comparing weighted enum members are available:
366+
367+
* `Weighted::isGreaterThan(Weighted): bool`
368+
* `Weighted::isGreaterThanOrEuqalTo(Weighted): bool`
369+
* `Weighted::isEuqalTo(Weighted): bool`
370+
* `Weighted::isLessThanOrEqualTo(Weighted): bool`
371+
* `Weighted::isLessThan(Weighted): bool`
372+
373+
The following methods for filtering the enum are available:
374+
375+
* `Weighted::getMembersGreaterThanThis(): array<Weighted>`
376+
* `Weighted::getMembersGreaterThanOrEqualToThis(): array<Weighted>`
377+
* `Weighted::getMembersEqualToThis(): array<Weighted>`
378+
* `Weighted::getMembersLessThanOrEqualToThis(): array<Weighted>`
379+
* `Weighted::getMembersLessThanThis(): array<Weighted>`
380+
* `static Weighted::getMembersGreaterThan(Weighted): array<Weighted>`
381+
* `static Weighted::getMembersGreaterThanOrEqualTo(Weighted): array<Weighted>`
382+
* `static Weighted::getMembersEqualTo(Weighted): array<Weighted>`
383+
* `static Weighted::getMembersLessThanOrEqualTo(Weighted): array<Weighted>`
384+
* `static Weighted::getMembersLessThan(Weighted): array<Weighted>`
385+
* `static Weighted::getMembersBetween(Weighted $lowerMember, Weighted $higherMember): array<Weighted>`
386+
* `static Weighted::getMembersBetweenOrEqualTo(Weighted $lowerMember, Weighted $higherMember): array<Weighted>`
387+
388+
### Model-trait `HasWeightedEnumScopes`
389+
390+
With the model trait `HasWeightedEnumsScopes` you can easily search for models with enum values greater than one or between two other enum value, even with string values.
391+
392+
Available scopes:
393+
394+
* `whereGreaterThanEnum(string $attribute, Weighted $member)`
395+
* `whereGreaterThanOrEqualToEnum(string $attribute, Weighted $member)`
396+
* `whereEqualToEnum(string $attribute, Weighted $member)`
397+
* `whereLessThanOrEqualToEnum(string $attribute, Weighted $member)`
398+
* `whereLessThanEnum(string $attribute, Weighted $member)`
399+
* `whereBetweenEnum(string $attribute, Weighted $lowerMember, Weighted $higherMember)`
400+
* `whereBetweenOrEqualEnum(string $attribute, Weighted $lowerMember, Weighted $higherMember)`
401+
328402
## License information
329403

330404
Much of the functionality in this Package is inspired by [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) and some code has been taken from it and modified, for example the `MakeEnumCommand.php`, the `EnumServiceProvider.php` and this readme.

0 commit comments

Comments
 (0)