diff --git a/Enum.php b/Enum.php index 26aba84..783250a 100644 --- a/Enum.php +++ b/Enum.php @@ -5,7 +5,7 @@ /** * Abstract Enum class * - * @property mixed $value + * @property mixed $id * @property mixed $name */ abstract class Enum @@ -21,7 +21,7 @@ abstract class Enum /** * @var mixed */ - protected $value; + protected $id; /** @@ -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; } } @@ -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); } @@ -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; } @@ -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; } @@ -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; @@ -222,6 +222,6 @@ public function __get($name) */ public function __toString() { - return (string)$this->value; + return (string)$this->id; } } diff --git a/README.md b/README.md index 7cf52c2..7e94df2 100644 --- a/README.md +++ b/README.md @@ -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)). ## Установка @@ -87,18 +87,18 @@ class Status extends Enum $status = new Status(Status::DRAFT); ``` -## Список значений `toValues` +## Список значений `toIds` Возвращает массив значений объекта. Поддерживает [фильтрацию](#filtering). ```php -Status::toValues(); // ['draft', 'publish'] -Status::toValues(['priority' => 20]); // ['publish'] +Status::toIds(); // ['draft', 'publish'] +Status::toIds(['priority' => 20]); // ['publish'] ``` ## Список с названиями `toList` -Возвращает массив вида `$value => $name`. Поддерживает [фильтрацию](#filtering). +Возвращает массив вида `$id => $name`. Поддерживает [фильтрацию](#filtering). ```php Status::toList(); // ['draft' => 'Черновик', 'publish' => 'Опубликован'] @@ -111,9 +111,9 @@ Status::toList(['priority' => 20]); // ['publish' => 'Опубликован'] ```php [ - $value => [ + $id => [ + 'id' => $id, 'name' => $name, - 'value' => $value, 'param1' => $param1, 'param2' => $param2, … @@ -135,7 +135,7 @@ Status::toArray(['priority' => 20]); // ['publish' => 'Опубликован'] ```php [ - $value => Enum, + $id => Enum, … ] ``` @@ -152,7 +152,7 @@ Status::isValid('publish', [['<', 'priority', 5]]); // false ## Фильтрация -Методы [`toValues`](#toValues), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid) поддерживают фильтрацию. +Методы [`toIds`](#toIds), [`toList`](#toList), [`toArray`](#toArray), [`toObjects`](#toObjects), [`isValid`](#isValid) поддерживают фильтрацию. Фильтр передаётся в виде массива: @@ -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']]]); ] ``` diff --git a/composer.json b/composer.json index a075405..7a28500 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 61717ea..9c95fdf 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -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); } diff --git a/tests/PureTest.php b/tests/PureTest.php index af5aa5f..5f8eb16 100644 --- a/tests/PureTest.php +++ b/tests/PureTest.php @@ -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], @@ -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], @@ -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()); } @@ -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()); } diff --git a/tests/WithDataTest.php b/tests/WithDataTest.php index fab8de8..9151d52 100644 --- a/tests/WithDataTest.php +++ b/tests/WithDataTest.php @@ -20,19 +20,19 @@ protected function setUp() /** - * @dataProvider invalidValueProvider + * @dataProvider invalidIdProvider * @expectedException UnexpectedValueException */ - public function testCreateWithInvalidValue($value) + public function testCreateWithInvalidId($id) { - new WithData($value); + new WithData($id); } /** * @return array */ - public function invalidValueProvider() + public function invalidIdProvider() { return [ [0], @@ -43,18 +43,18 @@ public function invalidValueProvider() /** - * @dataProvider isValueProvider + * @dataProvider isIdProvider */ - public function testIsValid($value, $isValid) + public function testIsValid($id, $isValid) { - $this->assertSame(WithData::isValid($value), $isValid); + $this->assertSame(WithData::isValid($id), $isValid); } /** * @return array */ - public function isValueProvider() + public function isIdProvider() { return [ [0, false], @@ -70,22 +70,22 @@ public function testToArray() WithData::ONE => [ 'name' => 'One', 'number' => 101, - 'value' => WithData::ONE, + 'id' => WithData::ONE, ], WithData::TWO => [ 'name' => 'Two', 'number' => 102, - 'value' => WithData::TWO, + 'id' => WithData::TWO, ], WithData::THREE => [ 'name' => 'Three', 'number' => 103, - 'value' => WithData::THREE, + 'id' => WithData::THREE, ], WithData::ONE2 => [ 'name' => 'One2', 'number' => 101, - 'value' => WithData::ONE2, + 'id' => WithData::ONE2, ], ], WithData::toArray()); } @@ -102,14 +102,14 @@ public function testToList() } - public function testToValues() + public function testToIds() { $this->assertSame([ WithData::ONE, WithData::TWO, WithData::THREE, WithData::ONE2, - ], WithData::toValues()); + ], WithData::toIds()); } @@ -127,9 +127,9 @@ public function testToObjects() /** * @dataProvider filterProvider */ - public function testFilter($filter, $values) + public function testFilter($filter, $ids) { - $this->assertSame(WithData::toValues($filter), $values); + $this->assertSame(WithData::toIds($filter), $ids); } @@ -147,10 +147,10 @@ public function filterProvider() [[['<=', 'number', 102]], [1, 2, 10]], [[['>=', 'number', 102]], [2, 3]], [[['>=', 'number', 101], ['<', 'number', 103]], [1, 2, 10]], - [['number' => 101, 'value' => 1], [1]], + [['number' => 101, 'id' => 1], [1]], [['number' => 13], []], [[['in', 'number', [101, 102]]], [1, 2, 10]], - [[['in', 'value', [2, 3]]], [2, 3]], + [[['in', 'id', [2, 3]]], [2, 3]], [[['in', 'number', [1, 2]]], []], ]; } diff --git a/tests/WithNameTest.php b/tests/WithNameTest.php index eb894f3..ea74f22 100644 --- a/tests/WithNameTest.php +++ b/tests/WithNameTest.php @@ -16,19 +16,19 @@ protected function setUp() /** - * @dataProvider invalidValueProvider + * @dataProvider invalidIdProvider * @expectedException UnexpectedValueException */ - public function testCreateWithInvalidValue($value) + public function testCreateWithInvalidId($id) { - new WithName($value); + new WithName($id); } /** * @return array */ - public function invalidValueProvider() + public function invalidIdProvider() { return [ [0], @@ -40,18 +40,18 @@ public function invalidValueProvider() /** - * @dataProvider isValueProvider + * @dataProvider isIdProvider */ - public function testIsValid($value, $isValid) + public function testIsValid($id, $isValid) { - $this->assertSame(WithName::isValid($value), $isValid); + $this->assertSame(WithName::isValid($id), $isValid); } /** * @return array */ - public function isValueProvider() + public function isIdProvider() { return [ [0, false], @@ -67,11 +67,11 @@ public function testToArray() $this->assertSame([ WithName::FOO => [ 'name' => 'Foo Name', - 'value' => WithName::FOO, + 'id' => WithName::FOO, ], WithName::BAR => [ 'name' => 'Bar Name', - 'value' => WithName::BAR, + 'id' => WithName::BAR, ], ], WithName::toArray()); } @@ -86,12 +86,12 @@ public function testToList() } - public function testToValues() + public function testToIds() { $this->assertSame([ WithName::FOO, WithName::BAR, - ], WithName::toValues()); + ], WithName::toIds()); }