Completed
Push — master ( d9d0f6...69ec8b )
by Tõnis
02:54
created

StaticModel::findByKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace dameter\abstracts;
4
5
use yii\base\Model;
6
7
abstract class StaticModel extends Model
8
{
9
10
    /**
11
     * @return array
12
     */
13
    public abstract function modelsAttributes();
14
15
    /**
16
     * @return static[]
17
     */
18
    public function models() {
19
        $models = [];
20
        foreach ($this->modelsAttributes() as $key => $attributes) {
21
            $models[$key] = new static($attributes);
22
        }
23
        return $models;
24
    }
25
26
    /**
27
     * @param string $key
28
     * @return static
29
     */
30
    public function findByKey($key) {
31
        $models = $this->modelsAttributes();
32
        if (isset($models[$key])) {
33
            return new static($models[$key]);
34
        }
35
        return null;
36
    }
37
38
}