Skip to content

Commit

Permalink
update readme, add __callStatic func
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Jan 9, 2017
1 parent 72f3797 commit e3562bf
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ to the require section of your `composer.json` file.
## Declaration

```php
<?php

namespace app\models\enums;

use yii2mod\enum\helpers\BaseEnum;

class PostStatus extends BaseEnum
Expand Down Expand Up @@ -66,7 +70,16 @@ class PostStatus extends BaseEnum
];
}
```
## Usage
## Enum creation
```php
$status = new PostStatus(PostStatus::PENDING);

// or you can use the magic methods

$status = PostStatus::PENDING();
```

## Static methods
```php
PostStatus::getConstantsByValue() // ['PENDING', 'APPROVED', 'REJECTED', 'POSTPONED']
PostStatus::getConstantsByName() // ['PENDING' => 0, 'APPROVED' => 1, 'REJECTED' => 2, 'POSTPONED' => 3]
Expand All @@ -78,3 +91,35 @@ PostStatus::listData() // ['Pending', 'Approved', 'Rejected', 'Postponed']
PostStatus::getLabel(1) // Approved
PostStatus::getValueByName('Approved') // 1
```
## Type-Hint and Validation Rules
```php
<?php

use models\enums\PostStatus;
use yii\db\ActiveRecord;

class CommentModel extends ActiveRecord
{
public function rules()
{
return [
['status', 'default', 'value' => PostStatus::APPROVED],
['status', 'in', 'range' => PostStatus::getConstantsByName()],
];
}

public function setStatus(PostStatus $status)
{
$this->status = $status;
}

public function getStatus()
{
if (!$this->status) {
$this->status = PostStatus::PENDING();
}
return $this->status;
}
}
```

30 changes: 30 additions & 0 deletions helpers/BaseEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace yii2mod\enum\helpers;

use BadMethodCallException;
use ReflectionClass;
use UnexpectedValueException;
use Yii;
Expand Down Expand Up @@ -265,4 +266,33 @@ public static function isValidValue($value)

return array_key_exists($value, $constants);
}

/**
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
*
* @param string $name
* @param array $arguments
*
* @return static
*
* @throws BadMethodCallException
*/
public static function __callStatic($name, $arguments)
{
$constants = static::getConstantsByName();

if (isset($constants[$name])) {
return new static($constants[$name]);
}

throw new BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
}

/**
* @return string
*/
public function __toString()
{
return (string)$this->_value;
}
}
8 changes: 8 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public function testCreateByValue()
$this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
}

public function testCreateByStaticFunction()
{
$enum = PostStatus::APPROVED();

$this->assertEquals(PostStatus::APPROVED, $enum->getValue());
$this->assertTrue(array_key_exists($enum->getName(), PostStatus::getConstantsByName()));
}

/**
* @expectedException \UnexpectedValueException
*/
Expand Down

0 comments on commit e3562bf

Please sign in to comment.