Skip to content

Commit

Permalink
Свойство value переименовано в id.
Browse files Browse the repository at this point in the history
Метод toValues переименован в toIds.
  • Loading branch information
vjik committed Sep 4, 2017
1 parent fbf303b commit 2b4ce22
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 76 deletions.
34 changes: 17 additions & 17 deletions Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Abstract Enum class
*
* @property mixed $value
* @property mixed $id
* @property mixed $name
*/
abstract class Enum
Expand All @@ -21,7 +21,7 @@ abstract class Enum
/**
* @var mixed
*/
protected $value;
protected $id;


/**
Expand All @@ -31,16 +31,16 @@ abstract class Enum


/**
* @param mixed $value
* @param mixed $id
*
* @throws \UnexpectedValueException
*/
public function __construct($value)
public function __construct($id)
{
if (!static::isValid($value)) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
if (!static::isValid($id)) {
throw new \UnexpectedValueException("Value '$id' is not part of the enum " . get_called_class());
}
foreach (static::toArray()[$value] as $k => $v) {
foreach (static::toArray()[$id] as $k => $v) {
$this->$k = $v;
}
}
Expand All @@ -49,14 +49,14 @@ public function __construct($value)
/**
* Проверяет входит ли значение в допустимые
*
* @param $value
* @param $id
* @param $filter
*
* @return bool
*/
public static function isValid($value, array $filter = [])
public static function isValid($id, array $filter = [])
{
return in_array($value, static::toValues($filter), true);
return in_array($id, static::toIds($filter), true);
}


Expand Down Expand Up @@ -85,7 +85,7 @@ public static function toArray(array $filter = [])
if (!isset($items[$constant]['name'])) {
$items[$constant]['name'] = $constant;
}
$items[$constant]['value'] = $constant;
$items[$constant]['id'] = $constant;
}
static::$_cache[$class] = $items;
}
Expand Down Expand Up @@ -154,13 +154,13 @@ public static function toArray(array $filter = [])
*
* @return array
*/
public static function toValues(array $filter = [])
public static function toIds(array $filter = [])
{
$values = [];
$ids = [];
foreach (static::toArray($filter) as $item) {
$values[] = $item['value'];
$ids[] = $item['id'];
}
return $values;
return $ids;
}


Expand Down Expand Up @@ -191,7 +191,7 @@ public static function toList(array $filter = [])
public static function toObjects(array $filter = [])
{
$objects = [];
foreach (static::toValues($filter) as $id) {
foreach (static::toIds($filter) as $id) {
$objects[$id] = new static($id);
}
return $objects;
Expand Down Expand Up @@ -222,6 +222,6 @@ public function __get($name)
*/
public function __toString()
{
return (string)$this->value;
return (string)$this->id;
}
}
20 changes: 10 additions & 10 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), [`toObjects`](#toObjects), [`isValid`](#isValid)).
- Вспомогательные функции ([`toIds`](#toIds), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid)).

## Установка

Expand Down Expand Up @@ -87,18 +87,18 @@ class Status extends Enum
$status = new Status(Status::DRAFT);
```

## <a name="toValues"></a>Список значений `toValues`
## <a name="toIds"></a>Список значений `toIds`

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

```php
Status::toValues(); // ['draft', 'publish']
Status::toValues(['priority' => 20]); // ['publish']
Status::toIds(); // ['draft', 'publish']
Status::toIds(['priority' => 20]); // ['publish']
```

## <a name="toList"></a>Список с названиями `toList`

Возвращает массив вида `$value => $name`. Поддерживает [фильтрацию](#filtering).
Возвращает массив вида `$id => $name`. Поддерживает [фильтрацию](#filtering).

```php
Status::toList(); // ['draft' => 'Черновик', 'publish' => 'Опубликован']
Expand All @@ -111,9 +111,9 @@ Status::toList(['priority' => 20]); // ['publish' => 'Опубликован']

```php
[
$value => [
$id => [
'id' => $id,
'name' => $name,
'value' => $value,
'param1' => $param1,
'param2' => $param2,
Expand All @@ -135,7 +135,7 @@ Status::toArray(['priority' => 20]); // ['publish' => 'Опубликован']

```php
[
$value => Enum,
$id => Enum,
]
```
Expand All @@ -152,7 +152,7 @@ Status::isValid('publish', [['<', 'priority', 5]]); // false

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

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

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

Expand All @@ -173,7 +173,7 @@ Status::isValid('publish', [['<', 'priority', 5]]); // false
```php
[
Status::isValid('publish', [['in', 'priority', [5, 10]]]);
Status::isValid('closed', [['in', 'value', ['publish', 'closed', 'draft']]]);
Status::isValid('closed', [['in', 'id', ['publish', 'closed', 'draft']]]);
]
```

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.2.0",
"version": "2.0.0",
"type": "library",
"keywords": [
"php",
Expand Down
8 changes: 4 additions & 4 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ protected function setUp()
$this->withData = new WithData(WithData::ONE);
}

public function testGetValue()
public function testGetId()
{
$this->assertEquals(Pure::FOO, $this->pure->value);
$this->assertEquals(Pure::FOO, $this->pure->id);
$this->assertEquals((string)Pure::FOO, $this->pure);

$this->assertEquals(WithName::FOO, $this->withName->value);
$this->assertEquals(WithName::FOO, $this->withName->id);
$this->assertEquals((string)WithName::FOO, $this->withName);

$this->assertEquals(WithData::ONE, $this->withData->value);
$this->assertEquals(WithData::ONE, $this->withData->id);
$this->assertEquals((string)WithData::ONE, $this->withData);
}

Expand Down
28 changes: 14 additions & 14 deletions tests/PureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ protected function setUp()


/**
* @dataProvider invalidValueProvider
* @dataProvider invalidIdProvider
* @expectedException UnexpectedValueException
*/
public function testCreateWithInvalidValue($value)
public function testCreateWithInvalidId($id)
{
new Pure($value);
new Pure($id);
}


/**
* @return array
*/
public function invalidValueProvider()
public function invalidIdProvider()
{
return [
[0],
Expand All @@ -40,18 +40,18 @@ public function invalidValueProvider()


/**
* @dataProvider isValueProvider
* @dataProvider isIdProvider
*/
public function testIsValid($value, $isValid)
public function testIsValid($id, $isValid)
{
$this->assertSame(Pure::isValid($value), $isValid);
$this->assertSame(Pure::isValid($id), $isValid);
}


/**
* @return array
*/
public function isValueProvider()
public function isIdProvider()
{
return [
[0, false],
Expand All @@ -70,19 +70,19 @@ public function testToArray()
$this->assertSame([
Pure::FOO => [
'name' => Pure::FOO,
'value' => Pure::FOO,
'id' => Pure::FOO,
],
Pure::BAR => [
'name' => Pure::BAR,
'value' => Pure::BAR,
'id' => Pure::BAR,
],
Pure::ONE => [
'name' => Pure::ONE,
'value' => Pure::ONE,
'id' => Pure::ONE,
],
Pure::TWO => [
'name' => Pure::TWO,
'value' => Pure::TWO,
'id' => Pure::TWO,
],
], Pure::toArray());
}
Expand All @@ -99,14 +99,14 @@ public function testToList()
}


public function testToValues()
public function testToIds()
{
$this->assertSame([
Pure::FOO,
Pure::BAR,
Pure::ONE,
Pure::TWO,
], Pure::toValues());
], Pure::toIds());
}


Expand Down
Loading

0 comments on commit 2b4ce22

Please sign in to comment.