Skip to content

Commit edc281b

Browse files
committed
Добавлена статическая функция get
1 parent a37e3bc commit edc281b

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

Enum.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ abstract class Enum
1717
/**
1818
* @var array
1919
*/
20-
protected static $_cache = [];
20+
protected static $_cacheItems = [];
21+
22+
/**
23+
* @var array
24+
*/
25+
protected static $_cacheInstances = [];
2126

2227
/**
2328
* @var int|string
@@ -44,6 +49,22 @@ public function __construct($id)
4449
}
4550
}
4651

52+
/**
53+
* @param int|string $id
54+
* @return static
55+
* @throws ReflectionException
56+
* @throws UnexpectedValueException
57+
* @since 2.1.0
58+
*/
59+
public static function get($id)
60+
{
61+
$key = get_called_class() . '~' . $id;
62+
if (empty(static::$_cacheInstances[$key])) {
63+
static::$_cacheInstances[$key] = new static($id);
64+
}
65+
return static::$_cacheInstances[$key];
66+
}
67+
4768
/**
4869
* Проверяет входит ли значение в допустимые
4970
* @param int|string $id
@@ -65,7 +86,7 @@ public static function isValid($id, array $filter = [])
6586
public static function toArray(array $filter = [])
6687
{
6788
$class = get_called_class();
68-
if (!array_key_exists($class, static::$_cache)) {
89+
if (!array_key_exists($class, static::$_cacheItems)) {
6990
$reflection = new \ReflectionClass($class);
7091
if (is_callable([$class, 'items'])) {
7192
/** @noinspection PhpUndefinedMethodInspection */
@@ -82,9 +103,9 @@ public static function toArray(array $filter = [])
82103
}
83104
$items[$constant]['id'] = $constant;
84105
}
85-
static::$_cache[$class] = $items;
106+
static::$_cacheItems[$class] = $items;
86107
}
87-
$items = array_filter(static::$_cache[$class], function ($item) use ($filter) {
108+
$items = array_filter(static::$_cacheItems[$class], function ($item) use ($filter) {
88109
foreach ($filter as $key => $filterItem) {
89110
if (is_int($key)) {
90111
list($operator, $key, $value) = $filterItem;
@@ -181,7 +202,7 @@ public static function toObjects(array $filter = [])
181202
{
182203
$objects = [];
183204
foreach (static::toIds($filter) as $id) {
184-
$objects[$id] = new static($id);
205+
$objects[$id] = static::get($id);
185206
}
186207
return $objects;
187208
}

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,16 @@ class Status extends Enum
8383

8484
## Создание объекта
8585

86+
Создать объект можно через создание класса или статическую функцию `get`:
87+
8688
```php
8789
$status = new Status(Status::DRAFT);
90+
$status = Status::get(Status::DRAFT);
8891
```
8992

93+
Второй вариант препочтительнее, так как он кэширует объекты и, если объект уже был инициализирован,
94+
то он будет взят из кэша, а не будет создан заново.
95+
9096
## <a name="toIds"></a>Список значений `toIds`
9197

9298
Возвращает массив значений объекта. Поддерживает [фильтрацию](#filtering).

composer.json

+1-1
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": "2.0.0",
4+
"version": "2.1.0",
55
"type": "library",
66
"keywords": [
77
"php",

0 commit comments

Comments
 (0)