Completed
Push — master ( d5b4df...8b174f )
by Dmitry
03:04
created

AbstractModel::getRelatedAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace hiqdev\billing\hiapi\models;
4
5
use yii\base\InvalidConfigException;
6
7
abstract class AbstractModel implements ModelInterface
8
{
9
    public function hasAttribute($name): bool
10
    {
11
        return isset($this->attributes()[$name]);
12
    }
13
14
    public function hasRelation($name): bool
15
    {
16
        return isset($this->relations()[$name]);
17
    }
18
19
    /**
20
     * @param $name
21
     * @return string
22
     * @throws InvalidConfigException
23
     */
24
    public function getRelation($name)
25
    {
26
        if (!$this->hasRelation($name)) {
27
            throw new InvalidConfigException('Model ' . static::class . ' does not have relation ' . $name);
28
        }
29
30
        return $this->relations()[$name];
31
    }
32
33
    public function getAttribute($name)
34
    {
35
        if (!$this->hasAttribute($name)) {
36
            throw new InvalidConfigException('Attribute ' . $name . ' is not available within ' . static::class);
37
        }
38
39
        $className = $this->attributes()[$name];
40
        return new $className;
41
    }
42
}
43