Passed
Push — master ( b28623...b691b7 )
by Sebastian
04:23
created

valBoolFalse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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