Skip to content

Commit 33d98cb

Browse files
committed
Исправлено: в некоторых случаях значения приводились к типу integer, что приводило к некорректной работе класса. Проблема связана с приведением типа ключей массива.
1 parent 78cacf8 commit 33d98cb

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

Enum.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,22 @@ public static function toArray(array $filter = [])
7171
{
7272
$class = get_called_class();
7373
if (!array_key_exists($class, static::$_cache)) {
74+
$reflection = new \ReflectionClass($class);
7475
if (is_callable([$class, 'items'])) {
7576
/** @noinspection PhpUndefinedMethodInspection */
7677
$items = $class::items();
78+
array_walk($items, function (&$item) {
79+
$item = is_array($item) ? $item : ['name' => $item];
80+
});
7781
} else {
78-
$items = [];
79-
$reflection = new \ReflectionClass($class);
80-
foreach ($reflection->getConstants() as $constant) {
81-
$items[$constant] = $constant;
82-
}
82+
$items = array_fill_keys($reflection->getConstants(), []);
8383
}
84-
array_walk($items, function (&$item, $value) {
85-
$item = is_array($item) ? $item : ['name' => $item];
86-
if (!isset($item['name'])) {
87-
$item['name'] = $value;
84+
foreach ($reflection->getConstants() as $constant) {
85+
if (!isset($items[$constant]['name'])) {
86+
$items[$constant]['name'] = $constant;
8887
}
89-
$item['value'] = $value;
90-
});
88+
$items[$constant]['value'] = $constant;
89+
}
9190
static::$_cache[$class] = $items;
9291
}
9392
$items = array_filter(static::$_cache[$class], function ($item) use ($filter) {
@@ -153,7 +152,11 @@ public static function toArray(array $filter = [])
153152
*/
154153
public static function toValues(array $filter = [])
155154
{
156-
return array_keys(static::toArray($filter));
155+
$values = [];
156+
foreach (static::toArray($filter) as $item) {
157+
$values[] = $item['value'];
158+
}
159+
return $values;
157160
}
158161

159162

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vjik/php-enum",
33
"description": "PHP 5.4+ Enum implementation",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"type": "library",
66
"keywords": [
77
"php",

0 commit comments

Comments
 (0)