Completed
Push — master ( aeaa5d...9fb24b )
by Gino
23:28 queued 09:34
created

TDMCreatePhpCode::getPhpCodeRawurlencode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: TDMCreatePhpCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
/**
26
 * Class TDMCreatePhpCode.
27
 */
28
class TDMCreatePhpCode
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
    }
40
41
    /*
42
    *  @static function &getInstance
43
    *  @param null
44
    */
45
    /**
46
     * @return TDMCreatePhpCode
47
     */
48
    public static function &getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /*
59
    *  @public function getPhpCodeCommentLine
60
    *  @param $comment
61
    *  @param $var
62
    *  @return string
63
    */
64
    public function getPhpCodeCommentLine($comment = '', $var = '')
65
    {
66
        return "// {$comment} {$var}\n";
67
    }
68
69
    /*
70
    *  @public function getPhpCodeVariables
71
    *  @param $type
72
    *  @param $var
73
    *  @return string
74
    */
75
    public function getPhpCodeVariables($type = 'REQUEST', $var = '')
76
    {
77
        $type = strtoupper($type);
78
        switch ($type) {
79
            case 'GET':
80
                $ret = "\$_GET['{$var}']";
81
                break;
82
            case 'POST':
83
                $ret = "\$_POST['{$var}']";
84
                break;
85
            case 'FILES':
86
                $ret = "\$_FILES['{$var}']";
87
                break;
88
            case 'COOKIE':
89
                $ret = "\$_COOKIE['{$var}']";
90
                break;
91
            case 'ENV':
92
                $ret = "\$_ENV['{$var}']";
93
                break;
94
            case 'SERVER':
95
                $ret = "\$_SERVER['{$var}']";
96
                break;
97
            default:
98
                $ret = "\$_REQUEST['{$var}']";
99
                break;
100
        }
101
102
        return $ret;
103
    }
104
105
    /*
106
    *  @public function getPhpCodeFileExists
107
    *  @param $filename
108
    *
109
    *  @return string
110
    */
111
    public function getPhpCodeFileExists($filename)
112
    {
113
        return "file_exists({$filename})";
114
    }
115
116
    /*
117
    *  @public function getPhpCodeIncludeDir
118
    *  @param $directory
119
    *  @param $filename
120
    *  @param $once
121
    *  @param $isPath
122
    *
123
    *  @return string
124
    */
125
    public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false)
126
    {
127
        if ($once === false) {
128
            if ($isPath === false) {
129
                $ret = "include {$directory} .'/{$filename}.php';\n";
130
            } else {
131
                $ret = "include {$directory};\n";
132
            }
133
        } else {
134
            if ($isPath === false) {
135
                $ret = "include_once {$directory} .'/{$filename}.php';\n";
136
            } else {
137
                $ret = "include_once {$directory};\n";
138
            }
139
        }
140
141
        return $ret;
142
    }
143
144
    /*
145
     * @public function getPhpCodeConditions
146
     * @param string $condition
147
     * @param string $operator
148
     * @param string $type
149
     * @param string $contentIf
150
     * @param mixed  $contentElse
151
     *
152
     * @return string
153
     */
154
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false)
155
    {
156
        if (false === $contentElse) {
157
            $ret = <<<EOT
158
if ({$condition}{$operator}{$type}) {
159
	{$contentIf}
160
}\n
161
EOT;
162
        } else {
163
            $ret = <<<EOT
164
if ({$condition}{$operator}{$type}) {
165
	{$contentIf}
166
} else {
167
	{$contentElse}
168
}\n
169
EOT;
170
        }
171
172
        return $ret;
173
    }
174
175
    /*
176
     * @public function getPhpCodeForeach
177
     * @param string $array
178
	 * @param string $arrayKey     
179
     * @param string $key
180
     * @param string $value     
181
	 * @param string $content
182
     *
183
     * @return string
184
     */
185
    public function getPhpCodeForeach($array = '', $arrayKey = false, $key = false, $value = false, $content = '')
186
    {
187
        $vars = '';
188
		if ((false === $arrayKey) && (false === $key)) {
189
            $vars = "\${$array} as \${$value}";
190
        } elseif ((false === $arrayKey) && (false !== $key)) {
191
            $vars = "\${$array} as \${$key} => \${$value}";
192
        } elseif ((false !== $arrayKey) && (false === $key)) {
193
            $vars = "array_keys(\${$array}) as \${$value}";
194
        }
195
196
        $ret = <<<EOT
197
foreach({$vars}) {
198
	{$content}
199
}\n
200
EOT;
201
202
        return $ret;
203
    }
204
205
    /*
206
     * @public function getPhpCodeFor
207
     * @param $var
208
     * @param $content
209
     * @param $value
210
     * @param $initVal
211
     * @param $operator
212
     *
213
     * @return string
214
     */
215
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '')
216
    {
217
        $ret = <<<EOT
218
for(\${$var} = {$initVal}; \${$var} {$operator} \${$value}; \${$var}++) {
219
	{$content}
220
}\n
221
EOT;
222
223
        return $ret;
224
    }
225
226
    /*
227
     * @public function getPhpCodeWhile
228
     * @param $var
229
     * @param $content
230
     * @param $value
231
     * @param $operator
232
     *
233
     * @return string
234
     */
235
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '')
236
    {
237
        $ret = <<<EOT
238
while(\${$var} {$operator} {$value}) {
239
	{$content}
240
}\n
241
EOT;
242
243
        return $ret;
244
    }
245
246
    /**
247
     *  @public function getPhpCodeSwitch
248
     *
249
     *  @param $op
250
     *  @param $content
251
     *
252
     *  @return string
253
     */
254
    public function getPhpCodeSwitch($op = '', $content = '')
255
    {
256
        $ret = <<<EOT
257
// Switch options
258
switch (\${$op}){
259
{$content}
260
}\n
261
EOT;
262
263
        return $ret;
264
    }
265
266
    /**
267
     *  @public function getPhpCodeCaseSwitch     
268
     *  @param $cases
269
	 *  @param $defaultAfterCase
270
	 *  @param $default
271
     *
272
     *  @return string
273
     */
274
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false)
275
    {        
276
        $ret = '';
277
		$def = "\tdefault:\n";
278
        foreach($cases as $case => $value) {
279
			$case = is_string($case) ? "'{$case}'" : $case;
280
			if (!empty($case)) {
281
				$ret .= "\tcase {$case}:\n";
282
				if($defaultAfterCase != false){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
283
					$ret .= $def;
284
				}
285
				if (is_array($value)) {
286
					foreach($value as $content) {
287
						$ret .= "\t\t{$content}\n";
288
					}
289
				}
290
				$ret .= "\tbreak;\n";
291
			}	
292
			$defaultAfterCase = false;
293
		}		
294
        if ($default !== false) {
295
            $ret .= $def;
296
			$ret .= "\t\t{$default}\n";
297
			$ret .= "\tbreak;\n";			
298
        }
299
300
        return $ret;
301
    }
302
303
    /*
304
    *  @public function getPhpCodeIsset
305
    *  @param $var
306
    *  @return string
307
    */
308
    public function getPhpCodeIsset($var)
309
    {
310
        return "isset(\${$var})";
311
    }
312
	
313
	/*
314
    *  @public function getPhpCodeUnset
315
    *  @param $var
316
    *  @return string
317
    */
318
    public function getPhpCodeUnset($var = '')
319
    {
320
        return "unset(\${$var});\n";
321
    }
322
323
    /*
324
    *  @public function getPhpCodeImplode
325
    *  @param $left
326
    *  @param $right
327
    *  @return string
328
    */
329
    public function getPhpCodeImplode($left, $right)
330
    {
331
        return "implode('{$left} ', {$right})";
332
    }
333
334
    /*
335
    *  @public function getPhpCodeSprintf
336
    *  @param $left
337
    *  @param $right
338
    *  @return string
339
    */
340
    public function getPhpCodeSprintf($left, $right)
341
    {
342
        return "sprintf({$left}, {$right})";
343
    }
344
345
    /*
346
    *  @public function getPhpCodeEmpty
347
    *  @param $var
348
    *  @return string
349
    */
350
    public function getPhpCodeEmpty($var)
351
    {
352
        return "empty({$var})";
353
    }
354
355
    /*
356
    *  @public function getPhpCodeHeader
357
    *  @param $var
358
    *  @return string
359
    */
360
    public function getPhpCodeHeader($var)
361
    {
362
        return "header({$var})";
363
    }
364
365
    /*
366
    *  @public function getPhpCodeRawurlencode
367
    *  @param $var
368
    *  @return string
369
    */
370
    public function getPhpCodeRawurlencode($var)
371
    {
372
        return "rawurlencode({$var})";
373
    }
374
	
375
	/*
376
    *  @public function getPhpCodePregReplace
377
    *  @param $return
378
	*  @param $exp
379
	*  @param $str
380
	*  @param $val
381
	*  @param $isParam
382
    *
383
    *  @return string
384
    */
385
    public function getPhpCodePregReplace($return, $exp, $str, $val, $isParam = false)
386
    {
387
		if ($isParam === false) {	
388
			$ret = "\${$return} = preg_replace( '{$exp}' , '{$str}' , {$val});\n";
389
		} else {
390
			$ret = "preg_replace( '{$exp}' , '{$str}' , {$val})";
391
		}
392
		return $ret;
393
    }
394
	
395
	/*
396
    *  @public function getPhpCodeStrReplace
397
    *  @param $left
398
	*  @param $var
399
	*  @param $str
400
	*  @param $value
401
	*  @param $isParam
402
    *
403
    *  @return string
404
    */
405
    public function getPhpCodeStrReplace($left, $var, $str, $value, $isParam = false)
406
    {
407
        if ($isParam === false) {	
408
			$ret = "\${$left} = str_replace( '{$var}' , '{$str}' , {$value});\n";
409
		} else {
410
			$ret = "str_replace( '{$var}' , '{$str}' , {$value})";
411
		}
412
		return $ret;
413
    }
414
	
415
	/*
416
    *  @public function getPhpCodeStripTags
417
    *  @param $left
418
	*  @param $value
419
	*  @param $isParam
420
    *
421
    *  @return string
422
    */
423
    public function getPhpCodeStripTags($left, $value, $isParam = false)
424
    {
425
        if ($isParam === false) {	
426
			$ret = "\${$left} = strip_tags({$value});\n";
427
		} else {
428
			$ret = "strip_tags({$value})";
429
		}
430
		return $ret;
431
    }
432
433
    /*
434
    *  @public function getPhpCodeHtmlentities
435
    *  @param $entitiesVar
436
    *  @param $entitiesQuote
437
    *  @return string
438
    */
439
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
440
    {
441
        $entitiesVar = $entitiesQuote !== false ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
442
        $entities = "htmlentities({$entitiesVar})";
443
444
        return $entities;
445
    }
446
447
    /*
448
    *  @public function getPhpCodeHtmlspecialchars
449
    *  @param $specialVar
450
    *  @param $specialQuote
451
    *  @return string
452
    */
453
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
454
    {
455
        $specialVar = $specialQuote !== false ? $specialVar.', '.$specialQuote : $specialVar;
456
        $specialchars = "htmlspecialchars({$specialVar})";
457
458
        return $specialchars;
459
    }
460
}
461