-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.php
67 lines (61 loc) · 2.2 KB
/
Utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts, <jerome@taotesting.com>
* @license GPLv2
* @package
*/
namespace qtism\data\storage\php;
/**
* This class provides utility methods dedicated to PHP data storage.
*
* @author Jérôme Bogaerts <jerome@taotesting.com>
*
*/
class Utils {
/**
* Whether a given $value is considered to be scalar.
*
* A value will be considered scalar if it is a PHP scalar
* value or the null value.
*
* @return boolean
*/
static public function isScalar($value) {
return is_scalar($value) === true || is_null($value) === true;
}
/**
* Whether a given $string represents a variable reference e.g. '$foobar'.
*
* @return boolean
*/
static public function isVariableReference($string) {
return is_string($string) === true && mb_strpos($string, '$') === 0 && mb_strlen($string, 'UTF-8') > 1;
}
/**
* Returns a PHP style double quoted string.
*
* @param string $string The string to be quoted (e.g. blabla).
* @return string The quoted string (e.g. "blabla").
*/
static public function doubleQuotedPhpString($string) {
$escapes = array("\\", '"', "\n", "\t", "\v", "\r", "\f", "$");
$replace = array("\\\\", '\\"', "\\n", "\\t", "\\v", "\\r", "\\f", "\\$");
return "\"" . str_replace($escapes, $replace, $string) . "\""; // UTF-8 safe.
}
}