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

Commit

Permalink
Implemented memberOrNullBy and variants. Closes #18.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Feb 14, 2014
1 parent 188bb27 commit 2e267dc
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 127 deletions.
115 changes: 76 additions & 39 deletions src/AbstractMultiton.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@
*/
abstract class AbstractMultiton implements MultitonInterface
{
/**
* Returns an array of all members in this multiton.
*
* @return array<string,MultitonInterface> All members.
*/
final public static function members()
{
$class = get_called_class();
if (!array_key_exists($class, self::$members)) {
self::$members[$class] = array();
static::initializeMembers();
}

return self::$members[$class];
}

/**
* Returns a single member by string key.
*
Expand Down Expand Up @@ -147,41 +131,40 @@ function (MultitonInterface $member) use (
}

/**
* Returns a set of members by comparison with the result of an accessor
* method.
* Returns a single member by comparison with the result of an accessor
* method. Additionally returns null if the supplied value is null.
*
* @param string $property The name of the property (accessor method) to match.
* @param mixed $value The value to match.
* @param mixed $value The value to match, or null.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
*
* @return array<string,MultitonInterface> All members for which $member->{$property}() === $value.
* @return MultitonInterface|null The first member for which $member->{$property}() === $value, or null if the supplied value is null.
* @throws Exception\UndefinedMemberExceptionInterface If no associated member is found.
*/
final public static function membersBy(
final public static function memberOrNullBy(
$property,
$value,
$isCaseSensitive = null
) {
if (null === $isCaseSensitive) {
$isCaseSensitive = true;
}
if (!$isCaseSensitive && is_scalar($value)) {
$value = strtoupper(strval($value));
}
$member = static::memberByWithDefault(
$property,
$value,
null,
$isCaseSensitive
);
if (null === $member) {
if (null === $value) {
return null;
}

return static::membersByPredicate(
function (MultitonInterface $member) use (
throw static::createUndefinedMemberException(
get_called_class(),
$property,
$value,
$isCaseSensitive
) {
$memberValue = $member->{$property}();
if (!$isCaseSensitive && is_scalar($memberValue)) {
$memberValue = strtoupper(strval($memberValue));
}
$value
);
}

return $memberValue === $value;
}
);
return $member;
}

/**
Expand Down Expand Up @@ -228,6 +211,60 @@ final public static function memberByPredicateWithDefault(
return $default;
}

/**
* Returns an array of all members in this multiton.
*
* @return array<string,MultitonInterface> All members.
*/
final public static function members()
{
$class = get_called_class();
if (!array_key_exists($class, self::$members)) {
self::$members[$class] = array();
static::initializeMembers();
}

return self::$members[$class];
}

/**
* Returns a set of members by comparison with the result of an accessor
* method.
*
* @param string $property The name of the property (accessor method) to match.
* @param mixed $value The value to match.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
*
* @return array<string,MultitonInterface> All members for which $member->{$property}() === $value.
*/
final public static function membersBy(
$property,
$value,
$isCaseSensitive = null
) {
if (null === $isCaseSensitive) {
$isCaseSensitive = true;
}
if (!$isCaseSensitive && is_scalar($value)) {
$value = strtoupper(strval($value));
}

return static::membersByPredicate(
function (MultitonInterface $member) use (
$property,
$value,
$isCaseSensitive
) {
$memberValue = $member->{$property}();
if (!$isCaseSensitive && is_scalar($memberValue)) {
$memberValue = strtoupper(strval($memberValue));
}

return $memberValue === $value;
}
);
}

/**
* Returns a set of members by predicate callback.
*
Expand Down
42 changes: 29 additions & 13 deletions src/AbstractValueMultiton.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ final public static function memberByValue($value, $isCaseSensitive = null)
return static::memberBy('value', $value, $isCaseSensitive);
}

/**
* Returns a set of members matching the supplied value.
*
* @param scalar $value The value associated with the members.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
*
* @return array<string,ValueMultitonInterface> All members with the supplied value.
*/
final public static function membersByValue($value, $isCaseSensitive = null)
{
return static::membersBy('value', $value, $isCaseSensitive);
}

/**
* Returns a single member by value. Additionally returns a default if no
* associated member is found.
Expand All @@ -67,6 +54,35 @@ final public static function memberByValueWithDefault(
);
}

/**
* Returns a single member by value.
*
* @param scalar|null $value The value associated with the member, or null.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
*
* @return ValueMultitonInterface|null The first member with the supplied value, or null if the supplied value is null.
* @throws Exception\UndefinedMemberException If no associated member is found.
*/
final public static function memberOrNullByValue(
$value,
$isCaseSensitive = null
) {
return static::memberOrNullBy('value', $value, $isCaseSensitive);
}

/**
* Returns a set of members matching the supplied value.
*
* @param scalar $value The value associated with the members.
* @param boolean|null $isCaseSensitive True if the search should be case sensitive.
*
* @return array<string,ValueMultitonInterface> All members with the supplied value.
*/
final public static function membersByValue($value, $isCaseSensitive = null)
{
return static::membersBy('value', $value, $isCaseSensitive);
}

/**
* Returns the value of this member.
*
Expand Down
56 changes: 42 additions & 14 deletions test/suite/AbstractEnumerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ protected function setUp()

// Multiton tests ==========================================================

public function testMembers()
{
$this->assertSame(array(
'BAZ' => ValidEnumeration::BAZ(),
'FOO' => ValidEnumeration::FOO(),
'BAR' => ValidEnumeration::BAR(),
), ValidEnumeration::members());
}

public function testMemberByKey()
{
$foo = ValidEnumeration::memberByKey('FOO');
Expand Down Expand Up @@ -101,13 +92,18 @@ public function testMemberByWithDefault()
$this->assertNull(ValidEnumeration::memberByWithDefault('key', 'qux'));
}

public function testMembersBy()
public function testMemberOrNullBy()
{
$foo = ValidEnumeration::membersBy('value', 'oof');
$bar = ValidEnumeration::membersBy('value', 'RAB', false);
$this->assertSame(ValidEnumeration::FOO(), ValidEnumeration::memberOrNullBy('key', 'FOO'));
$this->assertSame(ValidEnumeration::BAR(), ValidEnumeration::memberOrNullBy('key', 'BAR'));
$this->assertSame(ValidEnumeration::FOO(), ValidEnumeration::memberOrNullBy('key', 'Foo', false));
$this->assertNull(ValidEnumeration::memberOrNullBy('key', null));
}

$this->assertSame(array('FOO' => ValidEnumeration::FOO()), $foo);
$this->assertSame(array('BAR' => ValidEnumeration::BAR()), $bar);
public function testMemberOrNullByFailureUndefined()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedMemberException');
ValidEnumeration::memberOrNullBy('key', 'DOOM');
}

public function testMemberByPredicate()
Expand Down Expand Up @@ -170,6 +166,24 @@ function (ValidEnumeration $member) {
$this->assertNull($defaultNull);
}

public function testMembers()
{
$this->assertSame(array(
'BAZ' => ValidEnumeration::BAZ(),
'FOO' => ValidEnumeration::FOO(),
'BAR' => ValidEnumeration::BAR(),
), ValidEnumeration::members());
}

public function testMembersBy()
{
$foo = ValidEnumeration::membersBy('value', 'oof');
$bar = ValidEnumeration::membersBy('value', 'RAB', false);

$this->assertSame(array('FOO' => ValidEnumeration::FOO()), $foo);
$this->assertSame(array('BAR' => ValidEnumeration::BAR()), $bar);
}

public function testMembersByPredicate()
{
$notBaz = ValidEnumeration::membersByPredicate(
Expand Down Expand Up @@ -280,6 +294,20 @@ public function testMemberByValueWithDefault()
$this->assertNull(ValidEnumeration::memberByValueWithDefault('qux'));
}

public function testMemberOrNullByValue()
{
$this->assertSame(ValidEnumeration::FOO(), ValidEnumeration::memberOrNullByValue('oof'));
$this->assertSame(ValidEnumeration::BAR(), ValidEnumeration::memberOrNullByValue('rab'));
$this->assertSame(ValidEnumeration::FOO(), ValidEnumeration::memberOrNullByValue('Oof', false));
$this->assertNull(ValidEnumeration::memberOrNullByValue(null));
}

public function testMemberOrNullByValueFailureUndefined()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedMemberException');
ValidEnumeration::memberOrNullByValue('mood');
}

public function testMembersByValue()
{
$this->assertSame(array('FOO' => ValidEnumeration::FOO()), ValidEnumeration::membersByValue('oof'));
Expand Down
66 changes: 40 additions & 26 deletions test/suite/AbstractMultitonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ protected function setUp()

// Multiton tests ==========================================================

public function testMembers()
{
$this->assertSame(
array(
'FOO' => ValidMultiton::FOO(),
'BAR' => ValidMultiton::BAR(),
'BAZ' => ValidMultiton::BAZ(),
),
ValidMultiton::members()
);
}

public function testMemberByKey()
{
$this->assertSame(array(), ValidMultiton::calls());
Expand Down Expand Up @@ -118,22 +106,18 @@ public function testMemberByWithDefault()
$this->assertNull(ValidMultiton::memberByWithDefault('key', 'qux'));
}

public function testMembersBy()
public function testMemberOrNullBy()
{
$this->assertSame(array(), ValidMultiton::calls());

$foo = ValidMultiton::membersBy('value', 'oof');
$bar = ValidMultiton::membersBy('value', 'RAB', false);

$this->assertSame(array('FOO' => ValidMultiton::FOO()), $foo);
$this->assertSame(array('BAR' => ValidMultiton::BAR()), $bar);
$this->assertSame(ValidMultiton::FOO(), ValidMultiton::memberOrNullBy('key', 'FOO'));
$this->assertSame(ValidMultiton::BAR(), ValidMultiton::memberOrNullBy('key', 'BAR'));
$this->assertSame(ValidMultiton::FOO(), ValidMultiton::memberOrNullBy('key', 'Foo', false));
$this->assertNull(ValidMultiton::memberOrNullBy('key', null));
}

$this->assertSame(array(
array(
'Eloquent\Enumeration\Test\Fixture\ValidMultiton::initializeMembers',
array(),
),
), ValidMultiton::calls());
public function testMemberOrNullByFailureUndefined()
{
$this->setExpectedException('Eloquent\Enumeration\Exception\UndefinedMemberException');
ValidMultiton::memberOrNullBy('key', 'DOOM');
}

public function testMemberByPredicate()
Expand Down Expand Up @@ -205,6 +189,36 @@ function (ValidMultiton $member) {
$this->assertNull($defaultNull);
}

public function testMembers()
{
$this->assertSame(
array(
'FOO' => ValidMultiton::FOO(),
'BAR' => ValidMultiton::BAR(),
'BAZ' => ValidMultiton::BAZ(),
),
ValidMultiton::members()
);
}

public function testMembersBy()
{
$this->assertSame(array(), ValidMultiton::calls());

$foo = ValidMultiton::membersBy('value', 'oof');
$bar = ValidMultiton::membersBy('value', 'RAB', false);

$this->assertSame(array('FOO' => ValidMultiton::FOO()), $foo);
$this->assertSame(array('BAR' => ValidMultiton::BAR()), $bar);

$this->assertSame(array(
array(
'Eloquent\Enumeration\Test\Fixture\ValidMultiton::initializeMembers',
array(),
),
), ValidMultiton::calls());
}

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

0 comments on commit 2e267dc

Please sign in to comment.