Skip to content

Commit

Permalink
made Laravel reporter configurable, added keys to Value
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 21, 2022
1 parent 6baffa1 commit dbecccc
Show file tree
Hide file tree
Showing 26 changed files with 394 additions and 116 deletions.
5 changes: 3 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ version: '2'
plugins:
markdownlint:
exclude_patterns:
- '!**/*.md'
- vendor
- LICENSE.md
enabled: true
Expand All @@ -11,7 +10,9 @@ plugins:
enabled: false
phpcodesniffer:
exclude_patterns:
- !**/*.php
- "**/*"
- "!src/*"
- 'src/Enums/*'
enabled: true
phpmd:
enabled: true
Expand Down
40 changes: 0 additions & 40 deletions .php_cs.dist.php

This file was deleted.

9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

All notable changes to `Enumhancer` will be documented in this file

## 1.12.0 - 2022-06-19
## 1.15.0 - 2022-06-21

- Made the Laravel [Reporter](docs/reporters.md#laravel) configurable
- added `key` method to [Value](docs/value.md)

## 1.14.0 - 2022-06-19

- Added transition hooks [State](docs/state.md)
- [Makers](docs/makers.md) & [From](docs/from.md) now allow you to use integer
- [Makers](docs/makers.md) & [From](docs/from.md) now allow you to use integer
keys on basic and string enums

## 1.12.0 - 2022-06-15
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ more will be added in the future.
If you have an idea, or you miss something that needs to be added, just let me
know.

Enumhancer is case-agnostic, which means `Enum` equals `ENUM` equals `enum`. It
Is also agnostic in what type of enum you use. Which means you can use
numeric keys with basic and string backed enums as well as string keys with
basic and integer backed enums. New projects rarely needs more than basic
enums anyway.
Enumhancer is case-agnostic, which means `Enum` equals `ENUM` equals `enum`.
This is done with the idea that it is useless to have two different enums
having the same name and different casing.

Note: While functionality that also exists in Spatie's PHP Enum is made
It is also type-agnostic. This way you can steer clear as much as possible
from the extra work that comes with backed enums.

Note: While most functionality that also exists in Spatie's PHP Enum is made
backwards compatible to allow for an easy migration to PHP native enums,
currently this is not the case for the PHPUnit assertions or Faker Provider.
currently this is not the case for their laravel package, PHPUnit assertions or
Faker Provider.

## Installation

Expand All @@ -34,7 +36,7 @@ composer require henzeb/enumhancer
## Usage

You can simply add the `Enhancers` trait to your `enum` in order to use almost
all functionality of this package. All features should work with `unit` enums as
all functionality of this package. All features should work with `basic` enums as
well as `backed` enums' unless stated otherwise.

```php
Expand Down
18 changes: 17 additions & 1 deletion docs/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ enum YourEnum: string {

## Laravel

For laravel there is out of the box support, see [README](../README.md#Laravel)
For laravel, there is out of the box support, which is automatically loaded
see [README](../README.md#laravels-auto-discovery).

Out of the box, the reporter will report to your configured default channel,
but you can change the channel and the `LogLevel`.

Note: You don't need to disable autodiscovery for this, as it will override
the configuration.

```php
use Henzeb\Enumhancer\Enums\LogLevel;
use Henzeb\Enumhancer\Helpers\EnumReporter;

EnumReporter::laravel(LogLevel::Alert); // alerts to configured channel
EnumReporter::laravel(null, 'stack'); // notices to stack
EnumReporter::laravel(null, 'stack', 'daily'); // notices to stack and daily
```
15 changes: 11 additions & 4 deletions docs/value.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Value

When you have a basic enum, you have no value. You have to use the `name` value.
But what if you want to have the lowercase version of that name? That's where
`Value` comes in.
Basic enums don't have a value. But if you want to know their numeric key or
their value, you are referred to name and `cases()` respectively to find out.
`Value` eases that problem for you.

## Usage

Expand All @@ -13,6 +13,7 @@ enum yourEnum {
use Value;

case MY_ENUM;
case Other;

}
```
Expand All @@ -21,7 +22,13 @@ enum yourEnum {

```php
YourEnum::MY_ENUM->value(); // will return `my_enum`;
YourEnum::Other->value(); // will return `other`;

YourEnum::MY_ENUM->key(); // will return `0`;
YourEnum::Other->key(); // will return `1`;
```

Note: When used with a string or int backed enum, this method will return it's
Note: When used with a string or int backed enum, `value` will return it's
actual value.

Note: When used with an int backed enum, `key` will return the value.
4 changes: 2 additions & 2 deletions src/Concerns/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

trait Comparison
{
final public function equals(self|string|int ...$equals): bool
public function equals(self|string|int ...$equals): bool
{
return EnumCompare::equals($this, ...$equals);
}

final public function __call(string $name, array $arguments): self|bool
public function __call(string $name, array $arguments): self|bool
{
if (EnumCompare::isValidCall(self::class, $name, $arguments)) {
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s(...)', $this::class, $name));
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait Constructor
{
final public static function __callStatic(string $name, array $arguments)
public static function __callStatic(string $name, array $arguments)
{
return EnumMakers::make(self::class, $name, true);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Concerns/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Henzeb\Enumhancer\Concerns;

use UnitEnum;
use ValueError;
use Henzeb\Enumhancer\Helpers\EnumMakers;
use Henzeb\Enumhancer\Helpers\EnumCompare;

trait Defaults
{
final static public function default(): ?self
static public function default(): ?UnitEnum
{
try {
return EnumMakers::make(self::class, 'default', true);
Expand All @@ -17,7 +18,7 @@ final static public function default(): ?self
}
}

final public function isDefault(): bool
public function isDefault(): bool
{
$default = self::default();

Expand All @@ -28,7 +29,7 @@ final public function isDefault(): bool
return false;
}

final public function isNotDefault(): bool
public function isNotDefault(): bool
{
return !$this->isDefault();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait Extractor
{
final public static function extract(string $text, Mapper|string|null $mapper = null): array
public static function extract(string $text, Mapper|string|null $mapper = null): array
{
return EnumExtractor::extract(self::class, $text, $mapper);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

trait From
{
final public static function from(string $key): self
public static function from(string $key): self
{
return EnumMakers::make(static::class, $key, useDefault: true);
}

final public static function tryFrom(string $key): ?self
public static function tryFrom(string $key): ?self
{
return EnumMakers::tryMake(static::class, $key);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function labels(): array
return [];
}

final public function label(): ?string
public function label(): ?string
{
return self::labels()[$this->name]
?? (method_exists($this, 'value') ? $this->value() : null)
Expand Down
8 changes: 4 additions & 4 deletions src/Concerns/Makers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

trait Makers
{
final public static function make(int|string|null $value): self
public static function make(int|string|null $value): self
{
return EnumMakers::make(self::class, $value);
}

final public static function tryMake(int|string|null $value): ?self
public static function tryMake(int|string|null $value): ?self
{
return EnumMakers::tryMake(self::class, $value);
}
Expand All @@ -20,7 +20,7 @@ final public static function tryMake(int|string|null $value): ?self
* @param iterable $values
* @return self[]
*/
final public static function makeArray(iterable $values): array
public static function makeArray(iterable $values): array
{
return EnumMakers::makeArray(self::class, $values);
}
Expand All @@ -29,7 +29,7 @@ final public static function makeArray(iterable $values): array
* @param iterable $values
* @return self[]
*/
final public static function tryMakeArray(iterable $values): array
public static function tryMakeArray(iterable $values): array
{
return EnumMakers::tryMakeArray(self::class, $values);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Concerns/Mappers.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ protected static function mapper(): ?Mapper
return null;
}

final public static function make(string|int|null $value, Mapper|string $mapper = null): self
public static function make(string|int|null $value, Mapper|string $mapper = null): self
{
return EnumMakers::make(
self::class,
EnumMapper::map(self::class, $value, $mapper, self::mapper())
);
}

final public static function tryMake(string|int|null $value, Mapper|string $mapper = null): ?self
public static function tryMake(string|int|null $value, Mapper|string $mapper = null): ?self
{
return EnumMakers::tryMake(
self::class,
EnumMapper::map(self::class, $value, $mapper, self::mapper())
);
}

final public static function makeArray(iterable $values, Mapper|string $mapper = null): array
public static function makeArray(iterable $values, Mapper|string $mapper = null): array
{

return EnumMakers::makeArray(
Expand All @@ -47,15 +47,15 @@ final public static function makeArray(iterable $values, Mapper|string $mapper =
);
}

final public static function tryMakeArray(iterable $values, Mapper|string $mapper = null): array
public static function tryMakeArray(iterable $values, Mapper|string $mapper = null): array
{
return EnumMakers::tryMakeArray(
self::class,
EnumMapper::mapArray(self::class, $values, $mapper, self::mapper())
);
}

final public static function makeOrReport(
public static function makeOrReport(
int|string|null $value,
BackedEnum $context = null,
Mapper|string $mapper = null
Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait Properties
{
final public static function property(string $property, mixed $value = null): mixed
public static function property(string $property, mixed $value = null): mixed
{
if (null === $value) {
return EnumProperties::get(self::class, $property);
Expand All @@ -16,12 +16,12 @@ final public static function property(string $property, mixed $value = null): mi
return $value;
}

final public static function unset(string $property): void
public static function unset(string $property): void
{
EnumProperties::clear(self::class, $property);
}

final public static function unsetAll(): void
public static function unsetAll(): void
{
EnumProperties::clear(self::class);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Subset.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait Subset
{
final public static function of(self ...$enums): EnumSubset
public static function of(self ...$enums): EnumSubset
{
return new EnumSubsetMethods(self::class, ...($enums ?:self::cases()));
}
Expand Down
11 changes: 10 additions & 1 deletion src/Concerns/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@

trait Value
{
final public function value(): string|int
public function value(): string|int
{
return EnumValue::value($this);
}

public function key(): int
{
if (property_exists($this, 'value') && is_numeric($this->value)) {
return (int)$this->value;
}

return array_search($this, $this::cases());
}
}
Loading

0 comments on commit dbecccc

Please sign in to comment.