From 33d98cb126af03459dff6e57346779970c988d85 Mon Sep 17 00:00:00 2001 From: Sergey Predvoditelev Date: Tue, 29 Aug 2017 14:12:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE:=20=D0=B2=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D1=8B=D1=85=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D1=8F?= =?UTF-8?q?=D1=85=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=B2=D0=BE=D0=B4=D0=B8=D0=BB=D0=B8=D1=81?= =?UTF-8?q?=D1=8C=20=D0=BA=20=D1=82=D0=B8=D0=BF=D1=83=20integer,=20=D1=87?= =?UTF-8?q?=D1=82=D0=BE=20=D0=BF=D1=80=D0=B8=D0=B2=D0=BE=D0=B4=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=20=D0=BA=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=80=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=BD=D0=BE=D0=B9=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0.=20=D0=9F=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC=D0=B0=20=D1=81=D0=B2=D1=8F=D0=B7?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20=D1=81=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D1=82=D0=B8=D0=BF=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=B9=20=D0=BC=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D0=B2=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Enum.php | 27 +++++++++++++++------------ composer.json | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Enum.php b/Enum.php index b57da9d..ccebbd4 100644 --- a/Enum.php +++ b/Enum.php @@ -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) { @@ -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; } diff --git a/composer.json b/composer.json index 0b53548..0c22715 100644 --- a/composer.json +++ b/composer.json @@ -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",