Skip to content

Commit e3562bf

Browse files
author
Igor Chepurnoy
committed
update readme, add __callStatic func
1 parent 72f3797 commit e3562bf

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ to the require section of your `composer.json` file.
3939
## Declaration
4040

4141
```php
42+
<?php
43+
44+
namespace app\models\enums;
45+
4246
use yii2mod\enum\helpers\BaseEnum;
4347

4448
class PostStatus extends BaseEnum
@@ -66,7 +70,16 @@ class PostStatus extends BaseEnum
6670
];
6771
}
6872
```
69-
## Usage
73+
## Enum creation
74+
```php
75+
$status = new PostStatus(PostStatus::PENDING);
76+
77+
// or you can use the magic methods
78+
79+
$status = PostStatus::PENDING();
80+
```
81+
82+
## Static methods
7083
```php
7184
PostStatus::getConstantsByValue() // ['PENDING', 'APPROVED', 'REJECTED', 'POSTPONED']
7285
PostStatus::getConstantsByName() // ['PENDING' => 0, 'APPROVED' => 1, 'REJECTED' => 2, 'POSTPONED' => 3]
@@ -78,3 +91,35 @@ PostStatus::listData() // ['Pending', 'Approved', 'Rejected', 'Postponed']
7891
PostStatus::getLabel(1) // Approved
7992
PostStatus::getValueByName('Approved') // 1
8093
```
94+
## Type-Hint and Validation Rules
95+
```php
96+
<?php
97+
98+
use models\enums\PostStatus;
99+
use yii\db\ActiveRecord;
100+
101+
class CommentModel extends ActiveRecord
102+
{
103+
public function rules()
104+
{
105+
return [
106+
['status', 'default', 'value' => PostStatus::APPROVED],
107+
['status', 'in', 'range' => PostStatus::getConstantsByName()],
108+
];
109+
}
110+
111+
public function setStatus(PostStatus $status)
112+
{
113+
$this->status = $status;
114+
}
115+
116+
public function getStatus()
117+
{
118+
if (!$this->status) {
119+
$this->status = PostStatus::PENDING();
120+
}
121+
return $this->status;
122+
}
123+
}
124+
```
125+

helpers/BaseEnum.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace yii2mod\enum\helpers;
44

5+
use BadMethodCallException;
56
use ReflectionClass;
67
use UnexpectedValueException;
78
use Yii;
@@ -265,4 +266,33 @@ public static function isValidValue($value)
265266

266267
return array_key_exists($value, $constants);
267268
}
269+
270+
/**
271+
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
272+
*
273+
* @param string $name
274+
* @param array $arguments
275+
*
276+
* @return static
277+
*
278+
* @throws BadMethodCallException
279+
*/
280+
public static function __callStatic($name, $arguments)
281+
{
282+
$constants = static::getConstantsByName();
283+
284+
if (isset($constants[$name])) {
285+
return new static($constants[$name]);
286+
}
287+
288+
throw new BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
289+
}
290+
291+
/**
292+
* @return string
293+
*/
294+
public function __toString()
295+
{
296+
return (string)$this->_value;
297+
}
268298
}

tests/EnumTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public function testCreateByValue()
5252
$this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
5353
}
5454

55+
public function testCreateByStaticFunction()
56+
{
57+
$enum = PostStatus::APPROVED();
58+
59+
$this->assertEquals(PostStatus::APPROVED, $enum->getValue());
60+
$this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
61+
}
62+
5563
/**
5664
* @expectedException \UnexpectedValueException
5765
*/

0 commit comments

Comments
 (0)