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\forms; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\logic\IntegrityException; |
14
|
|
|
use hipanel\modules\finance\models\CertificateResource; |
15
|
|
|
use yii\web\UnprocessableEntityHttpException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CertificateTariffForm. |
19
|
|
|
* |
20
|
|
|
* @author Dmytro Naumenko <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class CertificateTariffForm extends AbstractTariffForm |
23
|
|
|
{ |
24
|
|
|
protected $certificateTypes = []; |
25
|
|
|
|
26
|
|
|
public function __construct(array $config = []) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($config); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function load($data, $formName = null) |
32
|
|
|
{ |
33
|
|
|
$this->setAttributes($data[$this->formName()]); |
34
|
|
|
$this->setResources($data[(new CertificateResource())->formName()]); |
35
|
|
|
|
36
|
|
|
$this->initTariff(); |
37
|
|
|
|
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public static function getPeriods() |
45
|
|
|
{ |
46
|
|
|
return CertificateResource::getPeriods(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setResources($resources) |
50
|
|
|
{ |
51
|
|
|
$result = []; |
52
|
|
|
foreach ($resources as $resource) { |
53
|
|
|
$result[] = $this->createResource($resource); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->_resources = $result; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function createResource($resource) |
62
|
|
|
{ |
63
|
|
|
if ($resource instanceof CertificateResource) { |
64
|
|
|
return $resource; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$model = new CertificateResource(['scenario' => $this->scenario ?: 'default']); |
68
|
|
|
if ($model->load($resource, '') && $model->validate()) { |
69
|
|
|
return $model; |
70
|
|
|
} else { |
71
|
|
|
throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors())); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getCertificateTypes() |
76
|
|
|
{ |
77
|
|
|
$result = []; |
78
|
|
|
|
79
|
|
|
foreach ($this->tariff->resources as $resource) { |
80
|
|
|
if (isset($this->certificateTypes[$resource->object_id])) { |
81
|
|
|
$result[$resource->object_id] = $this->certificateTypes[$resource->object_id]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getCertificateTypeId($type) |
89
|
|
|
{ |
90
|
|
|
return array_search($type, $this->certificateTypes, true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $type |
95
|
|
|
* @throws IntegrityException |
96
|
|
|
* @return CertificateResource[] |
97
|
|
|
*/ |
98
|
|
|
public function getTypeResources($type) |
99
|
|
|
{ |
100
|
|
|
return $this->extractResources($type, $this->tariff->resources); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $certificateTypes |
105
|
|
|
*/ |
106
|
|
|
public function setCertificateTypes($certificateTypes) |
107
|
|
|
{ |
108
|
|
|
$this->certificateTypes = $certificateTypes; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $type |
113
|
|
|
* @throws IntegrityException |
114
|
|
|
* @return CertificateResource[] |
115
|
|
|
*/ |
116
|
|
|
public function getTypeParentResources($type) |
117
|
|
|
{ |
118
|
|
|
return $this->extractResources($type, $this->parentTariff->resources); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function extractResources($certificateType, $resources) |
122
|
|
|
{ |
123
|
|
|
$id = $this->getCertificateTypeId($certificateType); |
124
|
|
|
|
125
|
|
|
$tmpres = []; |
126
|
|
|
|
127
|
|
|
foreach ($resources as $resource) { |
128
|
|
|
if (strcmp($resource->object_id, $id) === 0 && $resource->isTypeCorrect()) { |
129
|
|
|
$tmpres[$resource->type] = $resource; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$types = $resource->getTypes(); |
134
|
|
|
/* XXX why die? let's try with empty resource |
135
|
|
|
* if (count($tmpres) !== count($types)) { |
136
|
|
|
throw new IntegrityException('Found ' . count($tmpres) . ' resources for certificate "' . $type . '". Must be exactly ' . count($types)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// sorts $tmpres by order of $resource->getTypes() |
140
|
|
|
$tmpres = array_merge($types, $tmpres); |
141
|
|
|
*/ |
142
|
|
|
|
143
|
|
|
foreach (array_keys($types) as $type) { |
144
|
|
|
$res[$type] = isset($tmpres[$type]) ? $tmpres[$type] : $this->createResource([ |
|
|
|
|
145
|
|
|
'object_id' => $id, |
146
|
|
|
'type' => $type, |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $res; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.