parseNumber()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace AppUtils;
4
5
use DateInterval;
6
use Throwable;
7
8
/**
9
 * Parses the specified number, and returns a NumberInfo instance.
10
 *
11
 * @param NumberInfo|string|int|float|NULL $value
12
 * @param bool $forceNew
13
 * @return NumberInfo
14
 */
15
function parseNumber($value, bool $forceNew=false)
16
{
17
    if($value instanceof NumberInfo && $forceNew !== true) {
18
        return $value;
19
    }
20
    
21
    return new NumberInfo($value);
22
}
23
24
/**
25
 * Like {@see parseNumber()}, but returns an immutable
26
 * instance where any operations that modify the value
27
 * return a new instance, leaving the original instance
28
 * intact.
29
 *
30
 * @param NumberInfo|string|int|float|NULL $value
31
 * @return NumberInfo_Immutable
32
 */
33
function parseNumberImmutable($value) : NumberInfo_Immutable
34
{
35
    return new NumberInfo_Immutable($value);
36
}
37
38
/**
39
 * Parses the specified variable, and allows accessing
40
 * information on it.
41
 * 
42
 * @param mixed $variable
43
 * @return VariableInfo
44
 */
45
function parseVariable($variable) : VariableInfo
46
{
47
    return new VariableInfo($variable);
48
}
49
50
/**
51
 * Like the native PHP function <code>parse_url</code>,
52
 * but with a friendly API and some enhancements and fixes 
53
 * for a few things that the native function handles poorly.
54
 * 
55
 * @param string $url The URL to parse.
56
 * @return URLInfo
57
 */
58
function parseURL(string $url) : URLInfo
59
{
60
    return new URLInfo($url);
61
}
62
63
/**
64
 * Creates a throwable info instance for the specified error,
65
 * which enables accessing additional information on it,
66
 * as well as serializing it to be able to persist it in storage.
67
 * 
68
 * @param Throwable $e
69
 * @return ConvertHelper_ThrowableInfo
70
 */
71
function parseThrowable(Throwable $e) : ConvertHelper_ThrowableInfo
72
{
73
    return ConvertHelper_ThrowableInfo::fromThrowable($e);
74
}
75
76
/**
77
 * Restores a throwable info instance from a previously 
78
 * serialized array.
79
 * 
80
 * @param array<string,mixed> $serialized
81
 * @return ConvertHelper_ThrowableInfo
82
 */
83
function restoreThrowable(array $serialized) : ConvertHelper_ThrowableInfo
84
{
85
    return ConvertHelper_ThrowableInfo::fromSerialized($serialized);
86
}
87
88
/**
89
 * Creates an interval wrapper, that makes it a lot easier
90
 * to work with date intervals. It also solves
91
 *
92
 * @param DateInterval $interval
93
 * @return ConvertHelper_DateInterval
94
 */
95
function parseInterval(DateInterval $interval) : ConvertHelper_DateInterval
96
{
97
    return ConvertHelper_DateInterval::fromInterval($interval);
98
}
99
100
/**
101
 * Translation function used to translate some of the internal
102
 * strings: if the localization is installed, it will use this
103
 * to do the translation.
104
 * 
105
 * @return string
106
 */
107
function t() : string
108
{
109
    $args = func_get_args();
110
    
111
    // is the localization package installed?
112
    if(function_exists('\AppLocalize\t'))
113
    {
114
        return call_user_func_array('\AppLocalize\t', $args);
115
    }
116
    
117
    // simulate the translation function
118
    return strval(call_user_func_array('sprintf', $args));
119
}
120
121
/**
122
 * Creates a boolean value.
123
 * 
124
 * @param bool $initial The initial boolean value to use.
125
 * @return Value_Bool
126
 */
127
function valBool(bool $initial=false) : Value_Bool
128
{
129
    return new Value_Bool($initial);
130
}
131
132
/**
133
 * Creates a sticky true-based boolean value: starts out
134
 * as false, and if it is set to true, cannot be set to
135
 * false again afterwards.
136
 * 
137
 * @param bool $initial
138
 * @return Value_Bool_True
139
 */
140
function valBoolTrue(bool $initial=false) : Value_Bool_True
141
{
142
    return new Value_Bool_True($initial);
143
}
144
145
/**
146
 * Creates a sticky false-based boolean value: starts out
147
 * as true, and if it is set to false, cannot be set to
148
 * true again afterwards.
149
 * 
150
 * @param bool $initial
151
 * @return Value_Bool_False
152
 */
153
function valBoolFalse(bool $initial=true) : Value_Bool_False
154
{
155
    return new Value_Bool_False($initial);
156
}
157
158
/**
159
 * Creates a new StringBuilder instance.
160
 * 
161
 * @return StringBuilder
162
 */
163
function sb() : StringBuilder
164
{
165
    return new StringBuilder();
166
}
167
168
/**
169
 * Whether the current request is run via the command line.
170
 * @return bool
171
 */
172
function isCLI() : bool
173
{
174
    return php_sapi_name() === "cli";
175
}
176
177
/**
178
 * Removes the specified values from the target array.
179
 *
180
 * @param array<mixed> $haystack
181
 * @param array<mixed> $values
182
 * @param bool $strict
183
 * @return array<mixed>
184
 */
185
function array_remove_values(array $haystack, array $values, bool $strict=true) : array
186
{
187
    return array_filter(
188
        $haystack,
189
        static fn($entry) => !in_array($entry, $values, $strict)
190
    );
191
}
192
193
/**
194
 * Initializes the utilities: this is called automatically
195
 * because this file is included in the files list in the
196
 * composer.json, guaranteeing it is always loaded.
197
 */
198
function init() : void
199
{
200
    if(!class_exists('\AppLocalize\Localization')) {
201
        return;
202
    }
203
    
204
    $installFolder = realpath(__DIR__.'/../');
205
    
206
    // Register the classes as a localization source,
207
    // so they can be found, and use the bundled localization
208
    // files.
209
    \AppLocalize\Localization::addSourceFolder(
0 ignored issues
show
Bug introduced by
The type AppLocalize\Localization was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
210
        'application-utils',
211
        'Application Utils Package',
212
        'Composer Packages',
213
        $installFolder.'/localization',
214
        $installFolder.'/src'
215
    );
216
}
217
218
init();
219