Skip to content

Commit 2b4ce22

Browse files
committed
Свойство value переименовано в id.
Метод toValues переименован в toIds.
1 parent fbf303b commit 2b4ce22

File tree

7 files changed

+76
-76
lines changed

7 files changed

+76
-76
lines changed

Enum.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Abstract Enum class
77
*
8-
* @property mixed $value
8+
* @property mixed $id
99
* @property mixed $name
1010
*/
1111
abstract class Enum
@@ -21,7 +21,7 @@ abstract class Enum
2121
/**
2222
* @var mixed
2323
*/
24-
protected $value;
24+
protected $id;
2525

2626

2727
/**
@@ -31,16 +31,16 @@ abstract class Enum
3131

3232

3333
/**
34-
* @param mixed $value
34+
* @param mixed $id
3535
*
3636
* @throws \UnexpectedValueException
3737
*/
38-
public function __construct($value)
38+
public function __construct($id)
3939
{
40-
if (!static::isValid($value)) {
41-
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
40+
if (!static::isValid($id)) {
41+
throw new \UnexpectedValueException("Value '$id' is not part of the enum " . get_called_class());
4242
}
43-
foreach (static::toArray()[$value] as $k => $v) {
43+
foreach (static::toArray()[$id] as $k => $v) {
4444
$this->$k = $v;
4545
}
4646
}
@@ -49,14 +49,14 @@ public function __construct($value)
4949
/**
5050
* Проверяет входит ли значение в допустимые
5151
*
52-
* @param $value
52+
* @param $id
5353
* @param $filter
5454
*
5555
* @return bool
5656
*/
57-
public static function isValid($value, array $filter = [])
57+
public static function isValid($id, array $filter = [])
5858
{
59-
return in_array($value, static::toValues($filter), true);
59+
return in_array($id, static::toIds($filter), true);
6060
}
6161

6262

@@ -85,7 +85,7 @@ public static function toArray(array $filter = [])
8585
if (!isset($items[$constant]['name'])) {
8686
$items[$constant]['name'] = $constant;
8787
}
88-
$items[$constant]['value'] = $constant;
88+
$items[$constant]['id'] = $constant;
8989
}
9090
static::$_cache[$class] = $items;
9191
}
@@ -154,13 +154,13 @@ public static function toArray(array $filter = [])
154154
*
155155
* @return array
156156
*/
157-
public static function toValues(array $filter = [])
157+
public static function toIds(array $filter = [])
158158
{
159-
$values = [];
159+
$ids = [];
160160
foreach (static::toArray($filter) as $item) {
161-
$values[] = $item['value'];
161+
$ids[] = $item['id'];
162162
}
163-
return $values;
163+
return $ids;
164164
}
165165

166166

@@ -191,7 +191,7 @@ public static function toList(array $filter = [])
191191
public static function toObjects(array $filter = [])
192192
{
193193
$objects = [];
194-
foreach (static::toValues($filter) as $id) {
194+
foreach (static::toIds($filter) as $id) {
195195
$objects[$id] = new static($id);
196196
}
197197
return $objects;
@@ -222,6 +222,6 @@ public function __get($name)
222222
*/
223223
public function __toString()
224224
{
225-
return (string)$this->value;
225+
return (string)$this->id;
226226
}
227227
}

README.md

Lines changed: 10 additions & 10 deletions
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), [`toObjects`](#toObjects), [`isValid`](#isValid)).
8+
- Вспомогательные функции ([`toIds`](#toIds), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid)).
99

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

@@ -87,18 +87,18 @@ class Status extends Enum
8787
$status = new Status(Status::DRAFT);
8888
```
8989

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

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

9494
```php
95-
Status::toValues(); // ['draft', 'publish']
96-
Status::toValues(['priority' => 20]); // ['publish']
95+
Status::toIds(); // ['draft', 'publish']
96+
Status::toIds(['priority' => 20]); // ['publish']
9797
```
9898

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

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

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

112112
```php
113113
[
114-
$value => [
114+
$id => [
115+
'id' => $id,
115116
'name' => $name,
116-
'value' => $value,
117117
'param1' => $param1,
118118
'param2' => $param2,
119119
@@ -135,7 +135,7 @@ Status::toArray(['priority' => 20]); // ['publish' => 'Опубликован']
135135

136136
```php
137137
[
138-
$value => Enum,
138+
$id => Enum,
139139
140140
]
141141
```
@@ -152,7 +152,7 @@ Status::isValid('publish', [['<', 'priority', 5]]); // false
152152

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

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

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

@@ -173,7 +173,7 @@ Status::isValid('publish', [['<', 'priority', 5]]); // false
173173
```php
174174
[
175175
Status::isValid('publish', [['in', 'priority', [5, 10]]]);
176-
Status::isValid('closed', [['in', 'value', ['publish', 'closed', 'draft']]]);
176+
Status::isValid('closed', [['in', 'id', ['publish', 'closed', 'draft']]]);
177177
]
178178
```
179179

composer.json

Lines changed: 1 addition & 1 deletion
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.2.0",
4+
"version": "2.0.0",
55
"type": "library",
66
"keywords": [
77
"php",

tests/BaseTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ protected function setUp()
1919
$this->withData = new WithData(WithData::ONE);
2020
}
2121

22-
public function testGetValue()
22+
public function testGetId()
2323
{
24-
$this->assertEquals(Pure::FOO, $this->pure->value);
24+
$this->assertEquals(Pure::FOO, $this->pure->id);
2525
$this->assertEquals((string)Pure::FOO, $this->pure);
2626

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

30-
$this->assertEquals(WithData::ONE, $this->withData->value);
30+
$this->assertEquals(WithData::ONE, $this->withData->id);
3131
$this->assertEquals((string)WithData::ONE, $this->withData);
3232
}
3333

tests/PureTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ protected function setUp()
1616

1717

1818
/**
19-
* @dataProvider invalidValueProvider
19+
* @dataProvider invalidIdProvider
2020
* @expectedException UnexpectedValueException
2121
*/
22-
public function testCreateWithInvalidValue($value)
22+
public function testCreateWithInvalidId($id)
2323
{
24-
new Pure($value);
24+
new Pure($id);
2525
}
2626

2727

2828
/**
2929
* @return array
3030
*/
31-
public function invalidValueProvider()
31+
public function invalidIdProvider()
3232
{
3333
return [
3434
[0],
@@ -40,18 +40,18 @@ public function invalidValueProvider()
4040

4141

4242
/**
43-
* @dataProvider isValueProvider
43+
* @dataProvider isIdProvider
4444
*/
45-
public function testIsValid($value, $isValid)
45+
public function testIsValid($id, $isValid)
4646
{
47-
$this->assertSame(Pure::isValid($value), $isValid);
47+
$this->assertSame(Pure::isValid($id), $isValid);
4848
}
4949

5050

5151
/**
5252
* @return array
5353
*/
54-
public function isValueProvider()
54+
public function isIdProvider()
5555
{
5656
return [
5757
[0, false],
@@ -70,19 +70,19 @@ public function testToArray()
7070
$this->assertSame([
7171
Pure::FOO => [
7272
'name' => Pure::FOO,
73-
'value' => Pure::FOO,
73+
'id' => Pure::FOO,
7474
],
7575
Pure::BAR => [
7676
'name' => Pure::BAR,
77-
'value' => Pure::BAR,
77+
'id' => Pure::BAR,
7878
],
7979
Pure::ONE => [
8080
'name' => Pure::ONE,
81-
'value' => Pure::ONE,
81+
'id' => Pure::ONE,
8282
],
8383
Pure::TWO => [
8484
'name' => Pure::TWO,
85-
'value' => Pure::TWO,
85+
'id' => Pure::TWO,
8686
],
8787
], Pure::toArray());
8888
}
@@ -99,14 +99,14 @@ public function testToList()
9999
}
100100

101101

102-
public function testToValues()
102+
public function testToIds()
103103
{
104104
$this->assertSame([
105105
Pure::FOO,
106106
Pure::BAR,
107107
Pure::ONE,
108108
Pure::TWO,
109-
], Pure::toValues());
109+
], Pure::toIds());
110110
}
111111

112112

0 commit comments

Comments
 (0)