1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\logic\bill; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\forms\BillForm; |
6
|
|
|
use hipanel\modules\finance\models\Bill; |
7
|
|
|
use hipanel\modules\finance\models\Charge; |
8
|
|
|
use hiqdev\php\units\Quantity; |
9
|
|
|
use hiqdev\php\units\yii2\formatters\IntlFormatter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class QuantityFormatterFactory |
13
|
|
|
*/ |
14
|
|
|
final class QuantityFormatterFactory implements QuantityFormatterFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array maps bill type to a QuantityFormatter |
18
|
|
|
* // TODO: use DI to configure |
19
|
|
|
*/ |
20
|
|
|
protected $types = [ |
21
|
|
|
'support_time' => SupportTimeQuantity::class, |
22
|
|
|
'server_traf_max' => DefaultQuantityFormatter::class, |
23
|
|
|
'server_traf95_max' => DefaultQuantityFormatter::class, |
24
|
|
|
'backup_du' => DefaultQuantityFormatter::class, |
25
|
|
|
'server_du' => DefaultQuantityFormatter::class, |
26
|
|
|
'hw_purchase' => DefaultQuantityFormatter::class, |
27
|
|
|
'ip_num' => IPNumQuantity::class, |
28
|
|
|
'monthly' => MonthlyQuantity::class, |
29
|
|
|
'drenewal' => DomainRenewalQuantity::class, |
30
|
|
|
]; |
31
|
|
|
/** |
32
|
|
|
* @var IntlFormatter |
33
|
|
|
*/ |
34
|
|
|
private $intlFormatter; |
35
|
|
|
|
36
|
|
|
public function __construct(IntlFormatter $intlFormatter) |
37
|
|
|
{ |
38
|
|
|
$this->intlFormatter = $intlFormatter; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** {@inheritdoc} */ |
42
|
|
|
public function create($model): ?QuantityFormatterInterface |
43
|
|
|
{ |
44
|
|
|
if ($model instanceof Bill || $model instanceof BillForm) { |
45
|
|
|
return $this->forBill($model); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($model instanceof Charge) { |
49
|
|
|
return $this->forCharge($model); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw new \InvalidArgumentException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Bill|BillForm $bill |
57
|
|
|
* @return QuantityFormatterInterface|null |
58
|
|
|
*/ |
59
|
|
|
public function forBill($bill): ?QuantityFormatterInterface |
60
|
|
|
{ |
61
|
|
|
return $this->createByType($bill->type, Quantity::create($bill->unit, $bill->quantity), $bill); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function forCharge(Charge $charge): ?QuantityFormatterInterface |
65
|
|
|
{ |
66
|
|
|
return $this->createByType($charge->type, Quantity::create($charge->unit, $charge->quantity), $charge); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
|
|
public function createByType(string $type, Quantity $quantity, $context = null): ?QuantityFormatterInterface |
71
|
|
|
{ |
72
|
|
|
$type = $this->fixType($type); |
73
|
|
|
if ($className = $this->types[$type] ?? null) { |
74
|
|
|
/** @var QuantityFormatterInterface $formatter */ |
75
|
|
|
$formatter = new $className($quantity, $this->intlFormatter); |
76
|
|
|
|
77
|
|
|
if ($formatter instanceof ContextAwareQuantityFormatter) { |
78
|
|
|
$formatter->setContext($context); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $formatter; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function fixType($type): string |
88
|
|
|
{ |
89
|
|
|
if (strpos($type, ',') !== false) { |
90
|
|
|
$types = explode(',', $type); |
91
|
|
|
return end($types); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $type; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.