Passed
Branch master (861137)
by Tõnis
07:04
created

StaticModel::getModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace andmemasin\myabstract;
4
5
use yii\base\Model;
6
use andmemasin\helpers\MyArrayHelper;
7
8
class StaticModel extends Model
9
{
10
    public static $keyColumn = 'key';
11
12
    /**
13
     * @return array
14
     */
15
    public function getModelAttributes() {
16
        return [];
17
    }
18
19
    /**
20
     * @param $id
21
     * @return static
22
     */
23
    public static function getById($id) {
24
        $models = (new static)->getModelAttributes();
25
        if (isset($models[$id])) {
26
            return new static($models[$id]);
27
        }
28
        return null;
29
    }
30
    /**
31
     * @param $key
32
     * @return static
33
     */
34
    public static function getByKey($key) {
35
        $arr = MyArrayHelper::indexByColumn((new static)->getModelAttributes(), static::$keyColumn);
36
37
        if (isset($arr[$key])) {
38
            return new static($arr[$key]);
39
        }
40
        return null;
41
    }
42
43
}