Model::mergeAttributeLabels()   B
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 0
cts 23
cp 0
rs 8.4444
cc 8
nc 2
nop 1
crap 72
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\base;
12
13
use hipanel\models\Blocking;
14
use Yii;
15
16
class Model extends \hiqdev\hiart\ActiveRecord
17
{
18
    /**
19
     * @var string the i18n dictionary name that will be used to translate labels of Model
20
     */
21
    public static $i18nDictionary = 'hipanel';
22
23
    /**
24
     * No rules be default.
25
     */
26
    public function rules()
27
    {
28
        return [];
29
    }
30
31
    /**
32
     * return default labels for attribute.
33
     */
34
    public function defaultAttributeLabels()
35
    {
36
        return [
37
            'id' => Yii::t('hipanel', 'ID'),
38
            'remoteid' => Yii::t('hipanel', 'Remote ID'),
39
            'client' => Yii::t('hipanel', 'Client'),
40
            'client_id' => Yii::t('hipanel', 'Client'),
41
            'seller' => Yii::t('hipanel', 'Reseller'),
42
            'seller_id' => Yii::t('hipanel', 'Reseller'),
43
            'domain' => Yii::t('hipanel', 'Domain name'),
44
            'hdomain' => Yii::t('hipanel', 'Domain name'),
45
            'ns' => Yii::t('hipanel', 'Name Server'),
46
            'nss' => Yii::t('hipanel', 'Name Servers'),
47
            'server' => Yii::t('hipanel', 'Server'),
48
            'account' => Yii::t('hipanel', 'Account'),
49
            'service' => Yii::t('hipanel', 'Service'),
50
            'tariff' => Yii::t('hipanel', 'Tariff'),
51
            'contact' => Yii::t('hipanel', 'Contact'),
52
            'article' => Yii::t('hipanel', 'Article'),
53
            'host' => Yii::t('hipanel', 'Host'),
54
            'bill' => Yii::t('hipanel', 'Bill'),
55
            'backup' => Yii::t('hipanel', 'Backup'),
56
            'backuping' => Yii::t('hipanel', 'Backuping'),
57
            'crontab' => Yii::t('hipanel', 'Crontab'),
58
            'ip' => Yii::t('hipanel', 'IP'),
59
            'ips' => Yii::t('hipanel', 'IPs'),
60
            'mail' => Yii::t('hipanel', 'Mail'),
61
            'request' => Yii::t('hipanel', 'Request'),
62
            'db' => Yii::t('hipanel', 'Database'),
63
            'state' => Yii::t('hipanel', 'Status'),
64
            'status' => Yii::t('hipanel', 'Status'),
65
            'states' => Yii::t('hipanel', 'Statuses'),
66
            'type' => Yii::t('hipanel', 'Type'),
67
            'types' => Yii::t('hipanel', 'Types'),
68
            'note' => Yii::t('hipanel', 'Note'),
69
            'descr' => Yii::t('hipanel', 'Description'),
70
            'comment' => Yii::t('hipanel', 'Comment'),
71
            'created_date' => Yii::t('hipanel', 'Registered'),
72
            'updated_date' => Yii::t('hipanel', 'Last update'),
73
        ];
74
    }
75
76
    public function attributeLabels()
77
    {
78
        return $this->defaultAttributeLabels();
79
    }
80
81
    protected static $mergedLabels = [];
82
83
    /**
84
     * Merge Attribute labels for Model.
85
     */
86
    public function mergeAttributeLabels($labels)
87
    {
88
        if (!isset(static::$mergedLabels[static::class])) {
89
            $default = $this->defaultAttributeLabels();
90
            foreach ($this->attributes() as $k) {
91
                $label = $labels[$k] ?: $default[$k];
92
                if (!$label) {
93
                    if (preg_match('/(.+)_[a-z]+$/', $k, $m)) {
94
                        if (isset($labels[$m[1]])) {
95
                            $label = $labels[$m[1]];
96
                        }
97
                    }
98
                }
99
                if (!$label) {
100
                    $toTranslate = preg_replace(['/_id$/', '/_label$/', '/_ids$/', '/_like$/'], [' ID', '', ' IDs', ''], $k);
101
                    $toTranslate = preg_replace('/_/', ' ', $toTranslate);
102
                    $label = Yii::t(static::$i18nDictionary, ucfirst($toTranslate));
103
                }
104
                $labels[$k] = $label;
105
            }
106
            static::$mergedLabels[static::class] = $labels;
107
        }
108
109
        return static::$mergedLabels[static::class];
110
    }
111
112
    public function getClient()
113
    {
114
        return $this->client;
0 ignored issues
show
Documentation introduced by
The property client does not exist on object<hipanel\base\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
    }
116
117
    public function getClientId()
118
    {
119
        return $this->client_id;
0 ignored issues
show
Documentation introduced by
The property client_id does not exist on object<hipanel\base\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
    }
121
122
    public function getSeller()
123
    {
124
        return $this->seller;
0 ignored issues
show
Documentation introduced by
The property seller does not exist on object<hipanel\base\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
125
    }
126
127
    public function getSellerId()
128
    {
129
        return $this->seller_id;
0 ignored issues
show
Documentation introduced by
The property seller_id does not exist on object<hipanel\base\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
130
    }
131
132
    public function getBlocking()
133
    {
134
        return $this->hasOne(Blocking::class, ['id' => 'id']);
135
    }
136
}
137