Completed
Push — master ( 69ca06...8af5d6 )
by Nate
03:36
created

ActiveRecord::__get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\records;
10
11
use flipbox\ember\traits\AuditRules;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.0.0
16
 */
17
abstract class ActiveRecord extends \craft\db\ActiveRecord
18
{
19
    use AuditRules;
20
21
    /**
22
     * These attributes will have their 'getter' methods take priority over the normal attribute lookup.  It's
23
     * VERY important to note, that the value returned from the getter should NEVER be different than the raw
24
     * attribute value set.  If, for whatever reason, the getter determines the attribute value is
25
     * incorrect, it should set the new value prior to returning it.
26
     *
27
     * These getters are commonly used to ensure an associated model and their identifier are in sync.  For example,
28
     * a userId attribute and a user object (with an id attribute).  An operation may have saved and set an Id on the
29
     * model, but the userId attribute remains null.  The getter method may check (and set) the value prior to
30
     * returning it.
31
     *
32
     * @var array
33
     */
34
    protected $getterPriorityAttributes = [];
35
36
    /**
37
     * The table alias
38
     */
39
    const TABLE_ALIAS = '';
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public static function tableAlias()
45
    {
46
        return static::TABLE_ALIAS;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public static function tableName()
53
    {
54
        return '{{%' . static::tableAlias() . '}}';
55
    }
56
57
    /*******************************************
58
     * RULES
59
     *******************************************/
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function rules()
65
    {
66
        return array_merge(
67
            parent::rules(),
68
            $this->auditRules()
69
        );
70
    }
71
72
    /*******************************************
73
     * MAGIC
74
     *******************************************/
75
76
    /**
77
     * @param string $name
78
     * @return mixed
79
     */
80
    public function __get($name)
81
    {
82
        $getter = 'get' . $name;
83
        if (in_array($name, $this->getterPriorityAttributes, true) &&
84
            method_exists($this, $getter)
85
        ) {
86
            return $this->$getter();
87
        }
88
89
        return parent::__get($name);
90
    }
91
}
92