1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\providers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FormulaExamplesProvider |
9
|
|
|
* |
10
|
|
|
* @author Dmytro Naumenko <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class FormulaExamplesProvider |
13
|
|
|
{ |
14
|
|
|
public function getGroups() |
15
|
|
|
{ |
16
|
|
|
$result[] = $this->groupOf(Yii::t('hipanel.finance.price', 'Fixed discount'), $this->fixedDiscountFormulas()); |
|
|
|
|
17
|
|
|
$result[] = $this->groupOf(Yii::t('hipanel.finance.price', 'Growing discount'), $this->growingDiscountFormulas()); |
18
|
|
|
$result[] = $this->groupOf(Yii::t('hipanel.finance.price', 'Leasing'), $this->leasingFormulas()); |
19
|
|
|
|
20
|
|
|
return $result; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function fixedDiscountFormulas() |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
sprintf("discount.since('%s').fixed('10%%').reason('because')", date('m.Y')), |
27
|
|
|
sprintf("discount.since('%s').fixed('11 USD').reason('agreed')", date('m.Y')), |
28
|
|
|
sprintf("discount.fixed('20%%').till('%s').reason('loyalty')", date('m.Y', strtotime('+6 months'))), |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function growingDiscountFormulas() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
sprintf("discount.since('%s').grows('10%%').every('month').max('100%%').reason('because')", date('m.Y')), |
36
|
|
|
sprintf("discount.since('%s').min('50 USD').grows('20 USD').every('2 months').max('80 USD')", date('m.Y')), |
37
|
|
|
sprintf("discount.since('%s').min('5%%').grows('1%%').every('1 months').max('25%%')", date('m.Y')), |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function leasingFormulas() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
sprintf("leasing.since('%s').lasts('3 months').reason('TEST')", date('m.Y')), |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function groupOf($name, $formulas) |
49
|
|
|
{ |
50
|
|
|
return ['name' => $name, 'formulas' => $formulas]; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
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.