Passed
Branch master (31be8b)
by Gino
03:04
created

TDMCreatePhpCode::getPhpCodeCaseSwitch()   C

Complexity

Conditions 8
Paths 22

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 5.3846
cc 8
eloc 19
nc 22
nop 4
1
<?php
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
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
        $value = !empty($var) ? ' '.$var : '';
67
        $ret = "// {$comment}{$value}\n";
68
69
        return $ret;
70
    }
71
72
    /*
73
     * @public function getPhpCodeCommentMultiLine
74
     * @param $multiLine     
75
     *
76
     * @return string
77
     */
78
    public function getPhpCodeCommentMultiLine($multiLine = array(), $t = '')
79
    {
80
        $values = !empty($multiLine) ? $multiLine : array();
81
        $ret = "{$t}/**\n";
82
        foreach ($values as $string => $value) {
83
            if ($string === '' && $value === '') {
84
                $ret .= "{$t} *\n{$t}";
85
            } else {
86
                $ret .= "{$t} * {$string} {$value}\n{$t}";
87
            }
88
        }
89
        $ret .= "{$t}*/\n";
90
91
        return $ret;
92
    }
93
94
    /*
95
    *  @public function getPhpCodeDefine
96
    *  @param $left
97
    *  @param $right
98
    *
99
    *  @return string
100
    */
101
    public function getPhpCodeDefine($left, $right)
102
    {
103
        return "define('{$left}', {$right});\n";
104
    }
105
106
    /*
107
    *  @public function getPhpCodeDefine
108
    *  @param $left
109
    *  @param $right
110
    *
111
    *  @return string
112
    */
113
    public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access')
114
    {
115
        return "defined('{$left}') || die('{$right}');\n";
116
    }
117
118
    /*
119
    *  @public function getPhpCodeGlobalsVariables    
120
    *  @param $var
121
    *  @param $type
122
    *
123
    *  @return string
124
    */
125
    public function getPhpCodeGlobalsVariables($var = '', $type = 'REQUEST')
126
    {
127
        $type = strtoupper($type);
128
        switch ($type) {
129
            case 'GET':
130
                $ret = "\$_GET['{$var}']";
131
                break;
132
            case 'POST':
133
                $ret = "\$_POST['{$var}']";
134
                break;
135
            case 'FILES':
136
                $ret = "\$_FILES['{$var}']";
137
                break;
138
            case 'COOKIE':
139
                $ret = "\$_COOKIE['{$var}']";
140
                break;
141
            case 'ENV':
142
                $ret = "\$_ENV['{$var}']";
143
                break;
144
            case 'SERVER':
145
                $ret = "\$_SERVER['{$var}']";
146
                break;
147
            default:
148
                $ret = "\$_REQUEST['{$var}']";
149
                break;
150
        }
151
152
        return $ret;
153
    }
154
155
    /*
156
     * @public function getPhpCodeRemoveCarriageReturn
157
     * @param $string     
158
     *
159
     * @return string
160
     */
161
    public function getPhpCodeRemoveCarriageReturn($string)
162
    {
163
        return str_replace(array("\n", "\r"), '', $string);
164
    }
165
166
    /*
167
    *  @public function getPhpCodeFileExists
168
    *  @param $filename
169
    *
170
    *  @return string
171
    */
172
    public function getPhpCodeFileExists($filename)
173
    {
174
        return "file_exists({$filename})";
175
    }
176
177
    /*
178
    *  @public function getPhpCodeIncludeDir
179
    *  @param $directory
180
    *  @param $filename
181
    *  @param $once
182
    *  @param $isPath
183
    *
184
    *  @return string
185
    */
186
    public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false, $type = 'include')
187
    {
188
        if ($once == 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...
189
            if (!$isPath) {
190
                $ret = "{$type} {$directory} .'/{$filename}.php';\n";
191
            } else {
192
                $ret = "{$type} {$directory};\n";
193
            }
194
        } else {
195
            if (!$isPath) {
196
                $ret = "{$type}_once {$directory} .'/{$filename}.php';\n";
197
            } else {
198
                $ret = "{$type}_once {$directory};\n";
199
            }
200
        }
201
202
        return $ret;
203
    }
204
205
    /*
206
    *  @public function getPhpCodeTernaryOperator
207
    *  @param $return
208
    *  @param $condition
209
    *  @param $one
210
    *  @param $two
211
    *
212
    *  @return string
213
    */
214
    public function getPhpCodeTernaryOperator($return, $condition, $one, $two)
215
    {
216
        return "\${$return} = {$condition} ? {$one} : {$two};";
217
    }
218
219
    /*
220
    *  @public function getPhpCodeClass
221
    *  @param $name    
222
    *  @param $content
223
    *  @param $extends
224
    *  @param $type
225
    *
226
    *  @return string
227
    */
228
    public function getPhpCodeClass($name = '', $content = '', $extends = null, $type = null)
229
    {
230
        $typ = ($type != null) ? "{$type} " : '';
231
        $ext = ($extends != null) ? " extends {$extends}" : '';
232
        $ret = "{$typ}class {$name}{$ext} {";
233
        $ret .= "\t{$content}\n\t";
234
        $ret .= "}\n";
235
236
        return $ret;
237
    }
238
239
    /*
240
    *  @public function getPhpCodeClass
241
    *  @param $type    
242
    *  @param $name
243
    *  @param $assign
244
    *
245
    *  @return string
246
    */
247
    public function getPhpCodeVariableClass($type = 'private', $name = '', $assign = 'null')
248
    {
249
        return "{$type} \${$name} = {$assign}\n";
250
    }
251
252
    /*
253
    *  @public function getPhpCodeFunction
254
    *  @param $name
255
    *  @param $params
256
    *  @param $content
257
    *  @param $method
258
    *  @param $t - Indentation 
259
    *
260
    *  @return string
261
    */
262
    public function getPhpCodeFunction($name = '', $params = '', $content = '', $method = null, $t = '')
263
    {
264
        $inClass = ($method != null) ? $method : '';
265
        $ret = "{$t}{$inClass}function {$name}({$params})\n";
266
        $ret .= "{$t}{\n";
267
        $ret .= "{$t}\t{$content}\n\t{$t}";
268
        $ret .= "{$t}}\n";
269
270
        return $ret;
271
    }
272
273
    /*
274
     * @public function getPhpCodeConditions
275
     * @param string $condition
276
     * @param string $operator
277
     * @param string $type
278
     * @param string $contentIf
279
     * @param mixed  $contentElse
280
     * @param $t - Indentation 
281
     *
282
     * @return string
283
     */
284
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '')
285
    {
286
        if (false === $contentElse) {
287
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
288
            $ret .= "{$t}\t{$contentIf}";
289
            $ret .= "{$t}}\n";
290
        } else {
291
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
292
            $ret .= "{$t}\t{$contentIf}";
293
            $ret .= "{$t}} else {\n";
294
            $ret .= "{$t}\t{$contentElse}\n";
295
            $ret .= "{$t}}\n";
296
        }
297
298
        return $ret;
299
    }
300
301
    /*
302
     * @public function getPhpCodeForeach
303
     * @param string $array
304
     * @param string $arrayKey     
305
     * @param string $key
306
     * @param string $value     
307
     * @param string $content
308
     *
309
     * @return string
310
     */
311
    public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = '', $t = '')
312
    {
313
        $vars = '';
314
        if ((false === $arrayKey) && (false === $key)) {
315
            $vars = "\${$array} as \${$value}";
316
        } elseif ((false === $arrayKey) && (false !== $key)) {
317
            $vars = "\${$array} as \${$key} => \${$value}";
318
        } elseif ((false !== $arrayKey) && (false === $key)) {
319
            $vars = "array_keys(\${$array}) as \${$value}";
320
        }
321
322
        $ret = "{$t}foreach({$vars}) {\n";
323
        $ret .= "{$t}\t{$content}";
324
        $ret .= "{$t}}\n";
325
326
        return $ret;
327
    }
328
329
    /*
330
     * @public function getPhpCodeFor
331
     * @param $var
332
     * @param $content
333
     * @param $value
334
     * @param $initVal
335
     * @param $operator
336
     *
337
     * @return string
338
     */
339
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '')
340
    {
341
        $ret = "{$t}for(\${$var} = {$initVal}; {$var} {$operator} \${$value}; \${$var}++) {\n";
342
        $ret .= "{$t}\t{$content}\n\t{$t}";
343
        $ret .= "{$t}}\n";
344
345
        return $ret;
346
    }
347
348
    /*
349
     * @public function getPhpCodeWhile
350
     * @param $var
351
     * @param $content
352
     * @param $value
353
     * @param $operator
354
     *  @param $t
355
     *
356
     * @return string
357
     */
358
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '')
359
    {
360
        $ret = "{$t}while(\${$var} {$operator} {$value}) {\n";
361
        $ret .= "{$t}\t{$content}\n\t{$t}";
362
        $ret .= "{$t}}\n";
363
364
        return $ret;
365
    }
366
367
    /**
368
     *  @public function getPhpCodeSwitch
369
     *
370
     *  @param $op
371
     *  @param $content
372
     *  @param $t
373
     *
374
     *  @return string
375
     */
376
    public function getPhpCodeSwitch($op = '', $content = '', $t = '')
377
    {
378
        $ret = "{$t}switch(\${$op}) {\n";
379
        $ret .= "{$t}\t{$content}\n\t{$t}";
380
        $ret .= "{$t}}\n";
381
382
        return $ret;
383
    }
384
385
    /**
386
     *  @public function getPhpCodeCaseSwitch     
387
     *
388
     *  @param $cases
389
     *  @param $defaultAfterCase
390
     *  @param $default
391
     *  @param $t
392
     *
393
     *  @return string
394
     */
395
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t")
396
    {
397
        $ret = '';
398
        $def = "{$t}default:\n";
399
        foreach ($cases as $case => $value) {
400
            $case = is_string($case) ? "'{$case}'" : $case;
401
            if (!empty($case)) {
402
                $ret .= "{$t}case {$case}:\n";
403
                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...
404
                    $ret .= $def;
405
                }
406
                if (is_array($value)) {
407
                    foreach ($value as $content) {
408
                        $ret .= "{$t}{$t}{$content}\n";
409
                    }
410
                }
411
                $ret .= "{$t}break;\n";
412
            }
413
            $defaultAfterCase = false;
414
        }
415
        if ($default !== false) {
416
            $ret .= $def;
417
            $ret .= "{$t}{$default}\n";
418
            $ret .= "{$t}break;\n";
419
        }
420
421
        return $ret;
422
    }
423
424
    /*
425
    *  @public function getPhpCodeIsset
426
    *  @param $var
427
    *  @return string
428
    */
429
    public function getPhpCodeIsset($var)
430
    {
431
        return "isset(\${$var})";
432
    }
433
434
    /*
435
    *  @public function getPhpCodeUnset
436
    *  @param $var
437
    *  @return string
438
    */
439
    public function getPhpCodeUnset($var = '')
440
    {
441
        return "unset(\${$var});\n";
442
    }
443
444
    /*
445
    *  @public function getPhpCodeImplode
446
    *  @param $left
447
    *  @param $right
448
    *  @return string
449
    */
450
    public function getPhpCodeImplode($left, $right)
451
    {
452
        return "implode('{$left}', {$right})";
453
    }
454
455
    /*
456
    *  @public function getPhpCodeExplode
457
    *  @param $left
458
    *  @param $right
459
    *  @return string
460
    */
461
    public function getPhpCodeExplode($left, $right)
462
    {
463
        return "explode('{$left}', {$right})";
464
    }
465
466
    /*
467
    *  @public function getPhpCodeArray
468
    *  @param $var
469
    *  @param $left
470
    *  @param $right
471
    *  @param $key
472
    *  @param $isParam
473
    *
474
    *  @return string
475
    */
476
    public function getPhpCodeArray($var, $left = null, $right = null, $key = false, $isParam = false)
477
    {
478
        $leftIs = preg_match('/^[a-zA-Z0-9]+/', $left) ? "'{$left}'" : $left;
479
        $rightIs = preg_match('/^[a-zA-Z0-9]+/', $right) ? "'{$right}'" : $right;
480
        $arrayKey = ($key !== false) ? "{$leftIs} => {$rightIs}" : "{$leftIs}, {$rightIs}";
481
        $array = ($left !== null) ? (($right !== null) ? $arrayKey : $leftIs) : '';
482
        if (!$isParam) {
483
            $ret = "\${$var} = array({$array});\n";
484
        } else {
485
            $ret = "array({$array})";
486
        }
487
488
        return $ret;
489
    }
490
491
    /*
492
    *  @public function getPhpCodeArrayType
493
    *  @param $var
494
    *  @param $type
495
    *  @param $left
496
    *  @param $right
497
    *  @param $isParam
498
    *
499
    *  @return string
500
    */
501
    public function getPhpCodeArrayType($var, $type, $left, $right, $isParam = false)
502
    {
503
        if (!$isParam) {
504
            $ret = "\${$var}[] = array_{$type}(\${$left}, {$right});\n";
505
        } else {
506
            $ret = "array_{$type}(\${$left}, {$right})";
507
        }
508
509
        return $ret;
510
    }
511
512
    /*
513
    *  @public function getPhpCodeSprintf
514
    *  @param $left
515
    *  @param $right
516
    *  @return string
517
    */
518
    public function getPhpCodeSprintf($left, $right)
519
    {
520
        return "sprintf({$left}, {$right})";
521
    }
522
523
    /*
524
    *  @public function getPhpCodeEmpty
525
    *  @param $var
526
    *  @return string
527
    */
528
    public function getPhpCodeEmpty($var)
529
    {
530
        return "empty({$var})";
531
    }
532
533
    /*
534
    *  @public function getPhpCodeHeader
535
    *  @param $var
536
    *  @return string
537
    */
538
    public function getPhpCodeHeader($var)
539
    {
540
        return "header({$var})";
541
    }
542
543
    /*
544
    *  @public function getPhpCodeRawurlencode
545
    *  @param $var
546
    *  @return string
547
    */
548
    public function getPhpCodeRawurlencode($var)
549
    {
550
        return "rawurlencode({$var})";
551
    }
552
553
    /*
554
    *  @public function getPhpCodePregFunzions
555
    *  @param $return
556
    *  @param $exp
557
    *  @param $str
558
    *  @param $val
559
    *  @param $type
560
    *  @param $isParam
561
    *
562
    *  @return string
563
    */
564
    public function getPhpCodePregFunzions($return, $exp = '', $str, $val, $type = 'match', $isParam = false)
565
    {
566
        $pregFunz = "preg_{$type}( '";
567
        if (!$isParam) {
568
            $ret = "\${$return} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
569
        } else {
570
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
571
        }
572
573
        return $ret;
574
    }
575
576
    /*
577
    *  @public function getPhpCodeStrType
578
    *  @param $left
579
    *  @param $var
580
    *  @param $str
581
    *  @param $value
582
    *  @param $type
583
    *  @param $isParam
584
    *
585
    *  @return string
586
    */
587
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false)
588
    {
589
        $strType = "str_{$type}( '";
590
        if (!$isParam) {
591
            $ret = "\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
592
        } else {
593
            $ret = "{$strType}{$var}', '{$str}', {$value})";
594
        }
595
596
        return $ret;
597
    }
598
599
    /*
600
    *  @public function getPhpCodeStripTags
601
    *  @param $left
602
    *  @param $value
603
    *  @param $isParam
604
    *
605
    *  @return string
606
    */
607
    public function getPhpCodeStripTags($left = '', $value, $isParam = false)
608
    {
609
        if (!$isParam) {
610
            $ret = "\${$left} = strip_tags({$value});\n";
611
        } else {
612
            $ret = "strip_tags({$value})";
613
        }
614
615
        return $ret;
616
    }
617
618
    /*
619
    *  @public function getPhpCodeHtmlentities
620
    *  @param $entitiesVar
621
    *  @param $entitiesQuote
622
    *  @return string
623
    */
624
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
625
    {
626
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
627
        $entities = "htmlentities({$entitiesVar})";
628
629
        return $entities;
630
    }
631
632
    /*
633
    *  @public function getPhpCodeHtmlspecialchars
634
    *  @param $specialVar
635
    *  @param $specialQuote
636
    *  @return string
637
    */
638
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
639
    {
640
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
641
        $specialchars = "htmlspecialchars({$specialVar})";
642
643
        return $specialchars;
644
    }
645
}
646