Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.12% |
51 / 67 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| common_Utils | |
76.12% |
51 / 67 |
|
28.57% |
2 / 7 |
42.26 | |
0.00% |
0 / 1 |
| isUri | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| toResource | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| fullTrim | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNewUri | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toPHPVariableString | |
82.76% |
24 / 29 |
|
0.00% |
0 / 1 |
12.74 | |||
| toHumanReadablePhpString | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
6 | |||
| getResourceId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | use oat\oatbox\PhpSerializable; |
| 4 | use oat\oatbox\service\ServiceManager; |
| 5 | use oat\generis\model\kernel\uri\UriProvider; |
| 6 | |
| 7 | /** |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; under version 2 |
| 11 | * of the License (non-upgradable). |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | * |
| 22 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 23 | * (under the project TAO & TAO2); |
| 24 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 25 | * (under the project TAO-TRANSFER); |
| 26 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 27 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | * Generis Object Oriented API - common/class.Utils.php |
| 34 | * |
| 35 | * Utility functions |
| 36 | * |
| 37 | * This file is part of Generis Object Oriented API. |
| 38 | * |
| 39 | * @author lionel.lecaque@tudor.lu |
| 40 | * @package generis |
| 41 | |
| 42 | * @see @license GNU General Public (GPL) Version 2 http://www.opensource.org/licenses/gpl-2.0.php |
| 43 | */ |
| 44 | |
| 45 | class common_Utils |
| 46 | { |
| 47 | // --- ASSOCIATIONS --- |
| 48 | |
| 49 | |
| 50 | // --- ATTRIBUTES --- |
| 51 | |
| 52 | // --- OPERATIONS --- |
| 53 | |
| 54 | /** |
| 55 | * Check if the given string is a proper uri |
| 56 | * |
| 57 | * @access public |
| 58 | * @author Joel Bout, <joel@taotesting.com> |
| 59 | * @param string strarg |
| 60 | * @return bool |
| 61 | */ |
| 62 | public static function isUri($strarg) |
| 63 | { |
| 64 | $returnValue = (bool) false; |
| 65 | $uri = trim($strarg); |
| 66 | if (!empty($uri)) { |
| 67 | if ( |
| 68 | ( |
| 69 | preg_match("/^(http|https|file|ftp):\/\/[\/:.A-Za-z0-9_-]+#[A-Za-z0-9_-]+$/", $uri) |
| 70 | && strpos($uri, '#') > 0 |
| 71 | ) |
| 72 | || strpos($uri, "#") === 0 |
| 73 | ) { |
| 74 | $returnValue = true; |
| 75 | } |
| 76 | } |
| 77 | return (bool) $returnValue; |
| 78 | } |
| 79 | |
| 80 | public static function toResource($value) |
| 81 | { |
| 82 | if (is_array($value)) { |
| 83 | $returnValue = []; |
| 84 | foreach ($value as $val) { |
| 85 | $returnValue[] = self::toResource($val); |
| 86 | } |
| 87 | return $returnValue; |
| 88 | } else { |
| 89 | if (common_Utils::isUri($value)) { |
| 90 | return new core_kernel_classes_Resource($value); |
| 91 | } else { |
| 92 | return new core_kernel_classes_Literal($value); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Removes starting/ending spaces, strip html tags out, remove any \r and \n |
| 99 | * |
| 100 | * @access public |
| 101 | * @author Joel Bout, <joel@taotesting.com> |
| 102 | * @param string strarg |
| 103 | * @return string |
| 104 | */ |
| 105 | public static function fullTrim($strarg) |
| 106 | { |
| 107 | return strip_tags(trim($strarg)); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Backward compatibility function for URI Provider |
| 113 | * |
| 114 | * @access public |
| 115 | * @author Joel Bout, <joel@taotesting.com> |
| 116 | * @return string |
| 117 | * @deprecated |
| 118 | */ |
| 119 | public static function getNewUri() |
| 120 | { |
| 121 | return ServiceManager::getServiceManager()->get(UriProvider::SERVICE_ID)->provide(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the php code, that if evaluated |
| 126 | * would return the value provided |
| 127 | * |
| 128 | * @access public |
| 129 | * @author Joel Bout, <joel@taotesting.com> |
| 130 | * @param value |
| 131 | * @return string |
| 132 | */ |
| 133 | public static function toPHPVariableString($value) |
| 134 | { |
| 135 | switch (gettype($value)) { |
| 136 | case "string": |
| 137 | $returnValue = '\'' . str_replace('\'', '\\\'', str_replace('\\', '\\\\', $value)) . '\''; |
| 138 | break; |
| 139 | case "boolean": |
| 140 | $returnValue = $value ? 'true' : 'false'; |
| 141 | break; |
| 142 | case "integer": |
| 143 | case "double": |
| 144 | $returnValue = $value; |
| 145 | break; |
| 146 | case "array": |
| 147 | $string = ""; |
| 148 | foreach ($value as $key => $val) { |
| 149 | $string .= self::toPHPVariableString($key) . " => " . self::toPHPVariableString($val) . ","; |
| 150 | } |
| 151 | $returnValue = "array(" . substr($string, 0, -1) . ")"; |
| 152 | break; |
| 153 | case "NULL": |
| 154 | $returnValue = 'null'; |
| 155 | break; |
| 156 | case "object": |
| 157 | if ($value instanceof PhpSerializable) { |
| 158 | $returnValue = $value->__toPhpCode(); |
| 159 | } else { |
| 160 | $returnValue = 'unserialize(' . self::toPHPVariableString(serialize($value)) . ')'; |
| 161 | } |
| 162 | break; |
| 163 | default: |
| 164 | // resource and unexpected types |
| 165 | throw new common_exception_Error( |
| 166 | "Could not convert variable of type " . gettype($value) . " to PHP variable string" |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | return (string) $returnValue; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Same as toPHPVariableString except the sting |
| 175 | * is easier for humans to read |
| 176 | * |
| 177 | * @param mixed $value |
| 178 | * @param number indentation |
| 179 | * @return string |
| 180 | */ |
| 181 | public static function toHumanReadablePhpString($value, $indentation = 0) |
| 182 | { |
| 183 | if (is_array($value)) { |
| 184 | $array = array_keys($value); |
| 185 | $assocArray = ($array !== array_keys($array)); |
| 186 | $string = ""; |
| 187 | if ($assocArray) { |
| 188 | foreach ($value as $key => $val) { |
| 189 | $string .= PHP_EOL . str_repeat(' ', $indentation + 1) |
| 190 | . self::toPHPVariableString($key) |
| 191 | . " => " |
| 192 | . self::toHumanReadablePhpString($val, $indentation + 1) . ","; |
| 193 | } |
| 194 | } else { |
| 195 | foreach ($value as $val) { |
| 196 | $string .= PHP_EOL . str_repeat(' ', $indentation + 1) |
| 197 | . self::toHumanReadablePhpString($val, $indentation + 1) . ","; |
| 198 | } |
| 199 | } |
| 200 | $returnValue = "array(" . substr($string, 0, -1) . PHP_EOL . str_repeat(' ', $indentation) . ")"; |
| 201 | } elseif (is_string($value)) { |
| 202 | $returnValue = self::toPHPVariableString($value); |
| 203 | } else { |
| 204 | $lines = explode(PHP_EOL, self::toPHPVariableString($value)); |
| 205 | $returnValue = implode(PHP_EOL . str_repeat(' ', $indentation), $lines); |
| 206 | } |
| 207 | |
| 208 | return (string) $returnValue; |
| 209 | } |
| 210 | |
| 211 | public static function getResourceId(string $uri): string |
| 212 | { |
| 213 | return substr($uri, strpos($uri, '#') + 1); |
| 214 | } |
| 215 | } |