1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\models; |
4
|
|
|
|
5
|
|
|
use hipanel\base\Model; |
6
|
|
|
use hipanel\base\ModelTrait; |
7
|
|
|
use hipanel\modules\server\models\Server; |
8
|
|
|
use hiqdev\hiart\ActiveQuery; |
9
|
|
|
use Yii; |
10
|
|
|
use yii\db\QueryInterface; |
11
|
|
|
|
12
|
|
|
class Resource extends Model |
13
|
|
|
{ |
14
|
|
|
use ModelTrait; |
15
|
|
|
|
16
|
|
|
public static function tableName(): string |
17
|
|
|
{ |
18
|
|
|
return 'use'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function rules() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
[['id', 'object_id', 'type_id'], 'integer'], |
25
|
|
|
[['last', 'total'], 'number'], |
26
|
|
|
[['type', 'aggregation'], 'string'], |
27
|
|
|
[['time_from', 'time_till', 'date'], 'datetime', 'format' => 'php:Y-m-d'], |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getServer(): ActiveQuery |
32
|
|
|
{ |
33
|
|
|
return $this->hasOne(Server::class, ['object_id' => 'id']); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* @return QueryInterface |
39
|
|
|
*/ |
40
|
|
|
public static function find(array $options = []): QueryInterface |
41
|
|
|
{ |
42
|
|
|
return new ResourceQuery(get_called_class(), [ |
43
|
|
|
'options' => $options, |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getAmount() |
48
|
|
|
{ |
49
|
|
|
if (in_array($this->type, $this->getBandwidthTypes(), true)) { |
|
|
|
|
50
|
|
|
return $this->last; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
if (in_array($this->type, $this->getTrafficTypes(), true)) { |
|
|
|
|
53
|
|
|
return $this->total; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->total; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getDisplayAmount() |
60
|
|
|
{ |
61
|
|
|
if (in_array($this->type, $this->getBandwidthTypes(), true)) { |
|
|
|
|
62
|
|
|
return round($this->getAmount() / (10 ** 6), 2); |
63
|
|
|
} |
64
|
|
|
if (in_array($this->type, $this->getTrafficTypes(), true)) { |
|
|
|
|
65
|
|
|
return round($this->getAmount() / (10 ** 9), 2); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->getAmount(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getDisplayDate(): string |
72
|
|
|
{ |
73
|
|
|
if ($this->aggregation === 'month') { |
|
|
|
|
74
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'LLL y'); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
if ($this->aggregation === 'week') { |
|
|
|
|
77
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'dd LLL y'); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
if ($this->aggregation === 'day') { |
|
|
|
|
80
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'dd LLL y'); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date)); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getTrafficTypes(): array |
87
|
|
|
{ |
88
|
|
|
return ['server_traf_in', 'server_traf_max', 'server_traf']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getBandwidthTypes(): array |
92
|
|
|
{ |
93
|
|
|
return ['server_traf95_in', 'server_traf95_max', 'server_traf95']; |
94
|
|
|
} |
95
|
|
|
} |
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.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.