Completed
Push — master ( a8ea98...cecb98 )
by Gino
06:23 queued 03:11
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 getPhpCodeImplode
468
    *  @param $left
469
    *  @param $right
470
    *  @return string
471
    */
472
    public function getPhpCodeImplode($left, $right)
473
    {
474
        return "implode('{$left}', {$right})";
475
    }
476
477
    /*
478
    *  @public function getPhpCodeExplode
479
    *  @param $left
480
    *  @param $right
481
    *  @return string
482
    */
483
    public function getPhpCodeExplode($left, $right)
484
    {
485
        return "explode('{$left}', {$right})";
486
    }
487
488
    /*
489
    *  @public function getPhpCodeArray
490
    *  @param $var
491
    *  @param $array
492
    *  @param $isParam
493
    *
494
    *  @return string
495
    */
496
    public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t")
497
    {
498
        $retArray = array();
499
        if (is_array($array) && !empty($array)) {
500
            foreach ($array as $k => $v) {
501
                if (is_numeric($k)) {
502
                    $retArray[] = $v;
503
                } else {
504
                    $retArray[] = "{$k} => {$v}";
505
                }
506
            }
507
            $arrayContent = implode(', ', $retArray);
508
        } else {
509
            $arrayContent = '';
510
        }
511
        unset($retArray);
512
513
        if (!$isParam) {
514
            $ret = "{$t}\${$var} = array({$arrayContent});\n";
515
        } else {
516
            $ret = "array({$array})";
517
        }
518
519
        return $ret;
520
    }
521
522
    /*
523
    *  @public function getPhpCodeArrayType
524
    *  @param $var
525
    *  @param $type
526
    *  @param $left
527
    *  @param $right
528
    *  @param $isParam
529
    *
530
    *  @return string
531
    */
532
    public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t")
533
    {
534
        $vars = ($right != null) ? "\${$left}, {$right}" : "\${$left}";
535
        if (!$isParam) {
536
            $ret = "{$t}\${$var}[] = array_{$type}({$vars});\n";
537
        } else {
538
            $ret = "array_{$type}({$vars})";
539
        }
540
541
        return $ret;
542
    }
543
544
    /*
545
    *  @public function getPhpCodeSprintf
546
    *  @param $left
547
    *  @param $right
548
    *  @return string
549
    */
550
    public function getPhpCodeSprintf($left, $right)
551
    {
552
        return "sprintf({$left}, {$right})";
553
    }
554
555
    /*
556
    *  @public function getPhpCodeEmpty
557
    *  @param $var
558
    *  @return string
559
    */
560
    public function getPhpCodeEmpty($var)
561
    {
562
        return "empty({$var})";
563
    }
564
565
    /*
566
    *  @public function getPhpCodeHeader
567
    *  @param $var
568
    *  @return string
569
    */
570
    public function getPhpCodeHeader($var)
571
    {
572
        return "header({$var})";
573
    }
574
575
    /*
576
    *  @public function getPhpCodeRawurlencode
577
    *  @param $var
578
    *  @return string
579
    */
580
    public function getPhpCodeRawurlencode($var)
581
    {
582
        return "rawurlencode({$var})";
583
    }
584
585
    /*
586
    *  @public function getPhpCodePregFunzions
587
    *  @param $return
588
    *  @param $exp
589
    *  @param $str
590
    *  @param $val
591
    *  @param $type
592
    *  @param $isParam
593
    *
594
    *  @return string
595
    */
596
    public function getPhpCodePregFunzions($return, $exp = null, $str, $val, $type = 'match', $isParam = false, $t = "\t")
597
    {
598
        $pregFunz = "preg_{$type}( '";
599
        if (!$isParam) {
600
            $ret = "{$t}\${$return} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
601
        } else {
602
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
603
        }
604
605
        return $ret;
606
    }
607
608
    /*
609
    *  @public function getPhpCodeStrType
610
    *  @param $left
611
    *  @param $var
612
    *  @param $str
613
    *  @param $value
614
    *  @param $type
615
    *  @param $isParam
616
    *
617
    *  @return string
618
    */
619
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t")
620
    {
621
        $strType = "str_{$type}( '";
622
        if (!$isParam) {
623
            $ret = "{$t}\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
624
        } else {
625
            $ret = "{$strType}{$var}', '{$str}', {$value})";
626
        }
627
628
        return $ret;
629
    }
630
631
    /*
632
    *  @public function getPhpCodeStripTags
633
    *  @param $left
634
    *  @param $value
635
    *  @param $isParam
636
    *
637
    *  @return string
638
    */
639
    public function getPhpCodeStripTags($left = null, $value, $isParam = false, $t = "\t")
640
    {
641
        if (!$isParam) {
642
            $ret = "{$t}\${$left} = strip_tags({$value});\n";
643
        } else {
644
            $ret = "strip_tags({$value})";
645
        }
646
647
        return $ret;
648
    }
649
650
    /*
651
    *  @public function getPhpCodeHtmlentities
652
    *  @param $entitiesVar
653
    *  @param $entitiesQuote
654
    *  @return string
655
    */
656
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
657
    {
658
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
659
        $entities = "htmlentities({$entitiesVar})";
660
661
        return $entities;
662
    }
663
664
    /*
665
    *  @public function getPhpCodeHtmlspecialchars
666
    *  @param $specialVar
667
    *  @param $specialQuote
668
    *  @return string
669
    */
670
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
671
    {
672
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
673
        $specialchars = "htmlspecialchars({$specialVar})";
674
675
        return $specialchars;
676
    }
677
}
678