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\factories; |
12
|
|
|
|
13
|
|
|
use hipanel\helpers\StringHelper; |
14
|
|
|
use hipanel\modules\finance\models\CertificatePrice; |
15
|
|
|
use hipanel\modules\finance\models\DomainServicePrice; |
16
|
|
|
use hipanel\modules\finance\models\DomainZonePrice; |
17
|
|
|
use hipanel\modules\finance\models\OveruseServerPrice; |
18
|
|
|
use hipanel\modules\finance\models\Price; |
19
|
|
|
use hipanel\modules\finance\models\RatePrice; |
20
|
|
|
use hipanel\modules\finance\models\TemplatePrice; |
21
|
|
|
use yii\base\InvalidConfigException; |
22
|
|
|
use yii\base\Model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class PriceModelFactory builds objects, that must be children of [[Price]] class |
26
|
|
|
* using short class name. See definitions in $map property. |
27
|
|
|
* |
28
|
|
|
* @author Dmytro Naumenko <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class PriceModelFactory |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $map = [ |
36
|
|
|
'Price' => Price::class, |
37
|
|
|
'RatePrice' => RatePrice::class, |
38
|
|
|
'TemplatePrice' => TemplatePrice::class, |
39
|
|
|
'RateTemplatePrice' => TemplatePrice::class, |
40
|
|
|
'OveruseServerPrice' => OveruseServerPrice::class, |
41
|
|
|
'SinglePrice' => [ |
42
|
|
|
'domain,*' => DomainZonePrice::class, |
43
|
|
|
'feature,*' => DomainServicePrice::class, |
44
|
|
|
'overuse,server_traf95_max' => OveruseServerPrice::class, |
45
|
|
|
'*' => Price::class, |
46
|
|
|
], |
47
|
|
|
'CertificatePrice' => CertificatePrice::class, |
48
|
|
|
'EnumPrice' => CertificatePrice::class, |
49
|
|
|
'DomainZonePrice' => DomainZonePrice::class, |
50
|
|
|
'DomainServicePrice' => DomainServicePrice::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $className |
55
|
|
|
* @param string|null $priceType |
56
|
|
|
* @throws InvalidConfigException |
57
|
|
|
* @return Price |
58
|
|
|
*/ |
59
|
|
|
public function build(string $className, ?string $priceType = null) |
60
|
|
|
{ |
61
|
|
|
if (!isset($this->map[$className])) { |
62
|
|
|
throw new InvalidConfigException('Can not create model for class ' . $className); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (is_array($this->map[$className])) { |
66
|
|
|
foreach ($this->map[$className] as $type => $class) { |
67
|
|
|
if (StringHelper::matchWildcard($type, (string) $priceType)) { |
68
|
|
|
return new $class(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new $this->map[$className](); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getMap(): array |
80
|
|
|
{ |
81
|
|
|
return $this->map; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function instantiate(string $className): Model |
85
|
|
|
{ |
86
|
|
|
return new $className(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|