Skip to content

Commit

Permalink
Ignore private and protected constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Shira-3749 committed Feb 7, 2018
1 parent 4263f70 commit d7dc0ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
#########

1.0.1
*****

- ignore private and protected constants


1.0.0
*****

Expand Down
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Defining an enum class
const SUNDAY = 6;
}
.. NOTE::

Private and protected constants are ignored.


Supported value types
=====================
Expand Down Expand Up @@ -198,7 +202,7 @@ Creating enum instances

Instances created by ``fromValue()``, ``fromKey()`` and the static magic factory
methods are cached internally and reused.

Multiple calls to the factory methods with the same value or key will yield
the same instance.

Expand Down
12 changes: 10 additions & 2 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,16 @@ protected static function loadValueToKeyMap()
*/
protected static function determineKeyToValueMap(): array
{
// use all constants of current class
return (new \ReflectionClass(static::class))->getConstants();
// use all public constants of current class
$keyToValueMap = [];

foreach ((new \ReflectionClass(static::class))->getReflectionConstants() as $constant) {
if ($constant->isPublic()) {
$keyToValueMap[$constant->name] = $constant->getValue();
}
}

return $keyToValueMap;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/TestSubject/TestEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ class TestEnum extends Enum
const LOREM = 'foo';
const IPSUM = 123;
const DOLOR = null;

protected const SIT = 'protected const should be ignored';
private const AMET = 'private const should be ignored';
}

0 comments on commit d7dc0ca

Please sign in to comment.