Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
da26127
Change identifier to name
jodiedunlop Sep 14, 2018
6745120
Change static method valueFor() to valueForKey()
jodiedunlop Sep 14, 2018
df27a89
Add static method nameForKey()
jodiedunlop Sep 14, 2018
6400bb8
Change exists() and checkExists() to isValidKey() and requireValidKey…
jodiedunlop Sep 14, 2018
ce9166e
Improve keyForName()
jodiedunlop Sep 14, 2018
078f0a7
Fix error in key() when the key is not a string (eg. int)
jodiedunlop Sep 14, 2018
1f024a9
Fix is() instance method to handle non-string keys
jodiedunlop Sep 14, 2018
ad4f245
Fix late-static binding: change self references
jodiedunlop Sep 18, 2018
d159b97
Add Enum::instanceFromName() method
jodiedunlop Sep 19, 2018
10162b7
Change implementation of instanceFromKey() to use array_search
jodiedunlop Sep 19, 2018
c865499
Rename some internal variables
jodiedunlop Sep 19, 2018
434d78b
Removed flip() and renamed fromValue() to keyForValue()
jodiedunlop Sep 19, 2018
b1e9572
Change nameForKey implementation and throw on duplicate keys
jodiedunlop Sep 19, 2018
257ab25
Add uncommitted duplicate key classes
jodiedunlop Sep 19, 2018
549f8d3
Rename Enum::constantMap() method to namesAndKeys()
jodiedunlop Sep 21, 2018
f1e6c52
Update README to document API changes
jodiedunlop Sep 21, 2018
2031ec1
Add Enum::valueForName() method
jodiedunlop Sep 23, 2018
c616f17
Rename Bevs stub to Beverages
jodiedunlop Sep 23, 2018
903e409
Fix isValidName() to handle null keys
jodiedunlop Sep 23, 2018
32e71ee
Update CHANGELOG for 2.0.0 release
jodiedunlop Sep 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change static method valueFor() to valueForKey()
  • Loading branch information
jodiedunlop committed Sep 14, 2018
commit 6745120e1c103abbc9b6f0cdfe1551085ef52d21
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- **Breaking** Change `$instance->identifier` to `$instance->name()`
- **Breaking** Change `Enum::identifiers()` to `Enum::names()`
- **Breaking** Change `Enum::getKeyForIdentfier()` to `Enum::keyForName()`
- **Breaking** Change `Enum::valueFor()` to `Enum::valueForKey()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum::valueForName() for completeness?


# 1.1.0

Expand Down
4 changes: 2 additions & 2 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static function __callStatic($name, $arguments)
throw new InvalidEnumException("Invalid constant name '{$name}' in " . static::class);
}

return new static($name, $key, static::valueFor($key));
return new static($name, $key, static::valueForKey($key));
}

/**
Expand All @@ -180,7 +180,7 @@ public static function keyForName(string $name)
* @return mixed
* @throws InvalidKeyException
*/
public static function valueFor($key)
public static function valueForKey($key)
{
static::checkExists($key);

Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ public function test_get_values()
public function test_can_get_value_for_key()
{
// Not mapped
$this->assertEquals(null, Fruit::valueFor(Fruit::APPLE));
$this->assertEquals(null, Fruit::valueForKey(Fruit::APPLE));

// Mapped
$this->assertEquals('Kitty-cat', Animal::valueFor(Animal::CAT));
$this->assertEquals(null, Animal::valueFor(Animal::DOG));
$this->assertEquals(['you', 'filthy', 'animal'], Animal::valueFor('skyrat'));
$this->assertEquals('Kitty-cat', Animal::valueForKey(Animal::CAT));
$this->assertEquals(null, Animal::valueForKey(Animal::DOG));
$this->assertEquals(['you', 'filthy', 'animal'], Animal::valueForKey('skyrat'));
}

public function test_value_for_invalid_key_throws_exception()
{
$this->expectException(InvalidKeyException::class);
Fruit::valueFor('_does_not_exist_');
Fruit::valueForKey('_does_not_exist_');
}

public function test_can_instantiate_instance()
Expand Down