Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Implemented memberOrNullByKey. Relates to #18.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Feb 14, 2014
1 parent 2e267dc commit bd6ce7a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/AbstractMultiton.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ final public static function memberByKeyWithDefault(
);
}

/**
* Returns a single member by string key. Additionally returns null if the
* supplied key is null.
*
* @param string|null $key The string key associated with the member, or null.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
*
* @return MultitonInterface|null The member associated with the given string key, or null if the supplied key is null.
* @throws Exception\UndefinedMemberExceptionInterface If no associated member is found.
*/
final public static function memberOrNullByKey(
$key,
$isCaseSensitive = null
) {
return static::memberOrNullBy('key', $key, $isCaseSensitive);
}

/**
* Returns a single member by comparison with the result of an accessor
* method.
Expand Down
3 changes: 2 additions & 1 deletion src/AbstractValueMultiton.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ final public static function memberByValueWithDefault(
}

/**
* Returns a single member by value.
* Returns a single member by value. Additionally returns null if the
* supplied value is null.
*
* @param scalar|null $value The value associated with the member, or null.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
Expand Down
14 changes: 14 additions & 0 deletions test/suite/AbstractMultitonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public function testMemberByKeyWithDefault()
$this->assertNull(ValidMultiton::memberByKeyWithDefault('qux'));
}

public function testMemberOrNullByKey()
{
$this->assertSame(ValidMultiton::FOO(), ValidMultiton::memberOrNullByKey('FOO'));
$this->assertSame(ValidMultiton::BAR(), ValidMultiton::memberOrNullByKey('BAR'));
$this->assertSame(ValidMultiton::FOO(), ValidMultiton::memberOrNullByKey('Foo', false));
$this->assertNull(ValidMultiton::memberOrNullByKey(null));
}

public function testMemberOrNullByKeyFailureUndefined()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedMemberException');
ValidMultiton::memberOrNullByKey('DOOM');
}

public function testMemberBy()
{
$this->assertSame(array(), ValidMultiton::calls());
Expand Down
14 changes: 14 additions & 0 deletions test/suite/AbstractValueMultitonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public function testMemberByKeyWithDefault()
$this->assertNull(ValidValueMultiton::memberByKeyWithDefault('qux'));
}

public function testMemberOrNullByKey()
{
$this->assertSame(ValidValueMultiton::FOO(), ValidValueMultiton::memberOrNullByKey('FOO'));
$this->assertSame(ValidValueMultiton::BAR(), ValidValueMultiton::memberOrNullByKey('BAR'));
$this->assertSame(ValidValueMultiton::FOO(), ValidValueMultiton::memberOrNullByKey('Foo', false));
$this->assertNull(ValidValueMultiton::memberOrNullByKey(null));
}

public function testMemberOrNullByKeyFailureUndefined()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedMemberException');
ValidValueMultiton::memberOrNullByKey('DOOM');
}

public function testMemberBy()
{
$this->assertSame(array(), ValidValueMultiton::calls());
Expand Down

0 comments on commit bd6ce7a

Please sign in to comment.