Completed
Push — master ( 26969a...4031dc )
by Andrii
04:12
created

Model   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 107
ccs 0
cts 82
cp 0
rs 10
wmc 14
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 4 1
B defaultAttributeLabels() 0 37 1
C mergeAttributeLabels() 0 25 8
A getClient() 0 4 1
A getClientId() 0 4 1
A getSeller() 0 4 1
A getSellerId() 0 4 1
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\base;
13
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 = 'app';
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('app', 'ID'),
38
            'client_id'     => Yii::t('app', 'Client'),
39
            'seller'        => Yii::t('app', 'Reseller'),
40
            'seller_id'     => Yii::t('app', 'Reseller'),
41
            'domain_id'     => Yii::t('app', 'Domain ID'),
42
            'domain'        => Yii::t('app', 'Domain Name'),
43
            'hdomain_id'    => Yii::t('app', 'Domain ID'),
44
            'hdomain'       => Yii::t('app', 'Domain Name'),
45
            'server_id'     => Yii::t('app', 'Server ID'),
46
            'account_id'    => Yii::t('app', 'Account ID'),
47
            'service_id'    => Yii::t('app', 'Service ID'),
48
            'tariff_id'     => Yii::t('app', 'Tariff ID'),
49
            'contact_id'    => Yii::t('app', 'Contact ID'),
50
            'article_id'    => Yii::t('app', 'Article ID'),
51
            'host_id'       => Yii::t('app', 'Host ID'),
52
            'bill_id'       => Yii::t('app', 'Bill ID'),
53
            'backup_id'     => Yii::t('app', 'Backup ID'),
54
            'backuping_id'  => Yii::t('app', 'Backuping ID'),
55
            'crontab_id'    => Yii::t('app', 'Crontab ID'),
56
            'ip_id'         => Yii::t('app', 'IP ID'),
57
            'ip'            => Yii::t('app', 'IP'),
58
            'mail_id'       => Yii::t('app', 'Mail ID'),
59
            'request_id'    => Yii::t('app', 'Request ID'),
60
            'db_id'         => Yii::t('app', 'DataBase ID'),
61
            'db'            => Yii::t('app', 'DataBase'),
62
            'state_id'      => Yii::t('app', 'Status ID'),
63
            'state_label'   => Yii::t('app', 'Status label'),
64
            'state'         => Yii::t('app', 'Status'),
65
            'type'          => Yii::t('app', 'Type'),
66
            'type_id'       => Yii::t('app', 'Type'),
67
            'type_label'    => Yii::t('app', 'Type'),
68
            'descr'         => Yii::t('app', 'Description'),
69
       ];
70
    }
71
72
    protected static $mergedLabels = [];
73
74
    /**
75
     * Merge Attribute labels for Model.
76
     */
77
    public function mergeAttributeLabels($labels)
78
    {
79
        if (!isset(static::$mergedLabels[static::class])) {
80
            $default = $this->defaultAttributeLabels();
81
            foreach ($this->attributes() as $k) {
82
                $label = $labels[$k] ?: $default[$k];
83
                if (!$label) {
84
                    if (preg_match('/(.+)_[a-z]+$/', $k, $m)) {
85
                        if (isset($labels[$m[1]])) {
86
                            $label = $labels[$m[1]];
87
                        }
88
                    }
89
                }
90
                if (!$label) {
91
                    $toTranslate = preg_replace(['/_id$/', '/_label$/', '/_ids$/', '/_like$/'], [' ID', '', ' IDs', ''], $k);
92
                    $toTranslate = preg_replace('/_/', ' ', $toTranslate);
93
                    $label = Yii::t(static::$i18nDictionary, ucfirst($toTranslate));
94
                }
95
                $labels[$k] = $label;
96
            }
97
            static::$mergedLabels[static::class] = $labels;
98
        }
99
100
        return static::$mergedLabels[static::class];
101
    }
102
103
    public function getClient()
104
    {
105
        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...
106
    }
107
108
    public function getClientId()
109
    {
110
        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...
111
    }
112
113
    public function getSeller()
114
    {
115
        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...
116
    }
117
118
    public function getSellerId()
119
    {
120
        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...
121
    }
122
}
123