Skip to content

Commit

Permalink
Merge 9a04a10 into cccc43a
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Mar 29, 2019
2 parents cccc43a + 9a04a10 commit e874648
Show file tree
Hide file tree
Showing 28 changed files with 1,150 additions and 430 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_name: travis-ci
coverage_clover: build/clover.xml
json_path: build/coveralls.json
3 changes: 3 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
preset: laravel

enabled:
- ordered_class_elements

disabled:
- single_class_element_per_statement
- self_accessor
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ env:
before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
- composer require --dev scrutinizer/ocular --no-interaction
- composer require --dev php-coveralls/php-coveralls --no-interaction

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- vendor/bin/phpunit

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
- php vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage.clover
- php vendor/bin/php-coveralls
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

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

## 2.0.0 - 2019-xx-xx

A full major rework of the `Enum` class - we try to list all changes, for more details you can check out the [PR](https://github.com/spatie/enum/pull/18) and the [Issue](https://github.com/spatie/enum/issues/10).

- Add `\Spatie\Enum\Enumerable` interface
- Add `\Spatie\Enum\Exceptions\DuplicatedIndexException`, `\Spatie\Enum\Exceptions\DuplicatedValueException`, `\Spatie\Enum\Exceptions\InvalidIndexException` and `\Spatie\Enum\Exceptions\InvalidValueException` exceptions
- Add `\Spatie\Enum\Enum->getIndex()` method
- Add `\Spatie\Enum\Enum::getIndices()` method
- Add `\Spatie\Enum\Enum->getValue()` method
- Add `\Spatie\Enum\Enum::getValues()` method
- Rename `\Spatie\Enum\Enum::from()` to `\Spatie\Enum\Enum::make()`
- Rename `\Spatie\Enum\Enum::equals()` to `\Spatie\Enum\Enum::isEqual()`
- Rename `\Spatie\Enum\Enum::isOneOf()` to `\Spatie\Enum\Enum::isAny()`
- Change `\Spatie\Enum\Enum->__construct()` signature and responsibility - only take index & value and validate them
- Drop recursive `\Spatie\Enum\Enum::make()` support from inside of an unstatic method
- Drop `\Spatie\Enum\Enum::$map` in favor of `\Spatie\Enum\Enum->getIndex()`and `\Spatie\Enum\Enum->getValue()`
- Update all methods have strict type checks: `index: int` and `value: string`
- Update all methods are compatible with all required types: index, value, name or instance of Enum

## 1.1.0 - 2019-03-18

- Add support for is* checks
Expand Down
122 changes: 52 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setStatus(StatusEnum $status): void
$this->status = $status;
}

//
// ...

$class->setStatus(StatusEnum::draft());
```
Expand Down Expand Up @@ -65,7 +65,7 @@ public function setStatus(StatusEnum $status)
$this->status = $status;
}

//
// ...

$class->setStatus(StatusEnum::draft());
```
Expand All @@ -77,55 +77,80 @@ $class->setStatus(StatusEnum::draft());
### Creating an enum from a value

```php
$status = StatusEnum::from('draft');

// or

$status = new StatusEnum('published');
$status = StatusEnum::make('draft');
```

### Override enum values

By default, the string value of an enum is simply the name of that method.
In the previous example it would be `draft`.
By default, the string value of an enum is simply the name of that method. In the previous example it would be `draft`.

You can override this value, by adding the `$map` property:
You can override the value or the index by overriding the `getValue()` or `getIndex()` method:

```php
/**
* @method static self draft()
* @method static self published()
* @method static self archived()
*/
class StatusEnum extends Enum
{
protected static $map = [
'draft' => '1',
'published' => 'other published value',
'archived' => '-10',
];
public static function draft(): StatusEnum
{
return new class() extends StatusEnum {
public function getValue(): string
{
return 'status.draft';
}
public function getIndex(): int
{
return 10;
}
};
}

public static function published(): StatusEnum
{
return new class() extends StatusEnum {
public function getValue(): string
{
return 'status.published';
}
public function getIndex(): int
{
return 20;
}
};
}

public static function archived(): StatusEnum
{
return new class() extends StatusEnum {
public function getValue(): string
{
return 'status.archived';
}
public function getIndex(): int
{
return -10;
}
};
}
}
```

Mapping values is optional.

> Note that mapped values should always be strings.
Overriding these methods is always optional but if you want to rely on the index we recommend to define them yourself. Otherwise they could easily change - we only use array index.

### Comparing enums

Enums can be compared using the `equals` method:
Enums can be compared using the `isEqual` method:

```php
$status->equals($otherStatus);
$status->isEqual($otherStatus);
```

You can also use dynamic `is` methods:

```php
$status->isDraft(); // return a boolean
StatusEnum::isDraft($status); // return a boolean
```

Note that if you want auto completion on these `is` methods, you must add extra doc blocks on you enum classes.
Note that if you want auto completion on these `is` methods, you must add extra doc blocks on your enum classes.

### Enum specific methods

Expand All @@ -138,51 +163,8 @@ There are several ways to do this:
- Use enum specific methods, similar to Java.

This package also supports these enum specific methods.
Here's how you can implement them:

```php
abstract class MonthEnum extends Enum
{
abstract public function getNumericIndex(): int;

public static function january(): MonthEnum
{
return new class() extends MonthEnum
{
public function getNumericIndex(): int
{
return 1;
}
};
}

public static function february(): MonthEnum
{
return new class() extends MonthEnum
{
public function getNumericIndex(): int
{
return 2;
}
};
}

// …
}
```

By declaring the enum class itself as abstract,
and using static constructors instead of doc comments,
you're able to return an anonymous class per enum, each of them implementing the required methods.

You can use this enum the same way as any other:

```php
MonthEnum::january()->getNumericIndex()
```

Note that one drawback of this approach is that the value of the enum
**is always** the name of the static function, there's no way of mapping it.
By declaring the enum class itself as abstract, and using static constructors instead of doc comments, you're able to return an anonymous class per enum, each of them implementing the required methods.

### Testing

Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
"email": "brent@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Tom Witkowski",
"email": "dev.gummibeer@gmail.com",
"homepage": "https://gummibeer.de",
"role": "Developer"
}
],
"require": {
"php": "^7.2",
"ext-json": "*"
},
"require-dev": {
"larapack/dd": "^1.0",
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^8.0",
"symfony/stopwatch": "^4.2"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 1 addition & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-clover" target="build/clover.xml"/>
</logging>
</phpunit>

0 comments on commit e874648

Please sign in to comment.