ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Helpers.php
Go to the documentation of this file.
1<?php
2
4
7
8class Helpers
9{
15 public static function verySmallDenominator(float $numerator, float $denominator)
16 {
17 return (abs($denominator) < 1.0E-12) ? Functions::DIV0() : ($numerator / $denominator);
18 }
19
27 public static function validateNumericNullBool($number)
28 {
29 $number = Functions::flattenSingleValue($number);
30 if ($number === null) {
31 return 0;
32 }
33 if (is_bool($number)) {
34 return (int) $number;
35 }
36 if (is_numeric($number)) {
37 return 0 + $number;
38 }
39
40 throw new Exception(Functions::VALUE());
41 }
42
51 public static function validateNumericNullSubstitution($number, $substitute)
52 {
53 $number = Functions::flattenSingleValue($number);
54 if ($number === null && $substitute !== null) {
55 return $substitute;
56 }
57 if (is_numeric($number)) {
58 return 0 + $number;
59 }
60
61 throw new Exception(Functions::VALUE());
62 }
63
69 public static function validateNotNegative($number, ?string $except = null): void
70 {
71 if ($number >= 0) {
72 return;
73 }
74
75 throw new Exception($except ?? Functions::NAN());
76 }
77
83 public static function validatePositive($number, ?string $except = null): void
84 {
85 if ($number > 0) {
86 return;
87 }
88
89 throw new Exception($except ?? Functions::NAN());
90 }
91
97 public static function validateNotZero($number): void
98 {
99 if ($number) {
100 return;
101 }
102
103 throw new Exception(Functions::DIV0());
104 }
105
106 public static function returnSign(float $number): int
107 {
108 return $number ? (($number > 0) ? 1 : -1) : 0;
109 }
110
111 public static function getEven(float $number): float
112 {
113 $significance = 2 * self::returnSign($number);
114
115 return $significance ? (ceil($number / $significance) * $significance) : 0;
116 }
117
125 public static function numberOrNan($result)
126 {
127 return is_nan($result) ? Functions::NAN() : $result;
128 }
129}
$result
An exception for terminatinating execution or to throw for unit testing.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
static validateNumericNullBool($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:27
static validateNotNegative($number, ?string $except=null)
Confirm number >= 0.
Definition: Helpers.php:69
static validatePositive($number, ?string $except=null)
Confirm number > 0.
Definition: Helpers.php:83
static validateNotZero($number)
Confirm number != 0.
Definition: Helpers.php:97
static numberOrNan($result)
Return NAN or value depending on argument.
Definition: Helpers.php:125
static validateNumericNullSubstitution($number, $substitute)
Validate numeric, but allow substitute for null.
Definition: Helpers.php:51
static verySmallDenominator(float $numerator, float $denominator)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:15