|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 05.02.2018 12:31 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Ptl; |
|
7
|
|
|
|
|
8
|
|
|
use PTLTag; |
|
9
|
|
|
use \template; |
|
10
|
|
|
|
|
11
|
|
|
class PtlVariableDecorator { |
|
12
|
|
|
// Numeric decorators |
|
13
|
|
|
const PARAM_NUMERIC = 'num'; // define numeric decorator |
|
14
|
|
|
const PARAM_NUMERIC_FLOOR = 'floor'; |
|
15
|
|
|
const PARAM_NUMERIC_CEIL = 'ceil'; |
|
16
|
|
|
const PARAM_NUMERIC_ROUND = 'round'; // round=<decimal numbers> |
|
17
|
|
|
|
|
18
|
|
|
const PARAM_NUMERIC_FORMAT = 'format'; |
|
19
|
|
|
const PARAM_NUMERIC_COLOR = 'color'; // Color values: red - negative, yellow - zero, green - positive. Implies "floor" and "format" |
|
20
|
|
|
// const PARAM_NUMERIC_LIMIT = 'percent'; // _number_color_value replacement - see Decorators in PTL test |
|
21
|
|
|
// const PARAM_NUMERIC_LIMIT = 'limit'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Сортированный список поддерживаемых параметров |
|
25
|
|
|
* |
|
26
|
|
|
* @var string[] $allowedParams |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $allowedParams = array( |
|
29
|
|
|
self::PARAM_NUMERIC => '', |
|
30
|
|
|
// Will be dumped for all tags which does not have |num |
|
31
|
|
|
self::PARAM_NUMERIC_CEIL => self::PARAM_NUMERIC, |
|
32
|
|
|
self::PARAM_NUMERIC_FLOOR => self::PARAM_NUMERIC, |
|
33
|
|
|
self::PARAM_NUMERIC_ROUND => self::PARAM_NUMERIC, |
|
34
|
|
|
|
|
35
|
|
|
self::PARAM_NUMERIC_FORMAT => self::PARAM_NUMERIC, |
|
36
|
|
|
self::PARAM_NUMERIC_COLOR => self::PARAM_NUMERIC, |
|
37
|
|
|
// self::PARAM_NUMERIC_LIMIT => self::PARAM_NUMERIC, |
|
|
|
|
|
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $strTagFull - full PTL tag with enclosing curly braces |
|
42
|
|
|
* @param string $phpCompiledVar - compiled var reference ready for ECHO command |
|
43
|
|
|
* @param template $template - template to apply |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function decorate($strTagFull, $phpCompiledVar, $template) { |
|
48
|
|
|
$ptlTag = new PTLTag(substr($strTagFull, 1, strlen($strTagFull) - 2), $template, static::$allowedParams); |
|
49
|
|
|
|
|
50
|
|
|
$phpCompiledVar = static::num($phpCompiledVar, $ptlTag); |
|
51
|
|
|
|
|
52
|
|
|
return $phpCompiledVar; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return function call |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $funcName |
|
59
|
|
|
* @param string $value |
|
60
|
|
|
* @param string[] $params |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected static function func($funcName, $value, $params = []) { |
|
65
|
|
|
return $funcName . '(' . $value . (!empty($params) ? ',' . implode(',', $params) : '') . ')'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $phpCompiledVar |
|
70
|
|
|
* @param PTLTag $ptlTag |
|
71
|
|
|
*/ |
|
72
|
|
|
protected static function num($phpCompiledVar, $ptlTag) { |
|
73
|
|
|
$result = $phpCompiledVar; |
|
74
|
|
|
|
|
75
|
|
|
if (array_key_exists(self::PARAM_NUMERIC, $ptlTag->params)) { |
|
76
|
|
|
// Just dump other params |
|
77
|
|
|
foreach(static::$allowedParams as $paramName => $limitTag) { |
|
78
|
|
|
if ($limitTag != self::PARAM_NUMERIC || !array_key_exists($paramName, $ptlTag->params)) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
switch ($paramName) { |
|
83
|
|
|
case self::PARAM_NUMERIC_CEIL: |
|
84
|
|
|
case self::PARAM_NUMERIC_FLOOR: |
|
85
|
|
|
$result = static::func($paramName, $result, ''); |
|
|
|
|
|
|
86
|
|
|
break; |
|
87
|
|
|
|
|
88
|
|
|
case self::PARAM_NUMERIC_ROUND: |
|
89
|
|
|
$result = static::func($paramName, $result, [intval($ptlTag->params[$paramName])]); |
|
90
|
|
|
break; |
|
91
|
|
|
|
|
92
|
|
|
case self::PARAM_NUMERIC_FORMAT: |
|
93
|
|
|
$result = static::func('HelperString::numberFormat', $result, [0]); |
|
94
|
|
|
break; |
|
95
|
|
|
|
|
96
|
|
|
case self::PARAM_NUMERIC_COLOR: |
|
97
|
|
|
$result = static::func('prettyNumberStyledDefault', $result); |
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.