1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\models\decorators\server; |
12
|
|
|
|
13
|
|
|
use hipanel\inputs\OptionsInput; |
14
|
|
|
use hipanel\modules\finance\models\Tariff; |
15
|
|
|
use Yii; |
16
|
|
|
|
17
|
|
|
class LocationResourceDecorator extends AbstractServerResourceDecorator |
18
|
|
|
{ |
19
|
|
|
public function displayTitle() |
20
|
|
|
{ |
21
|
|
|
return Yii::t('hipanel:finance:tariff', 'Location'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function displayValue() |
25
|
|
|
{ |
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function displayUnit() |
30
|
|
|
{ |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function displayPrepaidAmount() |
35
|
|
|
{ |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getPrepaidQuantity() |
40
|
|
|
{ |
41
|
|
|
return 1; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getOverusePrice() |
45
|
|
|
{ |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function displayOverusePrice() |
50
|
|
|
{ |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function prepaidAmountType() |
55
|
|
|
{ |
56
|
|
|
return new OptionsInput($this->amountOptions()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function displayShortenLocations() |
60
|
|
|
{ |
61
|
|
|
$data = $this->amountOptions(); |
62
|
|
|
$result = []; |
63
|
|
|
foreach ($data as $item) { |
64
|
|
|
$result[] = substr($item, 0, strpos($item, ',')); |
65
|
|
|
} |
66
|
|
|
$result = array_unique($result, SORT_STRING); |
67
|
|
|
|
68
|
|
|
return rtrim(implode(', ', $result), ', '); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function amountOptions() |
72
|
|
|
{ |
73
|
|
|
$data = Yii::$app->cache->getOrSet([__METHOD__, 'serversGetLocations'], function () { |
74
|
|
|
return \hipanel\modules\server\models\Server::Perform('getLocations',[ |
75
|
|
|
Tariff::TYPE_XEN => ['type' => Tariff::TYPE_XEN], |
76
|
|
|
Tariff::TYPE_OPENVZ => ['type' => Tariff::TYPE_OPENVZ], |
77
|
|
|
], ['batch' => true]); |
78
|
|
|
}, 3600); |
79
|
|
|
|
80
|
|
|
return $data[$this->resource->tariff->type]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.