Skip to content

Commit 5b0babd

Browse files
author
Kevin Buchholz
authored
Merge pull request #4 from sourceboat/feature/2_is_helper
Add `is(EnumValue)` and `is<EnumValue>()` helper to enums
2 parents 1ec515d + f9e971d commit 5b0babd

File tree

8 files changed

+137
-14
lines changed

8 files changed

+137
-14
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ Returns the localized version of the value, default path is `enums.<EnumClass>.<
127127
UserType::SuperAdministrator()->localized(); // Returns for example 'Super Administrator', but `enums.App\Enums\UserType.3` when not set.
128128
```
129129

130+
### is(static): bool
131+
132+
Check if the instance is equal to the given member.
133+
134+
``` php
135+
UserType::SuperAdministrator()->is(UserType::Moderator()); // -> false
136+
UserType::SuperAdministrator()->is(UserType::SuperAdministrator()); // -> true
137+
```
138+
139+
### is<Enum_Value>(): bool
140+
141+
Check if the instance is equal to the member indicated by the method name.
142+
143+
``` php
144+
UserType::SuperAdministrator()->isModerator(); // -> false
145+
UserType::SuperAdministrator()->isSuperAdministrator(); // -> true
146+
UserType::SuperAdministrator()->isStudent(); // -> throws Eloquent\Enumeration\Exception\UndefinedMemberException
147+
```
148+
130149
### static randomMember(): static
131150

132151
Returns a random member from the enum. Useful for factories.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"test": "vendor/bin/phpunit",
5151
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
5252
"lint:phpcs": "vendor/bin/phpcs -s",
53-
"lint:phpmd": "vendor/bin/phpmd src,tests text phpmd.xml"
53+
"lint:phpmd": "vendor/bin/phpmd src,tests text phpmd.xml",
54+
"fix": "vendor/bin/phpcbf"
5455
},
5556
"extra": {
5657
"branch-alias": {

resources/stubs/Enumeration.stub

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ namespace DummyNamespace;
55
use Sourceboat\Enumeration\Enumeration;
66

77
/**
8-
* @method static \DummyNamespace\DummyClass OptionOne()
9-
* @method static \DummyNamespace\DummyClass OptionTwo()
10-
* @method static \DummyNamespace\DummyClass OptionThree()
8+
* @method static self OptionOne()
9+
* @method static self OptionTwo()
10+
* @method static self OptionThree()
11+
* @method bool isOptionOne()
12+
* @method bool isOptionTwo()
13+
* @method bool isOptionThree()
1114
*/
1215
final class DummyClass extends Enumeration
1316
{

src/Enumeration.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sourceboat\Enumeration;
44

55
use Eloquent\Enumeration\AbstractEnumeration;
6+
use Illuminate\Support\Str;
67
use Sourceboat\Enumeration\Rules\EnumerationValue;
78

89
/**
@@ -39,6 +40,17 @@ public function localized(): string
3940
return trans(static::getLocalizationPath() . '.' . $this->value());
4041
}
4142

43+
/**
44+
* Check if this instance equals to a specific member of the enum.
45+
*
46+
* @param static $value The member to check for
47+
* @return bool
48+
*/
49+
public function is($value): bool
50+
{
51+
return $this === $value;
52+
}
53+
4254
/**
4355
* Get all values in this enumeration.
4456
*
@@ -168,10 +180,25 @@ public static function defaultMember()
168180
* Returns a string representation of this member.
169181
*
170182
* @return string The string representation.
171-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
172183
*/
173-
public function __toString()
184+
public function __toString(): string
174185
{
175186
return (string) $this->value();
176187
}
188+
189+
/**
190+
* Implements `is<EnumKey>` methods for the enum.
191+
*
192+
* @param string $method
193+
* @param array $arguments
194+
* @return mixed
195+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
196+
*/
197+
public function __call($method, $arguments)
198+
{
199+
if (Str::startsWith($method, 'is')) {
200+
$key = Str::after($method, 'is');
201+
return $this->is(static::memberByKey($key, false));
202+
}
203+
}
177204
}

tests/FruitType.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
use Sourceboat\Enumeration\Enumeration;
66

77
/**
8-
* @method static \Sourceboat\Enumeration\Tests\FruitType BERRY()
9-
* @method static \Sourceboat\Enumeration\Tests\FruitType NUT()
10-
* @method static \Sourceboat\Enumeration\Tests\FruitType ACCESSORY_FRUIT()
11-
* @method static \Sourceboat\Enumeration\Tests\FruitType LEGUME()
8+
* @method static self BERRY()
9+
* @method static self NUT()
10+
* @method static self ACCESSORY_FRUIT()
11+
* @method static self LEGUME()
12+
* @method bool isBerry()
13+
* @method bool isNut()
14+
* @method bool isAccessory_Fruit()
15+
* @method bool isLegume()
1216
*/
1317
class FruitType extends Enumeration
1418
{

tests/UserRole.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
use Sourceboat\Enumeration\Enumeration;
66

77
/**
8-
* @method static \Sourceboat\Enumeration\Tests\UserRole MODERATOR()
9-
* @method static \Sourceboat\Enumeration\Tests\UserRole ADMIN()
10-
* @method static \Sourceboat\Enumeration\Tests\UserRole SUPER_ADMIN()
11-
* @method static \Sourceboat\Enumeration\Tests\UserRole USER()
8+
* @method static self MODERATOR()
9+
* @method static self ADMIN()
10+
* @method static self SUPER_ADMIN()
11+
* @method static self USER()
12+
* @method bool isModerator()
13+
* @method bool isAdmin()
14+
* @method bool isSuper_Admin()
15+
* @method bool isUser()
1216
*/
1317
class UserRole extends Enumeration
1418
{

tests/unit/IsEnumKeyTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Sourceboat\Enumeration\Tests;
4+
5+
class IsEnumKeyTest extends TestCase
6+
{
7+
/**
8+
* Data provider for the test `testIsEnumKey`.
9+
*
10+
* @return array
11+
*/
12+
public function dataProvider(): array
13+
{
14+
return [
15+
[ UserRole::USER(), 'isUser', true ],
16+
[ UserRole::USER(), 'isUSER', true ],
17+
[ UserRole::USER(), 'isuser', true ],
18+
[ UserRole::USER(), 'isUSer', true ],
19+
[ UserRole::MODERATOR(), 'isUser', false ],
20+
];
21+
}
22+
23+
/**
24+
* Test functionality of is<EnumKey> helper.
25+
*
26+
* @dataProvider dataProvider
27+
* @param \Sourceboat\Enumeration\Tests\UserRole $role
28+
* @param string $method
29+
* @param bool $result
30+
* @return void
31+
*/
32+
public function testIsEnumKey(UserRole $role, string $method, bool $result): void
33+
{
34+
$this->assertEquals($result, $role->{$method}());
35+
}
36+
37+
/**
38+
* Test functionality of is<EnumKey> helper.
39+
*
40+
* @expectedException \Eloquent\Enumeration\Exception\UndefinedMemberException
41+
* @return void
42+
*/
43+
public function testIsEnumKeyException(): void
44+
{
45+
UserRole::USER()->isStudent();
46+
}
47+
}

tests/unit/IsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Sourceboat\Enumeration\Tests;
4+
5+
class IsTest extends TestCase
6+
{
7+
/**
8+
* Check that the is helper functions correctly.
9+
*
10+
* @return void
11+
*/
12+
public function testGetCorrectResults(): void
13+
{
14+
$this->assertTrue(UserRole::MODERATOR()->is(UserRole::MODERATOR()));
15+
$this->assertFalse(UserRole::MODERATOR()->is(UserRole::ADMIN()));
16+
$this->assertFalse(UserRole::MODERATOR()->is('moderator'));
17+
}
18+
}

0 commit comments

Comments
 (0)