Test Failed
Push — trunk ( 0d5d14...30e273 )
by SuperNova.WS
11:46
created

PtlVariableDecorator::datetime()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 2
nop 2
dl 0
loc 20
rs 9.2222
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 42
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
  const PARAM_DATETIME = 'datetime'; // define date and/or time decorator
24
25
  /**
26
   * Сортированный список поддерживаемых параметров
27
   *
28
   * @var string[] $allowedParams
29
   */
30
  protected static $allowedParams = array(
31
    self::PARAM_NUMERIC       => '',
32
    // Will be dumped for all tags which does not have |num
33
    self::PARAM_NUMERIC_CEIL  => self::PARAM_NUMERIC,
34
    self::PARAM_NUMERIC_FLOOR => self::PARAM_NUMERIC,
35
    self::PARAM_NUMERIC_ROUND => self::PARAM_NUMERIC,
36
37
    self::PARAM_NUMERIC_FORMAT => self::PARAM_NUMERIC,
38
    self::PARAM_NUMERIC_COLOR  => self::PARAM_NUMERIC,
39
//    self::PARAM_NUMERIC_LIMIT  => self::PARAM_NUMERIC,
40
41
    self::PARAM_DATETIME       => self::PARAM_DATETIME,
42
  );
43
44
  /**
45
   * @param string   $strTagFull - full PTL tag with enclosing curly braces
46
   * @param string   $phpCompiledVar - compiled var reference ready for ECHO command
47
   * @param template $template - template to apply
48
   *
49
   * @return mixed
50
   */
51
  public static function decorate($strTagFull, $phpCompiledVar, $template) {
52
    $ptlTag = new PTLTag(substr($strTagFull, 1, strlen($strTagFull) - 2), $template, static::$allowedParams);
53
54
    $phpCompiledVar = static::num($phpCompiledVar, $ptlTag);
55
    $phpCompiledVar = static::datetime($phpCompiledVar, $ptlTag);
56
57
    return $phpCompiledVar;
58
  }
59
60
  /**
61
   * Return function call
62
   *
63
   * @param string   $funcName
64
   * @param string   $value
65
   * @param string[] $params
66
   *
67
   * @return string
68
   */
69
  protected static function func($funcName, $value, $params = []) {
70
    return $funcName . '(' . $value . (!empty($params) ? ',' . implode(',', $params) : '') . ')';
71
  }
72
73
  protected static function func2($funcName, $params = []) {
74
    return $funcName . '(' . (!empty($params) ? implode(',', $params) : '') . ')';
75
  }
76
77
  /**
78
   * @param        $phpCompiledVar
79
   * @param PTLTag $ptlTag
80
   */
81
  protected static function num($phpCompiledVar, $ptlTag) {
82
    $result = $phpCompiledVar;
83
84
    if (array_key_exists(self::PARAM_NUMERIC, $ptlTag->params)) {
85
      // Just dump other params
86
      foreach (static::$allowedParams as $paramName => $limitTag) {
87
        if ($limitTag != self::PARAM_NUMERIC || !array_key_exists($paramName, $ptlTag->params)) {
88
          continue;
89
        }
90
91
        switch ($paramName) {
92
          case self::PARAM_NUMERIC_CEIL:
93
          case self::PARAM_NUMERIC_FLOOR:
94
            $result = static::func($paramName, $result, '');
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type string[] expected by parameter $params of Ptl\PtlVariableDecorator::func(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            $result = static::func($paramName, $result, /** @scrutinizer ignore-type */ '');
Loading history...
95
          break;
96
97
          case self::PARAM_NUMERIC_ROUND:
98
            $result = static::func($paramName, $result, [intval($ptlTag->params[$paramName])]);
99
          break;
100
101
          case self::PARAM_NUMERIC_FORMAT:
102
            $result = static::func('HelperString::numberFormat', $result, [0]);
103
          break;
104
105
          case self::PARAM_NUMERIC_COLOR:
106
            $result = static::func('prettyNumberStyledDefault', $result);
107
          break;
108
        }
109
      }
110
111
    }
112
113
    return $result;
114
  }
115
116
  /**
117
   * @param        $phpCompiledVar
118
   * @param PTLTag $ptlTag
119
   */
120
  protected static function datetime($phpCompiledVar, $ptlTag) {
121
    $result = $phpCompiledVar;
122
123
    if (array_key_exists(self::PARAM_DATETIME, $ptlTag->params)) {
124
      // Just dump other params
125
      foreach (static::$allowedParams as $paramName => $limitTag) {
126
        if ($limitTag != self::PARAM_DATETIME || !array_key_exists($paramName, $ptlTag->params)) {
127
          continue;
128
        }
129
130
        switch ($paramName) {
131
          case self::PARAM_DATETIME:
132
            $result = "empty($result) ? '' : " . static::func2('date', ["'" . FMT_DATE_TIME_SQL . "'", $result]);
133
          break;
134
        }
135
      }
136
137
    }
138
139
    return $result;
140
  }
141
142
}
143