Skip to content

Commit

Permalink
Исправлено: в некоторых случаях значения приводились к типу integer, …
Browse files Browse the repository at this point in the history
…что приводило к некорректной работе класса. Проблема связана с приведением типа ключей массива.
  • Loading branch information
vjik committed Aug 29, 2017
1 parent 78cacf8 commit 33d98cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,22 @@ public static function toArray(array $filter = [])
{
$class = get_called_class();
if (!array_key_exists($class, static::$_cache)) {
$reflection = new \ReflectionClass($class);
if (is_callable([$class, 'items'])) {
/** @noinspection PhpUndefinedMethodInspection */
$items = $class::items();
array_walk($items, function (&$item) {
$item = is_array($item) ? $item : ['name' => $item];
});
} else {
$items = [];
$reflection = new \ReflectionClass($class);
foreach ($reflection->getConstants() as $constant) {
$items[$constant] = $constant;
}
$items = array_fill_keys($reflection->getConstants(), []);
}
array_walk($items, function (&$item, $value) {
$item = is_array($item) ? $item : ['name' => $item];
if (!isset($item['name'])) {
$item['name'] = $value;
foreach ($reflection->getConstants() as $constant) {
if (!isset($items[$constant]['name'])) {
$items[$constant]['name'] = $constant;
}
$item['value'] = $value;
});
$items[$constant]['value'] = $constant;
}
static::$_cache[$class] = $items;
}
$items = array_filter(static::$_cache[$class], function ($item) use ($filter) {
Expand Down Expand Up @@ -153,7 +152,11 @@ public static function toArray(array $filter = [])
*/
public static function toValues(array $filter = [])
{
return array_keys(static::toArray($filter));
$values = [];
foreach (static::toArray($filter) as $item) {
$values[] = $item['value'];
}
return $values;
}


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vjik/php-enum",
"description": "PHP 5.4+ Enum implementation",
"version": "1.1.0",
"version": "1.1.1",
"type": "library",
"keywords": [
"php",
Expand Down

0 comments on commit 33d98cb

Please sign in to comment.