Skip to content

Commit 78cacf8

Browse files
committed
Добавлена вспомогательная функция toObjects
1 parent 5162f41 commit 78cacf8

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

Enum.php

+17
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ public static function toList(array $filter = [])
174174
}
175175

176176

177+
/**
178+
* Все доступные значения в виде объектов
179+
*
180+
* @param array $filter
181+
*
182+
* @return array
183+
*/
184+
public static function toObjects(array $filter = [])
185+
{
186+
$objects = [];
187+
foreach (static::toValues($filter) as $id) {
188+
$objects[$id] = new static($id);
189+
}
190+
return $objects;
191+
}
192+
193+
177194
/**
178195
* @param $name
179196
*

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Поддержка [дополнительных данных](#extradata) для значений.
66
- Поддержка [геттеров](#getters).
77
- Поддержка [фильтрации](#filtering).
8-
- Вспомогательные функции ([`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`isValid`](#isValid)).
8+
- Вспомогательные функции ([`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid)).
99

1010
## Установка
1111

@@ -129,6 +129,17 @@ Status::toArray();
129129
Status::toArray(['priority' => 20]); // ['publish' => 'Опубликован']
130130
```
131131

132+
## <a name="toObjects"></a>Массив объектов `toObjects`
133+
134+
Возвращает массив вида:
135+
136+
```php
137+
[
138+
$value => Enum,
139+
140+
]
141+
```
142+
132143
## <a name="isValid"></a>Проверка значения `isValid`
133144

134145
Проверяет, существует ли значение в перечисляемом типе. Поддерживает [фильтрацию](#filtering).
@@ -141,7 +152,7 @@ Status::isValid('publish', [['<', 'priority', 5]]); // false
141152

142153
## <a name="filtering"></a>Фильтрация
143154

144-
Методы [`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`isValid`](#isValid) поддерживают фильтрацию.
155+
Методы [`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid) поддерживают фильтрацию.
145156

146157
Фильтр передаётся в виде массива:
147158

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

tests/PureTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,15 @@ public function testToValues()
108108
Pure::TWO,
109109
], Pure::toValues());
110110
}
111+
112+
113+
public function testToObjects()
114+
{
115+
$this->assertEquals([
116+
Pure::FOO => new Pure(Pure::FOO),
117+
Pure::BAR => new Pure(Pure::BAR),
118+
Pure::ONE => new Pure(Pure::ONE),
119+
Pure::TWO => new Pure(Pure::TWO),
120+
], Pure::toObjects());
121+
}
111122
}

tests/WithDataTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public function testToValues()
113113
}
114114

115115

116+
public function testToObjects()
117+
{
118+
$this->assertEquals([
119+
WithData::ONE => new WithData(WithData::ONE),
120+
WithData::TWO => new WithData(WithData::TWO),
121+
WithData::THREE => new WithData(WithData::THREE),
122+
WithData::ONE2 => new WithData(WithData::ONE2),
123+
], WithData::toObjects());
124+
}
125+
126+
116127
/**
117128
* @dataProvider filterProvider
118129
*/

tests/WithNameTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,13 @@ public function testToValues()
9393
WithName::BAR,
9494
], WithName::toValues());
9595
}
96+
97+
98+
public function testToObjects()
99+
{
100+
$this->assertEquals([
101+
WithName::FOO => new WithName(WithName::FOO),
102+
WithName::BAR => new WithName(WithName::BAR),
103+
], WithName::toObjects());
104+
}
96105
}

0 commit comments

Comments
 (0)