Skip to content

Commit

Permalink
Добавлена вспомогательная функция toObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Aug 21, 2017
1 parent 5162f41 commit 78cacf8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ public static function toList(array $filter = [])
}


/**
* Все доступные значения в виде объектов
*
* @param array $filter
*
* @return array
*/
public static function toObjects(array $filter = [])
{
$objects = [];
foreach (static::toValues($filter) as $id) {
$objects[$id] = new static($id);
}
return $objects;
}


/**
* @param $name
*
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Поддержка [дополнительных данных](#extradata) для значений.
- Поддержка [геттеров](#getters).
- Поддержка [фильтрации](#filtering).
- Вспомогательные функции ([`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`isValid`](#isValid)).
- Вспомогательные функции ([`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid)).

## Установка

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

## <a name="toObjects"></a>Массив объектов `toObjects`

Возвращает массив вида:

```php
[
$value => Enum,
]
```

## <a name="isValid"></a>Проверка значения `isValid`

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

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

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

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

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.0.0",
"version": "1.1.0",
"type": "library",
"keywords": [
"php",
Expand Down
11 changes: 11 additions & 0 deletions tests/PureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,15 @@ public function testToValues()
Pure::TWO,
], Pure::toValues());
}


public function testToObjects()
{
$this->assertEquals([
Pure::FOO => new Pure(Pure::FOO),
Pure::BAR => new Pure(Pure::BAR),
Pure::ONE => new Pure(Pure::ONE),
Pure::TWO => new Pure(Pure::TWO),
], Pure::toObjects());
}
}
11 changes: 11 additions & 0 deletions tests/WithDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public function testToValues()
}


public function testToObjects()
{
$this->assertEquals([
WithData::ONE => new WithData(WithData::ONE),
WithData::TWO => new WithData(WithData::TWO),
WithData::THREE => new WithData(WithData::THREE),
WithData::ONE2 => new WithData(WithData::ONE2),
], WithData::toObjects());
}


/**
* @dataProvider filterProvider
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/WithNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ public function testToValues()
WithName::BAR,
], WithName::toValues());
}


public function testToObjects()
{
$this->assertEquals([
WithName::FOO => new WithName(WithName::FOO),
WithName::BAR => new WithName(WithName::BAR),
], WithName::toObjects());
}
}

0 comments on commit 78cacf8

Please sign in to comment.