Completed
Push — trunk ( 153731...fde30a )
by SuperNova.WS
04:15
created

pretty_time_short()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 9.2
1
<?php
2
/**
3
 * Created by Gorlum 04.12.2017 4:25
4
 */
5
6
/**
7
 * Format $value to ID
8
 *
9
 * @param     $value
10
 * @param int $default
11
 *
12
 * @return float|int
13
 */
14
function idval($value, $default = 0) {
15
  $value = floatval($value);
16
17
  return preg_match('#^(\d*)#', $value, $matches) && $matches[1] ? floatval($matches[1]) : $default;
18
}
19
20
21
/**
22
 * @param int|float      $number
23
 * @param true|int|float $compareTo
24
 *    true             - compare to zero from above i.e. resources amount ($number > 0 for positive)
25
 *    numeric positive - compare to $compareTo from below i.e. price with resource amount ($number < $compareTo for positive)
26
 *    numeric negative - compare to -$compareTo from above i.e. resource amount with price ($n > -$compareTo for positive)
27
 *
28
 * @return string
29
 */
30
function prettyNumberGetClass($number, $compareTo) {
31
  $n = floor($number);
32
33
  if ($compareTo === true) {
34
    $class = $n == 0 ? 'zero' : ($n > 0 ? 'positive' : 'negative');
35
  } elseif ($compareTo >= 0) {
36
    $class = $n == $compareTo ? 'zero' : ($n < $compareTo ? 'positive' : 'negative');
37
  } else {
38
    $class = ($n == -$compareTo) ? 'zero' : ($n < -$compareTo ? 'negative' : 'positive');
39
  }
40
41
  return $class;
42
}
43
44
/**
45
 * Return number floored, formatted and styled with "span"
46
 *
47
 * @param int|float $number
48
 *
49
 * @return string
50
 * @see PtlVariableDecorator
51
 * // TODO - this should be made in templates
52
 */
53
function prettyNumberStyledDefault($number) {
54
  return prettyNumberStyledCompare($number, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer|double expected by parameter $compareTo of prettyNumberStyledCompare(). ( Ignorable by Annotation )

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

54
  return prettyNumberStyledCompare($number, /** @scrutinizer ignore-type */ true);
Loading history...
55
}
56
57
/**
58
 * @param int|float $number
59
 * @param int|float $compareTo
60
 *
61
 * @return string
62
 *
63
 * // TODO - this should be made in templates
64
 */
65
function prettyNumberStyledCompare($number, $compareTo) {
66
  return
67
    '<span class="' . prettyNumberGetClass($number, $compareTo) . '">' .
68
    HelperString::numberFloorAndFormat($number) .
69
    '</span>';
70
}
71
72
// ----------------------------------------------------------------------------------------------------------------
73
function pretty_time($seconds, $daysNumeric = false) {
74
  $day = floor($seconds / (24 * 3600));
75
76
  return sprintf(
77
    '%s%02d:%02d:%02d',
78
    $daysNumeric ? $day . '.' : ($day ? $day . SN::$lang['sys_day_short'] . ' ' : ''),
79
    floor($seconds / 3600 % 24),
80
    floor($seconds / 60 % 60),
81
    floor($seconds / 1 % 60)
82
  );
83
}
84
85
function pretty_time_short($seconds, $daysNumeric = true) {
0 ignored issues
show
Unused Code introduced by
The parameter $daysNumeric is not used and could be removed. ( Ignorable by Annotation )

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

85
function pretty_time_short($seconds, /** @scrutinizer ignore-unused */ $daysNumeric = true) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
  $day = floor($seconds / (24 * 3600));
87
88
  return sprintf(
89
    '%s%s%s%02d',
90
    $day ? $day . SN::$lang['sys_day_short'] . ' ' : '',
91
    $seconds > PERIOD_HOUR ? sprintf('%02d:', floor($seconds / 3600 % 24)) : '',
92
    $seconds > PERIOD_MINUTE ? sprintf('%02d:', floor($seconds / 60 % 60)) : '',
93
    floor($seconds / 1 % 60)
94
  );
95
}
96
97
function sys_time_human($time, $full = false) {
98
  global $lang;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
99
100
  $seconds = $time % 60;
101
  $time = floor($time / 60);
102
  $minutes = $time % 60;
103
  $time = floor($time / 60);
104
  $hours = $time % 24;
105
  $time = floor($time / 24);
106
107
  return
108
    ($full || $time ? "{$time} {$lang['sys_day']}&nbsp;" : '') .
109
    ($full || $hours ? "{$hours} {$lang['sys_hrs']}&nbsp;" : '') .
110
    ($full || $minutes ? "{$minutes} {$lang['sys_min']}&nbsp;" : '') .
111
    ($full || !$time || $seconds ? "{$seconds} {$lang['sys_sec']}" : '');
112
}
113
114
function sys_time_human_system($time) {
115
  return $time ? date(FMT_DATE_TIME_SQL, $time) . " ({$time}), " . sys_time_human(SN_TIME_NOW - $time) : '{NEVER}';
116
}
117
118
if (!function_exists('strptime')) {
119
  function strptime($date, $format) {
120
    $masks = array(
121
      '%d' => '(?P<d>[0-9]{2})',
122
      '%m' => '(?P<m>[0-9]{2})',
123
      '%Y' => '(?P<Y>[0-9]{4})',
124
      '%H' => '(?P<H>[0-9]{2})',
125
      '%M' => '(?P<M>[0-9]{2})',
126
      '%S' => '(?P<S>[0-9]{2})',
127
      // usw..
128
    );
129
130
    $rexep = "#" . strtr(preg_quote($format), $masks) . "#";
131
    if (preg_match($rexep, $date, $out)) {
132
      $ret = array(
133
        "tm_sec"  => (int)$out['S'],
134
        "tm_min"  => (int)$out['M'],
135
        "tm_hour" => (int)$out['H'],
136
        "tm_mday" => (int)$out['d'],
137
        "tm_mon"  => $out['m'] ? $out['m'] - 1 : 0,
138
        "tm_year" => $out['Y'] > 1900 ? $out['Y'] - 1900 : 0,
139
      );
140
    } else {
141
      $ret = false;
142
    }
143
144
    return $ret;
145
  }
146
}
147
148
149
function js_safe_string($string) {
150
  return str_replace(array("\r", "\n"), array('\r', '\n'), addslashes($string));
151
}
152
153
function sys_safe_output($string) {
154
  return str_replace(array("&", "\"", "<", ">", "'"), array("&amp;", "&quot;", "&lt;", "&gt;", "&apos;"), $string);
155
}
156
157
function str_raw2unsafe($raw) {
158
  return trim(strip_tags($raw));
159
}
160
161
function ip2longu($ip) {
162
  return sprintf('%u', floatval(ip2long($ip)));
163
}
164
165
/**
166
 * @param int $fullDate
167
 *
168
 * @return int
169
 */
170
function datePart($fullDate) {
171
  return (int)strtotime(date('Y-m-d', $fullDate));
172
}
173
174
175
/**
176
 * Fall-back function to support old, serialize()-d data
177
 *
178
 * @param string $var
179
 *
180
 * @return mixed|string
181
 * @deprecated
182
 */
183
function unserializeOrJsonDecode($var, $asArray = true) {
184
  // Variable is not a string - returning it back
185
  if (!is_string($var) || empty($var)) {
0 ignored issues
show
introduced by
The condition is_string($var) is always true.
Loading history...
186
    return $var;
187
  }
188
189
  set_error_handler(
190
  /**
191
   * @param $errno
192
   * @param $errstr
193
   * @param $errfile
194
   * @param $errline
195
   *
196
   * @return bool
197
   * @throws ErrorException
198
   */
199
    function ($errno, $errstr, $errfile, $errline) {
200
      throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
201
    }
202
  );
203
204
  try {
205
    $result = unserialize($var);
206
  } catch (ErrorException $e) {
207
    // String is not a serialized variable. May be it's a json?
208
    $result = json_decode($var, $asArray);
209
    if (JSON_ERROR_NONE != json_last_error()) {
210
      $result = $var;
211
    }
212
  } finally {
213
    restore_error_handler();
214
  }
215
216
  return $result;
217
}
218