Skip to content

Commit 864cbc2

Browse files
author
Mikhail Yarmoluk
committed
Init
0 parents  commit 864cbc2

File tree

5 files changed

+311
-0
lines changed

5 files changed

+311
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

Enum.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Kakadu\Yii2Enum;
4+
5+
/**
6+
* Class Enum
7+
* @package Kakadu\Yii2Enum
8+
* @author Konstantin Timoshenko
9+
* @author Yarmaliuk Mikhail
10+
* @version 2.0
11+
*
12+
* @since 2.0 rename to Enum
13+
* @since 1.0 AbstractDictionary
14+
*/
15+
abstract class Enum
16+
{
17+
/**
18+
* Default item
19+
*
20+
* @var array
21+
*/
22+
protected static $notSetMessage = ['app', 'Не указано'];
23+
24+
/**
25+
* Enum model attribute name
26+
*
27+
* @var string|NULL
28+
*/
29+
protected static $attribute = NULL;
30+
31+
/**
32+
* Get all items
33+
*
34+
* @return array
35+
*
36+
* @since 2.0 not abstract
37+
* @since 1.0 abstract function
38+
*/
39+
public static function all(): array
40+
{
41+
return [];
42+
}
43+
44+
/**
45+
* Enum constructor.
46+
*
47+
* @return void
48+
*/
49+
private function __construct()
50+
{
51+
}
52+
53+
/**
54+
* Get all items keys
55+
*
56+
* @return array
57+
*/
58+
public static function keys(): array
59+
{
60+
return array_keys(static::all());
61+
}
62+
63+
/**
64+
* Get title by vendor
65+
*
66+
* @param mixed $key
67+
*
68+
* @return string|NULL
69+
*/
70+
public static function get($key): ?string
71+
{
72+
$key = \is_object($key) ? $key->{self::$attribute} : $key;
73+
74+
return static::all()[$key] ?? static::getDefault();
75+
}
76+
77+
/**
78+
* Get default item
79+
*
80+
* @return string
81+
*/
82+
public static function getDefault(): string
83+
{
84+
[$category, $message] = static::$notSetMessage;
85+
86+
return \Yii::t($category, $message);
87+
}
88+
89+
/**
90+
* If model attribute has key
91+
*
92+
* @param mixed $model
93+
* @param string|int $key
94+
*
95+
* @return bool
96+
*/
97+
public static function has($model, $key): bool
98+
{
99+
if (is_object($model)) {
100+
if ($attribute = static::$attribute) {
101+
return $model->$attribute == $key;
102+
}
103+
} else {
104+
return $model == $key;
105+
}
106+
107+
return false;
108+
}
109+
110+
/**
111+
* If model attribute has key in range
112+
*
113+
* @param mixed $model
114+
* @param array $keys
115+
*
116+
* @return bool
117+
*/
118+
public static function hasIn($model, array $keys): bool
119+
{
120+
if (is_object($model)) {
121+
if ($attribute = static::$attribute) {
122+
return \in_array($model->$attribute, $keys);
123+
}
124+
} else {
125+
return \in_array($model, $keys);
126+
}
127+
128+
return false;
129+
}
130+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Matthew Patell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# yii2-enum
2+
Extension provide very simply use enum for models (and others) in yii2
3+
4+
Installation
5+
------------
6+
7+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
8+
9+
Either run
10+
11+
```
12+
php composer.phar require --prefer-dist matthew-p/yii2-services "*"
13+
```
14+
15+
or add
16+
17+
```
18+
"kakadu-dev/yii2-enum": "@dev"
19+
```
20+
21+
to the require section of your `composer.json` file.
22+
23+
Usage
24+
-----
25+
26+
Once the extension is installed, simply use it in your code by:
27+
28+
Create directory structure for model "User" (only example, not required):
29+
30+
```
31+
common/
32+
models/
33+
Users/
34+
Enum/
35+
UserStatus.php
36+
User.php
37+
UserQuery.php
38+
```
39+
40+
UserStatus class example:
41+
```php
42+
<?php
43+
44+
namespace common\models\Users\Enum;
45+
46+
use Yii;
47+
use Kakadu\Yii2Enum;
48+
49+
/**
50+
* Class UserStatus
51+
* @package common\models\Users\Enum
52+
* @author
53+
* @version 1.0
54+
*/
55+
class UserStatus extends Enum
56+
{
57+
const DELETED = 0;
58+
const ACTIVE = 1;
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected static $attribute = 'status';
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
public static function all(): array
69+
{
70+
return [
71+
self::DELETED => Yii::t('app', 'Deleted'),
72+
self::ACTIVE => Yii::t('app', 'Active'),
73+
];
74+
}
75+
}
76+
```
77+
78+
And use:
79+
80+
```php
81+
...
82+
83+
use common\models\Users\Enum\UserStatus;
84+
...
85+
86+
class User extends ActiveRecord implements IdentityInterface
87+
{
88+
...
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function rules()
94+
{
95+
return [
96+
['status', 'default', 'value' => UserStatus::ACTIVE],
97+
['status', 'in', 'range' => UserStatus::keys()],
98+
];
99+
}
100+
101+
...
102+
}
103+
```
104+
105+
More examples:
106+
```php
107+
$model = new User(['status' => UserStatus::ACTIVE]);
108+
109+
if (UserStatus::has($model, UserStatus::ACTIVE)) {
110+
//
111+
}
112+
113+
// DetailView widget (or GridView)
114+
DetailView::widget([
115+
'model' => $model,
116+
'attributes' => [
117+
'id',
118+
'name',
119+
[
120+
'attribute' => 'status',
121+
'format' => 'raw',
122+
'filter' => UserStatus::all(),
123+
'value' => UserStatus::get($model->status),
124+
],
125+
...
126+
],
127+
])
128+
129+
// In form
130+
$form->field($model, 'status')->dropDownList(UserStatus::all())
131+
```
132+
133+
That's all. Check it.

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "kakadu-dev/yii2-enum",
3+
"description": "Extension provide very simply use enum for models (and others) in yii2",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2","extension","enum","models","structure","logic"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Ярмолюк Михаил",
10+
"email": "lukomi@mail.ru"
11+
},
12+
{
13+
"name": "Тимошенко Константин",
14+
"email": "t.kanstantsin@gmail.com"
15+
}
16+
],
17+
"require": {
18+
"php": ">=7.1",
19+
"yiisoft/yii2": "*"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Kakadu\\Yii2Enum\\": ""
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)