Skip to content

Commit dbe3919

Browse files
committed
Add voku/arrayy Arrayy implementation
1 parent d68fa09 commit dbe3919

15 files changed

+167
-40
lines changed

CHANGELOG-1.x.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.2.0] - 2020-09-07
10+
### Added
11+
- `voku/arrayy` `Arrayy` implementation
12+
13+
### Updated
14+
- Rename `ArrayAccessibleStorage` to `GenericArrayAccessibleStorage`
15+
- Extract common methods to `GenericArrayAccessibleObjectStorage`
16+
917
## [1.1.1] - 2020-09-02
1018
### Fixed
1119
- There was an issue when calling `Enum::option1()` and then calling `Enum::from('option2')`, `option2` was not registered.
@@ -26,7 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2634
### Added
2735
- Array and illuminate collection implementations
2836

29-
[Unreleased]: https://github.com/ekvedaras/php-enum/compare/v1.1.1...HEAD
37+
[Unreleased]: https://github.com/ekvedaras/php-enum/compare/v1.2.0...HEAD
38+
[1.2.0]: https://github.com/ekvedaras/php-enum/compare/v1.1.1...v1.2.0
3039
[1.1.1]: https://github.com/ekvedaras/php-enum/compare/v1.1.0...v1.1.1
3140
[1.1.0]: https://github.com/ekvedaras/php-enum/compare/v1.0.1...v1.1.0
3241
[1.0.1]: https://github.com/ekvedaras/php-enum/compare/v1.0.0...v1.0.1

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ class Payment
390390
}
391391
```
392392

393+
### Don't mix implementations
394+
395+
Enum instances cache is stored in a static variable. Choose one implementation for your project
396+
and stick to it, otherwise you may unexpectedly get errors because types don't match.
397+
398+
You may create your own project enum class and extend your chosen implementation, so if it ever needs to be
399+
changed it can be done in one place only (if storage APIs match).
400+
393401
## Related packages
394402

395403
* [ekvedaras/laravel-enum](https://packagist.org/packages/ekvedaras/laravel-enum)

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
},
3434
"require-dev": {
3535
"illuminate/support": "^6|^7",
36-
"phpunit/phpunit": "^8.5.8"
36+
"phpunit/phpunit": "^8.5.8",
37+
"voku/arrayy": "^7.0"
3738
},
3839
"suggest": {
39-
"illuminate/collections": "*",
40-
"illuminate/support": "*"
40+
"illuminate/collections": "Required for \\EKvedaras\\PHPEnum\\Illuminate\\Collection\\Enum implementation. Provides standalone Laravel collections",
41+
"illuminate/support": "Required for \\EKvedaras\\PHPEnum\\Illuminate\\Collection\\Enum implementation. Use if illuminate/collections can't be installed due to version contstraints",
42+
"voku/arrayy": "Required for \\EKvedaras\\PHPEnum\\Arrayy\\Enum implementation"
4143
},
4244
"scripts": {
4345
"test": "phpunit",

src/Arrayy/Enum.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace EKvedaras\PHPEnum\Arrayy;
4+
5+
use Arrayy\Arrayy;
6+
use EKvedaras\PHPEnum\BaseEnum;
7+
use EKvedaras\PHPEnum\Storage\GenericArrayAccessibleObjectStorage;
8+
9+
/**
10+
* Class Enum
11+
* @package EKvedaras\PHPEnum\Arrayy
12+
* @method static Arrayy|static[] enum()
13+
* @method static Arrayy|string[] options()
14+
* @method static Arrayy|int[]|string[] keys()
15+
*/
16+
class Enum extends BaseEnum
17+
{
18+
use GenericArrayAccessibleObjectStorage;
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
protected static function getNewStorage()
24+
{
25+
return new Arrayy();
26+
}
27+
}

src/Illuminate/Collection/Enum.php

+4-28
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
namespace EKvedaras\PHPEnum\Illuminate\Collection;
44

55
use EKvedaras\PHPEnum\BaseEnum;
6-
use EKvedaras\PHPEnum\Storage\ArrayAccessibleStorage;
6+
use EKvedaras\PHPEnum\Storage\GenericArrayAccessibleObjectStorage;
77
use Illuminate\Support\Collection;
88

99
/**
1010
* Class Enum
1111
* @package EKvedaras\PHPEnum\Illuminate\Collection
1212
* @method static Collection|static[] enum()
13+
* @method static Collection|string[] options()
14+
* @method static Collection|int[]|string[] keys()
1315
*/
1416
abstract class Enum extends BaseEnum
1517
{
16-
use ArrayAccessibleStorage;
18+
use GenericArrayAccessibleObjectStorage;
1719

1820
/**
1921
* @return Collection
@@ -22,30 +24,4 @@ protected static function getNewStorage()
2224
{
2325
return new Collection();
2426
}
25-
26-
/**
27-
* @return Collection|string[]
28-
*/
29-
public static function options()
30-
{
31-
return static::enum()->map(function (self $enum) {
32-
return $enum->name();
33-
});
34-
}
35-
36-
/**
37-
* @return Collection|int[]|string[]
38-
*/
39-
public static function keys()
40-
{
41-
return static::enum()->keys();
42-
}
43-
44-
/**
45-
* @inheritDoc
46-
*/
47-
public static function keyString(string $glue = ',')
48-
{
49-
return static::keys()->implode($glue);
50-
}
5127
}

src/PHPArray/Enum.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace EKvedaras\PHPEnum\PHPArray;
44

55
use EKvedaras\PHPEnum\BaseEnum;
6-
use EKvedaras\PHPEnum\Storage\ArrayAccessibleStorage;
6+
use EKvedaras\PHPEnum\Storage\GenericArrayAccessibleStorage;
77

88
/**
99
* Class Enum
1010
* @package EKvedaras\PHPEnum\PHPArray
1111
*/
1212
abstract class Enum extends BaseEnum
1313
{
14-
use ArrayAccessibleStorage;
14+
use GenericArrayAccessibleStorage;
1515

1616
/**
1717
* @inheritDoc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace EKvedaras\PHPEnum\Storage;
4+
5+
/**
6+
* Trait GenericArrayAccessibleObjectStorage
7+
* @package EKvedaras\PHPEnum\Storage
8+
*/
9+
trait GenericArrayAccessibleObjectStorage
10+
{
11+
use GenericArrayAccessibleStorage;
12+
13+
/**
14+
* @return string[]
15+
*/
16+
public static function options()
17+
{
18+
return static::enum()->map(function (self $enum) {
19+
return $enum->name();
20+
});
21+
}
22+
23+
/**
24+
* @return int[]|string[]
25+
*/
26+
public static function keys()
27+
{
28+
return static::enum()->keys();
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public static function keyString(string $glue = ',')
35+
{
36+
return static::keys()->implode($glue);
37+
}
38+
}

src/Storage/ArrayAccessibleStorage.php renamed to src/Storage/GenericArrayAccessibleStorage.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace EKvedaras\PHPEnum\Storage;
44

55
/**
6-
* Trait ArrayAccessibleStorage
7-
* @package EKvedaras\PHPEnum
6+
* Trait GenericArrayAccessibleStorage
7+
* @package EKvedaras\PHPEnum\Storage
88
*/
9-
trait ArrayAccessibleStorage
9+
trait GenericArrayAccessibleStorage
1010
{
1111
/**
1212
* @inheritDoc

tests/Arrayy/EnumTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Tests\Arrayy;
4+
5+
use Arrayy\Arrayy;
6+
use Tests\BaseEnumTest;
7+
use Tests\Enums\PaymentStatusArrayyEnum;
8+
use Tests\TestCase;
9+
10+
/**
11+
* Class EnumTest
12+
* @package Tests\Arrayy
13+
*/
14+
class EnumTest extends TestCase
15+
{
16+
/** @test */
17+
public function it_returns_arrayy_storage()
18+
{
19+
$this->assertInstanceOf(Arrayy::class, PaymentStatusArrayyEnum::enum());
20+
$this->assertInstanceOf(Arrayy::class, PaymentStatusArrayyEnum::options());
21+
$this->assertInstanceOf(Arrayy::class, PaymentStatusArrayyEnum::keys());
22+
}
23+
24+
/** @test */
25+
public function it_fetches_keys()
26+
{
27+
$this->assertEquals(array_values(BaseEnumTest::PAYMENT_STATUS_IDS), PaymentStatusArrayyEnum::keys()->toArray());
28+
}
29+
}

tests/BaseEnumTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use EKvedaras\PHPEnum\BaseEnum;
66
use OutOfBoundsException;
7-
use PHPUnit\Framework\TestCase;
87
use RuntimeException;
98
use Tests\Enums\PaymentStatusArrayEnum;
9+
use Tests\Enums\PaymentStatusArrayyEnum;
1010
use Tests\Enums\PaymentStatusIlluminateCollectionEnum;
1111
use Tests\Enums\PaymentStatusOptions;
1212

@@ -46,6 +46,7 @@ public function enums(): array
4646
return [
4747
'array-storage' => [PaymentStatusArrayEnum::class],
4848
'collection-storage' => [PaymentStatusIlluminateCollectionEnum::class],
49+
'arrayy' => [PaymentStatusArrayyEnum::class],
4950
];
5051
}
5152

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tests\Enums;
4+
5+
use EKvedaras\PHPEnum\Arrayy\Enum;
6+
7+
/**
8+
* Class PaymentStatusArrayyEnum
9+
* @package Tests\Enums
10+
*/
11+
class PaymentStatusArrayyEnum extends Enum
12+
{
13+
use PaymentStatusOptions;
14+
}

tests/Enums/PaymentStatusOptions.php

+5
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ public static function notPartOfEnumStatic()
5353
public function notPartOfEnum()
5454
{
5555
}
56+
57+
public static function clearCache()
58+
{
59+
static::$cache = null;
60+
}
5661
}

tests/Illuminate/Collection/EnumTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Tests\Illuminate\Collection;
44

55
use Illuminate\Support\Collection;
6-
use PHPUnit\Framework\TestCase;
76
use Tests\BaseEnumTest;
87
use Tests\Enums\PaymentStatusIlluminateCollectionEnum;
8+
use Tests\TestCase;
99

1010
/**
1111
* Class EnumTest

tests/PHPArray/EnumTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Tests\PHPArray;
44

5-
use PHPUnit\Framework\TestCase;
65
use Tests\Enums\PaymentStatusArrayEnum;
6+
use Tests\TestCase;
77

88
/**
99
* Class EnumTest

tests/TestCase.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
6+
use Tests\Enums\PaymentStatusArrayEnum;
7+
8+
/**
9+
* Class TestCase
10+
* @package Tests
11+
*/
12+
class TestCase extends BaseTestCase
13+
{
14+
public static function setUpBeforeClass(): void
15+
{
16+
PaymentStatusArrayEnum::clearCache();
17+
}
18+
}

0 commit comments

Comments
 (0)