Completed
Push — master ( 532db1...f541a6 )
by Gino
06:07 queued 03:01
created

TDMCreatePhpCode::getPhpCodeArray()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.439
cc 6
eloc 17
nc 4
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 = null, $var = null, $t = '')
65
    {
66
        $value = !empty($var) ? ' '.$var : '';
67
        $ret = "{$t}// {$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 = "\n{$t}/**\n";
82
        foreach ($values as $string => $value) {
83
            if ($string === '' && $value === '') {
84
                $ret .= "{$t} *\n";
85
            } else {
86
                $ret .= "{$t} * {$string} {$value}\n";
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 getPhpCodeGlobals
120
    *  @param $var
121
    *  @param $value
122
    *
123
    *  @return string
124
    */
125
    public function getPhpCodeGlobals($var, $value = '')
126
    {
127
        if ($value != '') {
128
            $ret = "\$GLOBALS['{$var}'] = \${$value};\n";
129
        } else {
130
            $ret = "\$GLOBALS['{$var}']";
131
        }
132
133
        return $ret;
134
    }
135
136
    /*
137
    *  @public function getPhpCodeGlobalsVariables    
138
    *  @param $var
139
    *  @param $type
140
    *
141
    *  @return string
142
    */
143
    public function getPhpCodeGlobalsVariables($var = null, $type = 'REQUEST')
144
    {
145
        $type = strtoupper($type);
146
        switch ($type) {
147
            case 'GET':
148
                $ret = "\$_GET['{$var}']";
149
                break;
150
            case 'POST':
151
                $ret = "\$_POST['{$var}']";
152
                break;
153
            case 'FILES':
154
                $ret = "\$_FILES['{$var}']";
155
                break;
156
            case 'COOKIE':
157
                $ret = "\$_COOKIE['{$var}']";
158
                break;
159
            case 'ENV':
160
                $ret = "\$_ENV['{$var}']";
161
                break;
162
            case 'SERVER':
163
                $ret = "\$_SERVER['{$var}']";
164
                break;
165
            default:
166
                $ret = "\$_REQUEST['{$var}']";
167
                break;
168
        }
169
170
        return $ret;
171
    }
172
173
    /*
174
     * @public function getPhpCodeRemoveCarriageReturn
175
     * @param $string     
176
     *
177
     * @return string
178
     */
179
    public function getPhpCodeRemoveCarriageReturn($string, $n = "\n", $t = "\r")
180
    {
181
        return str_replace(array("{$n}", "{$t}"), '', $string);
182
    }
183
184
    /*
185
    *  @public function getPhpCodeFileExists
186
    *  @param $filename
187
    *
188
    *  @return string
189
    */
190
    public function getPhpCodeFileExists($filename)
191
    {
192
        return "file_exists({$filename})";
193
    }
194
195
    /*
196
    *  @public function getPhpCodeIncludeDir
197
    *  @param $directory
198
    *  @param $filename
199
    *  @param $once
200
    *  @param $isPath
201
    *
202
    *  @return string
203
    */
204
    public function getPhpCodeIncludeDir($directory = null, $filename = null, $once = false, $isPath = false, $type = 'include', $t = '')
205
    {
206
        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...
207
            if (!$isPath) {
208
                $ret = "{$t}{$type} {$directory} .'/{$filename}.php';\n";
209
            } else {
210
                $ret = "{$t}{$type} {$directory};\n";
211
            }
212
        } else {
213
            if (!$isPath) {
214
                $ret = "{$t}{$type}_once {$directory} .'/{$filename}.php';\n";
215
            } else {
216
                $ret = "{$t}{$type}_once {$directory};\n";
217
            }
218
        }
219
220
        return $ret;
221
    }
222
223
    /*
224
    *  @public function getPhpCodeTernaryOperator
225
    *  @param $return
226
    *  @param $condition
227
    *  @param $one
228
    *  @param $two
229
    *  @param $t - Indentation 
230
    *
231
    *  @return string
232
    */
233
    public function getPhpCodeTernaryOperator($return, $condition, $one, $two, $t = '')
234
    {
235
        return "{$t}\${$return} = {$condition} ? {$one} : {$two};\n";
236
    }
237
238
    /*
239
    *  @public function getPhpCodeClass
240
    *  @param $name    
241
    *  @param $content
242
    *  @param $extends
243
    *  @param $type
244
    *
245
    *  @return string
246
    */
247
    public function getPhpCodeClass($name = null, $content = null, $extends = null, $type = null)
248
    {
249
        $typ = ($type != null) ? "{$type} " : '';
250
        $ext = ($extends != null) ? " extends {$extends}" : '';
251
        $ret = "{$typ}class {$name}{$ext}\n";
252
        $ret .= '{';
253
        $ret .= "{$content}";
254
        $ret .= "}\n";
255
256
        return $ret;
257
    }
258
259
    /*
260
    *  @public function getPhpCodeClass
261
    *  @param $type    
262
    *  @param $name
263
    *  @param $assign
264
    *  @param $t - Indentation 
265
    *
266
    *  @return string
267
    */
268
    public function getPhpCodeVariableClass($type = 'private', $name = null, $assign = 'null', $t = '')
269
    {
270
        return "{$t}{$type} \${$name} = {$assign};\n";
271
    }
272
273
    /*
274
    *  @public function getPhpCodeFunction
275
    *  @param $name
276
    *  @param $params
277
    *  @param $content
278
    *  @param $method
279
    *  @param $t - Indentation 
280
    *
281
    *  @return string
282
    */
283
    public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '')
284
    {
285
        $inClass = ($method != null) ? $method : '';
286
        $ref = ($isRef != 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...
287
        $ret = "{$t}{$inClass}function {$ref}{$name}({$params})\n";
288
        $ret .= "{$t}{\n";
289
        $ret .= "{$content}";
290
        $ret .= "{$t}}\n";
291
292
        return $ret;
293
    }
294
295
    /*
296
     * @public function getPhpCodeConditions
297
     * @param string $condition
298
     * @param string $operator
299
     * @param string $type
300
     * @param string $contentIf
301
     * @param mixed  $contentElse
302
     * @param $t - Indentation 
303
     *
304
     * @return string
305
     */
306
    public function getPhpCodeConditions($condition = null, $operator = null, $type = null, $contentIf = null, $contentElse = false, $t = '')
307
    {
308
        if (false === $contentElse) {
309
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
310
            $ret .= "{$contentIf}";
311
            $ret .= "{$t}}\n";
312
        } else {
313
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
314
            $ret .= "{$contentIf}";
315
            $ret .= "{$t}} else {\n";
316
            $ret .= "{$contentElse}";
317
            $ret .= "{$t}}\n";
318
        }
319
320
        return $ret;
321
    }
322
323
    /*
324
     * @public function getPhpCodeForeach
325
     * @param string $array
326
     * @param string $arrayKey     
327
     * @param string $key
328
     * @param string $value     
329
     * @param string $content
330
     *
331
     * @return string
332
     */
333
    public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '')
334
    {
335
        $vars = '';
336
        if ((false === $arrayKey) && (false === $key)) {
337
            $vars = "\${$array} as \${$value}";
338
        } elseif ((false === $arrayKey) && (false !== $key)) {
339
            $vars = "\${$array} as \${$key} => \${$value}";
340
        } elseif ((false !== $arrayKey) && (false === $key)) {
341
            $vars = "array_keys(\${$array}) as \${$value}";
342
        }
343
344
        $ret = "{$t}foreach({$vars}) {\n";
345
        $ret .= "{$t}{$content}";
346
        $ret .= "{$t}}\n";
347
348
        return $ret;
349
    }
350
351
    /*
352
     * @public function getPhpCodeFor
353
     * @param $var
354
     * @param $content
355
     * @param $value
356
     * @param $initVal
357
     * @param $operator
358
     *
359
     * @return string
360
     */
361
    public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '')
362
    {
363
        $ret = "{$t}for(\${$var} = {$initVal}; {$var} {$operator} \${$value}; \${$var}++) {\n";
364
        $ret .= "{$t}{$content}";
365
        $ret .= "{$t}}\n";
366
367
        return $ret;
368
    }
369
370
    /*
371
     * @public function getPhpCodeWhile
372
     * @param $var
373
     * @param $content
374
     * @param $value
375
     * @param $operator
376
     *  @param $t
377
     *
378
     * @return string
379
     */
380
    public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '')
381
    {
382
        $ret = "{$t}while(\${$var} {$operator} {$value}) {\n";
383
        $ret .= "{$t}{$content}";
384
        $ret .= "{$t}}\n";
385
386
        return $ret;
387
    }
388
389
    /**
390
     *  @public function getPhpCodeSwitch
391
     *
392
     *  @param $op
393
     *  @param $content
394
     *  @param $t
395
     *
396
     *  @return string
397
     */
398
    public function getPhpCodeSwitch($op = null, $content = null, $t = '')
399
    {
400
        $ret = "{$t}switch(\${$op}) {\n";
401
        $ret .= "{$t}{$content}";
402
        $ret .= "{$t}}\n";
403
404
        return $ret;
405
    }
406
407
    /**
408
     *  @public function getPhpCodeCaseSwitch     
409
     *
410
     *  @param $cases
411
     *  @param $defaultAfterCase
412
     *  @param $default
413
     *  @param $t
414
     *
415
     *  @return string
416
     */
417
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = '')
418
    {
419
        $ret = '';
420
        $def = "{$t}default:\n";
421
        foreach ($cases as $case => $value) {
422
            $case = is_string($case) ? "'{$case}'" : $case;
423
            if (!empty($case)) {
424
                $ret .= "{$t}case {$case}:\n";
425
                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...
426
                    $ret .= $def;
427
                }
428
                if (is_array($value)) {
429
                    foreach ($value as $content) {
430
                        $ret .= "{$t}{$t}{$content}\n";
431
                    }
432
                }
433
                $ret .= "{$t}break;\n";
434
            }
435
            $defaultAfterCase = false;
436
        }
437
        if ($default !== false) {
438
            $ret .= $def;
439
            $ret .= "{$t}{$default}\n";
440
            $ret .= "{$t}break;\n";
441
        }
442
443
        return $ret;
444
    }
445
446
    /*
447
    *  @public function getPhpCodeIsset
448
    *  @param $var
449
    *  @return string
450
    */
451
    public function getPhpCodeIsset($var)
452
    {
453
        return "isset(\${$var})";
454
    }
455
456
    /*
457
    *  @public function getPhpCodeUnset
458
    *  @param $var
459
    *  @return string
460
    */
461
    public function getPhpCodeUnset($var = '', $t = '')
462
    {
463
        return "{$t}unset(\${$var});\n";
464
    }
465
466
    /*
467
    *  @public function getPhpCodeIsDir
468
    *  @param $var
469
    *  @return string
470
    */
471
    public function getPhpCodeIsDir($var)
472
    {
473
        return "is_dir({$var})";
474
    }
475
476
    /*
477
    *  @public function getPhpCodeImplode
478
    *  @param $left
479
    *  @param $right
480
    *  @return string
481
    */
482
    public function getPhpCodeImplode($left, $right)
483
    {
484
        return "implode('{$left}', {$right})";
485
    }
486
487
    /*
488
    *  @public function getPhpCodeExplode
489
    *  @param $left
490
    *  @param $right
491
    *  @return string
492
    */
493
    public function getPhpCodeExplode($left, $right)
494
    {
495
        return "explode('{$left}', {$right})";
496
    }
497
498
    /*
499
    *  @public function getPhpCodeChmod
500
    *  @param $var
501
    *  @param $perm
502
    *  @return string
503
    */
504
    public function getPhpCodeChmod($var, $perm = '0777', $t = '')
505
    {
506
        return "{$t}chmod(\${$var}, {$perm});\n";
507
    }
508
509
    /*
510
    *  @public function getPhpCodeMkdir
511
    *  @param $var
512
    *  @param $perm
513
    *  @return string
514
    */
515
    public function getPhpCodeMkdir($var, $perm = '0777', $t = '')
516
    {
517
        return "{$t}mkdir(\${$var}, {$perm});\n";
518
    }
519
520
    /*
521
    *  @public function getPhpCodeCopy
522
    *  @param $file
523
    *  @param $newfile
524
    *  @return string
525
    */
526
    public function getPhpCodeCopy($file, $newfile = '', $t = '')
527
    {
528
        return "{$t}copy({$file}, {$newfile});\n";
529
    }
530
531
    /*
532
    *  @public function getPhpCodeArray
533
    *  @param $var
534
    *  @param $array
535
    *  @param $isParam
536
    *
537
    *  @return string
538
    */
539
    public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t")
540
    {
541
        $retArray = array();
542
        if (is_array($array) && !empty($array)) {
543
            foreach ($array as $k => $v) {
544
                if (is_numeric($k)) {
545
                    $retArray[] = $v;
546
                } else {
547
                    $retArray[] = "{$k} => {$v}";
548
                }
549
            }
550
            $arrayContent = implode(', ', $retArray);
551
        } else {
552
            $arrayContent = '';
553
        }
554
        unset($retArray);
555
556
        if (!$isParam) {
557
            $ret = "{$t}\${$var} = array({$arrayContent});\n";
558
        } else {
559
            $ret = "array({$array})";
560
        }
561
562
        return $ret;
563
    }
564
565
    /*
566
    *  @public function getPhpCodeArrayType
567
    *  @param $var
568
    *  @param $type
569
    *  @param $left
570
    *  @param $right
571
    *  @param $isParam
572
    *
573
    *  @return string
574
    */
575
    public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t")
576
    {
577
        $vars = ($right != null) ? "\${$left}, {$right}" : "\${$left}";
578
        if (!$isParam) {
579
            $ret = "{$t}\${$var}[] = array_{$type}({$vars});\n";
580
        } else {
581
            $ret = "array_{$type}({$vars})";
582
        }
583
584
        return $ret;
585
    }
586
587
    /*
588
    *  @public function getPhpCodeSprintf
589
    *  @param $left
590
    *  @param $right
591
    *  @return string
592
    */
593
    public function getPhpCodeSprintf($left, $right)
594
    {
595
        return "sprintf({$left}, {$right})";
596
    }
597
598
    /*
599
    *  @public function getPhpCodeEmpty
600
    *  @param $var
601
    *  @return string
602
    */
603
    public function getPhpCodeEmpty($var)
604
    {
605
        return "empty({$var})";
606
    }
607
608
    /*
609
    *  @public function getPhpCodeHeader
610
    *  @param $var
611
    *  @return string
612
    */
613
    public function getPhpCodeHeader($var)
614
    {
615
        return "header({$var})";
616
    }
617
618
    /*
619
    *  @public function getPhpCodeRawurlencode
620
    *  @param $var
621
    *  @return string
622
    */
623
    public function getPhpCodeRawurlencode($var)
624
    {
625
        return "rawurlencode({$var})";
626
    }
627
628
    /*
629
    *  @public function getPhpCodePregFunzions
630
    *  @param $return
631
    *  @param $exp
632
    *  @param $str
633
    *  @param $val
634
    *  @param $type
635
    *  @param $isParam
636
    *
637
    *  @return string
638
    */
639
    public function getPhpCodePregFunzions($return, $exp = null, $str, $val, $type = 'match', $isParam = false, $t = "\t")
640
    {
641
        $pregFunz = "preg_{$type}( '";
642
        if (!$isParam) {
643
            $ret = "{$t}\${$return} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
644
        } else {
645
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
646
        }
647
648
        return $ret;
649
    }
650
651
    /*
652
    *  @public function getPhpCodeStrType
653
    *  @param $left
654
    *  @param $var
655
    *  @param $str
656
    *  @param $value
657
    *  @param $type
658
    *  @param $isParam
659
    *
660
    *  @return string
661
    */
662
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t")
663
    {
664
        $strType = "str_{$type}( '";
665
        if (!$isParam) {
666
            $ret = "{$t}\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
667
        } else {
668
            $ret = "{$strType}{$var}', '{$str}', {$value})";
669
        }
670
671
        return $ret;
672
    }
673
674
    /*
675
    *  @public function getPhpCodeStripTags
676
    *  @param $left
677
    *  @param $value
678
    *  @param $isParam
679
    *
680
    *  @return string
681
    */
682
    public function getPhpCodeStripTags($left = null, $value, $isParam = false, $t = '')
683
    {
684
        if (!$isParam) {
685
            $ret = "{$t}\${$left} = strip_tags({$value});\n";
686
        } else {
687
            $ret = "strip_tags({$value})";
688
        }
689
690
        return $ret;
691
    }
692
693
    /*
694
    *  @public function getPhpCodeHtmlentities
695
    *  @param $entitiesVar
696
    *  @param $entitiesQuote
697
    *  @return string
698
    */
699
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
700
    {
701
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
702
        $entities = "htmlentities({$entitiesVar})";
703
704
        return $entities;
705
    }
706
707
    /*
708
    *  @public function getPhpCodeHtmlspecialchars
709
    *  @param $specialVar
710
    *  @param $specialQuote
711
    *  @return string
712
    */
713
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
714
    {
715
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
716
        $specialchars = "htmlspecialchars({$specialVar})";
717
718
        return $specialchars;
719
    }
720
}
721