Skip to content

Commit

Permalink
introduced check for already requested member, renamed collection met…
Browse files Browse the repository at this point in the history
…hods from XOneOf to XIn
  • Loading branch information
thunderer committed Jan 29, 2024
1 parent 795e681 commit 334fb90
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
17 changes: 12 additions & 5 deletions src/Enum/EnumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ final public static function valueExists($value): bool
}

/** @param list<string> $members */
final public static function oneOfMembersExists(array $members): bool
final public static function membersExist(array $members): bool
{
static::resolveMembers();

return [] !== array_intersect(array_keys(static::$members[static::class]), $members);
}

/** @param list<int|string> $values */
final public static function oneOfValuesExists(array $values): bool
final public static function valuesExist(array $values): bool
{
static::resolveMembers();

Expand All @@ -216,23 +216,30 @@ final public function hasValue($value): bool
}

/** @param list<string> $members */
final public function hasOneOfMembers(array $members): bool
final public function hasMemberIn(array $members): bool
{
return in_array($this->member, $members, true);
}

/** @param list<int|string> $values */
final public function hasOneOfValues(array $values): bool
final public function hasValueIn(array $values): bool
{
return in_array($this->value, $values, true);
}

/** @param list<static> $enums */
final public function isOneOfInstances(array $enums): bool
final public function isIn(array $enums): bool
{
return in_array($this, $enums, true);
}

final public static function isMemberWarm(string $member): bool
{
return self::memberExists($member)
&& array_key_exists(static::class, static::$instances)
&& array_key_exists($member, static::$instances[static::class]);
}

/* --- INFO --- */

/** @return int|string */
Expand Down
68 changes: 41 additions & 27 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,26 @@ public function testHasValue(): void
$this->assertFalse($enum::SECOND()->hasValue(1));
}

public function testHasOneOfMembers(): void
public function testHasMemberIn(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::SECOND()->hasOneOfMembers(['FIRST', 'SECOND']));
$this->assertTrue($enum::SECOND()->hasOneOfMembers(['SECOND', 'THIRD']));
$this->assertFalse($enum::SECOND()->hasOneOfMembers(['THIRD', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasOneOfMembers(['FIRST', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasOneOfMembers([1.5, null, 'invalid']));
$this->assertTrue($enum::SECOND()->hasMemberIn(['FIRST', 'SECOND']));
$this->assertTrue($enum::SECOND()->hasMemberIn(['SECOND', 'THIRD']));
$this->assertFalse($enum::SECOND()->hasMemberIn(['THIRD', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasMemberIn(['FIRST', 'FOURTH']));
$this->assertFalse($enum::SECOND()->hasMemberIn([1.5, null, 'invalid']));
}

public function testHasOneOfValues(): void
public function testHasValueIn(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::SECOND()->hasOneOfValues([1, 2]));
$this->assertTrue($enum::SECOND()->hasOneOfValues([2, 3]));
$this->assertFalse($enum::SECOND()->hasOneOfValues([1, 4]));
$this->assertFalse($enum::SECOND()->hasOneOfValues([3, 4]));
$this->assertFalse($enum::SECOND()->hasOneOfValues([1.5, null, 'invalid']));
$this->assertTrue($enum::SECOND()->hasValueIn([1, 2]));
$this->assertTrue($enum::SECOND()->hasValueIn([2, 3]));
$this->assertFalse($enum::SECOND()->hasValueIn([1, 4]));
$this->assertFalse($enum::SECOND()->hasValueIn([3, 4]));
$this->assertFalse($enum::SECOND()->hasValueIn([1.5, null, 'invalid']));
}

public function testMemberExists(): void
Expand All @@ -303,34 +303,48 @@ public function testValueExists(): void
$this->assertFalse($enum::valueExists(3));
}

public function testOneOfMembersExists(): void
public function testMembersExist(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::oneOfMembersExists(['FIRST', 'SECOND']));
$this->assertTrue($enum::oneOfMembersExists(['FIRST', 'THIRD']));
$this->assertFalse($enum::oneOfMembersExists(['THIRD', 'FOURTH']));
$this->assertFalse($enum::oneOfMembersExists([1.5, null, 'invalid']));
$this->assertTrue($enum::membersExist(['FIRST', 'SECOND']));
$this->assertTrue($enum::membersExist(['FIRST', 'THIRD']));
$this->assertFalse($enum::membersExist(['THIRD', 'FOURTH']));
$this->assertFalse($enum::membersExist([1.5, null, 'invalid']));
}

public function testOneOfValuesExists(): void
public function testValuesExist(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::oneOfValuesExists([1, 2]));
$this->assertTrue($enum::oneOfValuesExists([1, 3]));
$this->assertFalse($enum::oneOfValuesExists([3, 4]));
$this->assertFalse($enum::oneOfValuesExists([1.5, null, 'invalid']));
$this->assertTrue($enum::valuesExist([1, 2]));
$this->assertTrue($enum::valuesExist([1, 3]));
$this->assertFalse($enum::valuesExist([3, 4]));
$this->assertFalse($enum::valuesExist([1.5, null, 'invalid']));
}

public function testIsOneOfInstances(): void
public function testIsIn(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertTrue($enum::SECOND()->isOneOfInstances([$enum::FIRST(), $enum::SECOND()]));
$this->assertTrue($enum::SECOND()->isOneOfInstances([$enum::SECOND(), new \stdClass()]));
$this->assertFalse($enum::SECOND()->isOneOfInstances([new \stdClass(), new \DateTimeImmutable()]));
$this->assertFalse($enum::SECOND()->isOneOfInstances([1.5, null, 'invalid']));
$this->assertTrue($enum::SECOND()->isIn([$enum::FIRST(), $enum::SECOND()]));
$this->assertTrue($enum::SECOND()->isIn([$enum::SECOND(), new \stdClass()]));
$this->assertFalse($enum::SECOND()->isIn([new \stdClass(), new \DateTimeImmutable()]));
$this->assertFalse($enum::SECOND()->isIn([1.5, null, 'invalid']));
}

public function testIsMemberWarm(): void
{
$enum = $this->makeRawEnum(['FIRST' => 1, 'SECOND' => 2]);

$this->assertFalse($enum::isMemberWarm('FIRST'));
$this->assertFalse($enum::isMemberWarm('SECOND'));
$enum::FIRST();
$this->assertTrue($enum::isMemberWarm('FIRST'));
$this->assertFalse($enum::isMemberWarm('SECOND'));
$enum::SECOND();
$this->assertTrue($enum::isMemberWarm('FIRST'));
$this->assertTrue($enum::isMemberWarm('SECOND'));
}

/* --- CONVERT --- */
Expand Down

0 comments on commit 334fb90

Please sign in to comment.