Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 845ea40
Showing
14 changed files
with
639 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Ignore all test and documentation for archive | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.coveralls.yml export-ignore | ||
/.travis.yml export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/tests export-ignore | ||
/docs export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.idea | ||
|
||
# composer | ||
/vendor | ||
/composer.lock | ||
|
||
# phpunit | ||
phpunit.phar | ||
/phpunit.xml | ||
/tests/runtime | ||
|
||
# coveralls | ||
coveralls.phar | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
language: php | ||
|
||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
# faster builds on new travis setup not using sudo | ||
sudo: false | ||
|
||
# cache vendor dirs | ||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
install: | ||
- travis_retry composer self-update && composer --version | ||
- travis_retry composer global require "fxp/composer-asset-plugin:^1.3.1" | ||
- export PATH="$HOME/.composer/vendor/bin:$PATH" | ||
- travis_retry composer install --prefer-dist --no-interaction | ||
|
||
after_script: | ||
- | | ||
if [ $TRAVIS_PHP_VERSION = '7.1' ]; then | ||
travis_retry wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar | ||
php coveralls.phar -v | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/** | ||
* @link https://github.com/tigrov/yii2-enum | ||
* @author Sergei Tigrov <rrr-r@ya.ru> | ||
*/ | ||
|
||
namespace tigrov\enum; | ||
|
||
use yii\base\Behavior; | ||
use yii\helpers\Inflector; | ||
|
||
/** | ||
* Parent class for an enum type behavior. Allows to get humanized value of the enum type based on class constants. | ||
* | ||
* @author Sergei Tigrov <rrr-r@ya.ru> | ||
*/ | ||
abstract class EnumBehavior extends Behavior | ||
{ | ||
/** | ||
* @var array list of attributes that are to be automatically humanized value. | ||
* humanized => original attribute | ||
*/ | ||
public $attributes = ['type' => 'type_key']; | ||
|
||
/** @var string|null a message category for translation the values */ | ||
public static $messageCategory = null; | ||
|
||
/** | ||
* Returns display values of the enum type | ||
* @return array [ code => value ] | ||
*/ | ||
public static function values() { | ||
static $list = []; | ||
|
||
$className = static::className(); | ||
if (!isset($list[$className])) { | ||
foreach (static::constants() as $value => $code) { | ||
$value = Inflector::humanize(strtolower($value), true); | ||
$list[$className][$code] = static::$messageCategory | ||
? \Yii::t(static::$messageCategory, $value) | ||
: $value; | ||
} | ||
} | ||
|
||
return $list[$className]; | ||
} | ||
|
||
/** | ||
* Returns a display value for $code | ||
* @param string $code value of a class constant | ||
* @return string | ||
*/ | ||
public static function value($code) | ||
{ | ||
return static::values()[$code]; | ||
} | ||
|
||
/** | ||
* Returns codes of the enum type | ||
* @return array | ||
*/ | ||
public static function codes() | ||
{ | ||
return array_keys(static::values()); | ||
} | ||
|
||
/** | ||
* Returns constants of the class | ||
* @return array constant name in key, constant value in value. | ||
*/ | ||
public static function constants() | ||
{ | ||
$class = new \ReflectionClass(static::className()); | ||
return $class->getConstants(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function canGetProperty($name, $checkVars = true) | ||
{ | ||
return isset($this->attributes[$name]) || parent::canGetProperty($name, $checkVars); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __get($name) | ||
{ | ||
if (isset($this->attributes[$name])) { | ||
if ($code = $this->owner->{$this->attributes[$name]}) { | ||
if (is_array($code)) { | ||
return array_intersect_key(static::values(), array_flip($code)); | ||
} | ||
|
||
return static::value($code); | ||
} | ||
} | ||
|
||
return parent::__get($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Tigrov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
yii2-enum | ||
============== | ||
|
||
Enum type behavior for Yii2 based on class constants. | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/Tigrov/yii2-enum/v/stable)](https://packagist.org/packages/Tigrov/yii2-enum) | ||
[![Build Status](https://travis-ci.org/Tigrov/yii2-enum.svg?branch=master)](https://travis-ci.org/Tigrov/yii2-enum) | ||
|
||
Installation | ||
------------ | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist tigrov/yii2-enum | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"tigrov/yii2-enum": "~1.0" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
Once the extension is installed, you can create an enum behavior as follow: | ||
|
||
```php | ||
class Status extends \tigrov\enum\EnumBehavior | ||
{ | ||
const ACTIVE = 'active'; | ||
const PENDING = 'pending'; | ||
const REJECTED = 'rejected'; | ||
const DELETED = 'deleted'; | ||
|
||
/** @var array list of attributes that are to be automatically humanized value. */ | ||
public $attributes = ['status' => 'status_key']; | ||
|
||
/** @var string|null a message category for translation the values */ | ||
public static $messageCategory = 'status'; | ||
} | ||
``` | ||
|
||
Create a table with the enum field | ||
```php | ||
\Yii::$app->getDb()->createCommand() | ||
->createTable('model', [ | ||
'id' => 'pk', | ||
'status_key' => 'string', | ||
])->execute(); | ||
``` | ||
|
||
Create a model for the table | ||
```php | ||
class Model extends \yii\db\ActiveRecord | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function behaviors() | ||
{ | ||
return [ | ||
Status::className(), | ||
// 'status' => [ | ||
// 'class' => Status::className(), | ||
// 'attributes' => ['status' => 'status_key'], | ||
// ], | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
[['status_key'], 'in', 'range' => Status::codes()], | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
and then use them in your code | ||
```php | ||
/** | ||
* @var ActiveRecord $model | ||
*/ | ||
$model = new Model; | ||
$model->status_key = Status::PENDING; | ||
|
||
// The field 'status' has humanize and translated value, see \yii\helpers\Inflector::humanize($word, true) | ||
$model->status; // is 'Pending' or translated value | ||
|
||
// To get all enum values | ||
Status::values(); | ||
|
||
// To get a display value | ||
Status::value(Status::PENDING); // is 'Pending' or translated value | ||
``` | ||
|
||
Examples | ||
-------- | ||
|
||
Gender codes: | ||
```php | ||
class GenderCode extends \tigrov\enum\EnumBehavior | ||
{ | ||
const MALE = 'M'; | ||
const FEMALE = 'F'; | ||
|
||
/** | ||
* @var array list of attributes that are to be automatically humanized value. | ||
* humanized => original attribute | ||
*/ | ||
public $attributes = ['gender' => 'gender_code']; | ||
|
||
/** @var string|null a message category for translation the values */ | ||
public static $messageCategory = 'gender'; | ||
} | ||
|
||
class Model extends \yii\db\ActiveRecord | ||
{ | ||
public function behaviors() | ||
{ | ||
return [ | ||
GenderCode::className(), | ||
]; | ||
} | ||
} | ||
|
||
$model->gender_code = 'M'; | ||
|
||
// The field 'gender' has humanize and translated value | ||
$model->gender; // is 'Male' or translated value | ||
``` | ||
|
||
Messenger names: | ||
```php | ||
class MessengerType extends \tigrov\enum\EnumBehavior | ||
{ | ||
const SKYPE = 'skype'; | ||
const WHATSAPP = 'whatsapp'; | ||
const VIBER = 'viber'; | ||
const FACEBOOK = 'facebook'; | ||
const IMESSAGE = 'imessage'; | ||
const TELEGRAM = 'telegram'; | ||
const LINE = 'line'; | ||
const JABBER = 'jabber'; | ||
const QQ = 'qq'; | ||
const BLACKBERRY = 'blackberry'; | ||
const AIM = 'aim'; | ||
const EBUDDY = 'ebuddy'; | ||
const YAHOO = 'yahoo'; | ||
const OTHER = 'other'; | ||
|
||
/** | ||
* Values of Messengers | ||
* @return array | ||
*/ | ||
public static function values() | ||
{ | ||
$values = parent::values(); | ||
|
||
// Correct some values | ||
$values['whatsapp'] = 'WhatsApp'; | ||
$values['imessage'] = 'iMessage'; | ||
$values['qq'] = 'QQ'; | ||
$values['blackberry'] = 'BlackBerry'; | ||
$values['aim'] = 'AIM'; | ||
$values['ebuddy'] = 'eBuddy'; | ||
$values['other'] = \Yii::t('enum', 'Other'), | ||
|
||
return $values; | ||
} | ||
} | ||
|
||
$model->type_key = 'whatsapp'; | ||
$model->type; // is 'WhatsApp' | ||
``` | ||
|
||
License | ||
------- | ||
|
||
[MIT](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "tigrov/yii2-enum", | ||
"description": "Enum type behavior for Yii2 based on class constants", | ||
"keywords": ["enum"], | ||
"type": "library", | ||
"license": "MIT", | ||
"support": { | ||
"source": "https://github.com/tigrov/yii2-enum" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Sergei Tigrov", | ||
"email": "rrr-r@ya.ru" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "~2.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"tigrov\\enum\\": "" | ||
} | ||
} | ||
} |
Oops, something went wrong.