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