Passed
Pull Request — master (#144)
by Michael
03:10
created

CreatePhpCode::getPhpCodeDefined()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files;
4
5
use XoopsModules\Tdmcreate;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * tdmcreate module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops http://www.txmodxoops.org
25
 *
26
 * @version         $Id: reatePhpCode.php 12258 2014-01-02 09:33:29Z timgno $
27
 */
28
29
/**
30
 * Class CreatePhpCode.
31
 */
32
class CreatePhpCode
33
{
34
    /**
35
     *  @static function getInstance
36
     *  @param null
37
     * @return CreatePhpCode
38
     */
39
    public static function getInstance()
40
    {
41
        static $instance = false;
42
        if (!$instance) {
43
            $instance = new self();
44
        }
45
46
        return $instance;
47
    }
48
49
    /**
50
     * @public function getPhpCodeCommentLine
51
     * @param        $comment
52
     * @param        $var
53
     * @param string $t
54
     * @return string
55
     */
56
    public function getPhpCodeCommentLine($comment = null, $var = null, $t = '')
57
    {
58
        $value = !empty($var) ? ' ' . $var : '';
59
        $ret = "{$t}// {$comment}{$value}\n";
60
61
        return $ret;
62
    }
63
64
    /**
65
     * @public function getPhpCodeCommentMultiLine
66
     * @param array  $multiLine
67
     *
68
     * @param string $t
69
     * @return string
70
     */
71
    public function getPhpCodeCommentMultiLine($multiLine = [], $t = '')
72
    {
73
        $values = !empty($multiLine) ? $multiLine : [];
74
        $ret = "\n{$t}/**\n";
75
        foreach ($values as $string => $value) {
76
            if ('' === $string && '' === $value) {
77
                $ret .= "{$t} *\n";
78
            } else {
79
                $ret .= "{$t} * {$string} {$value}\n";
80
            }
81
        }
82
        $ret .= "{$t} */\n";
83
84
        return $ret;
85
    }
86
87
    /**
88
     *  @public function getPhpCodeDefine
89
     *  @param $left
90
     *  @param $right
91
     *
92
     *  @return string
93
     */
94
    public function getPhpCodeDefine($left, $right)
95
    {
96
        return "define('{$left}', {$right});\n";
97
    }
98
99
    /**
100
     *  @public function getPhpCodeDefine
101
     *  @param $left
102
     *  @param $right
103
     *
104
     *  @return string
105
     */
106
    public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access')
107
    {
108
        return "defined('{$left}') || die('{$right}');\n";
109
    }
110
111
    /**
112
     *  @public function getPhpCodeGlobals
113
     *  @param $var
114
     *  @param $value
115
     *
116
     *  @return string
117
     */
118
    public function getPhpCodeGlobals($var, $value = '')
119
    {
120
        if ('' != $value) {
121
            $ret = "\$GLOBALS['{$var}'] = \${$value};\n";
122
        } else {
123
            $ret = "\$GLOBALS['{$var}']";
124
        }
125
126
        return $ret;
127
    }
128
129
    /**
130
     *  @public function getPhpCodeGlobalsVariables
131
     *  @param $var
132
     *  @param $type
133
     *
134
     *  @return string
135
     */
136
    public function getPhpCodeGlobalsVariables($var = null, $type = 'REQUEST')
137
    {
138
        $type = mb_strtoupper($type);
139
        switch ($type) {
140
            case 'GET':
141
                $ret = "\$_GET['{$var}']";
142
                break;
143
            case 'POST':
144
                $ret = "\$_POST['{$var}']";
145
                break;
146
            case 'FILES':
147
                $ret = "\$_FILES['{$var}']";
148
                break;
149
            case 'COOKIE':
150
                $ret = "\$_COOKIE['{$var}']";
151
                break;
152
            case 'ENV':
153
                $ret = "\$_ENV['{$var}']";
154
                break;
155
            case 'SERVER':
156
                $ret = "\$_SERVER['{$var}']";
157
                break;
158
            default:
159
                $ret = "\$_REQUEST['{$var}']";
160
                break;
161
        }
162
163
        return $ret;
164
    }
165
166
    /**
167
     * @public function getPhpCodeRemoveCarriageReturn
168
     * @param        $string
169
     *
170
     * @param string $n
171
     * @param string $t
172
     * @return string
173
     */
174
    public function getPhpCodeRemoveCarriageReturn($string, $n = "\n", $t = "\r")
175
    {
176
        return str_replace([(string)$n, (string)$t], '', $string);
177
    }
178
179
    /**
180
     *  @public function getPhpCodeFileExists
181
     *  @param $filename
182
     *
183
     *  @return string
184
     */
185
    public function getPhpCodeFileExists($filename)
186
    {
187
        return "file_exists({$filename})";
188
    }
189
190
    /**
191
     * @public function getPhpCodeIncludeDir
192
     * @param        $directory
193
     * @param        $filename
194
     * @param bool   $once
195
     * @param bool   $isPath
196
     *
197
     * @param string $type
198
     * @param string $t
199
     * @return string
200
     */
201
    public function getPhpCodeIncludeDir($directory = null, $filename = null, $once = false, $isPath = false, $type = 'include', $t = '')
202
    {
203
        if ('' === $type) {
204
            $type = 'include';
205
        }
206
        if (false === $once) {
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
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 = (null != $type) ? "{$type} " : '';
250
        $ext = (null != $extends) ? " 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
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
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 bool   $isRef
280
     * @param string $t - Indentation
281
     * @return string
282
     */
283
    public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '')
284
    {
285
        $inClass = (null != $method) ? $method : '';
286
        $ref = (false !== $isRef) ? '&' : '';
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
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 bool|string $arrayKey
327
     * @param bool|string $key
328
     * @param bool|string $value
329
     * @param string      $content
330
     *
331
     * @param string      $t
332
     * @return string
333
     */
334
    public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '')
335
    {
336
        $vars = '';
337
        $value = '' !== $value ? $value : 'i';
338
        if ((false === $arrayKey) && (false === $key)) {
339
            $vars = "\${$array} as \${$value}";
340
        } elseif ((false === $arrayKey) && (false !== $key)) {
341
            $vars = "\${$array} as \${$key} => \${$value}";
342
        } elseif ((false !== $arrayKey) && (false === $key)) {
343
            $vars = "array_keys(\${$array}) as \${$value}";
344
        }
345
346
        $ret = "{$t}foreach({$vars}) {\n";
347
        $ret .= "{$t}{$content}";
348
        $ret .= "{$t}}\n";
349
350
        return $ret;
351
    }
352
353
    /**
354
     * @public function getPhpCodeFor
355
     * @param        $var
356
     * @param        $content
357
     * @param        $value
358
     * @param        $initVal
359
     * @param        $operator
360
     *
361
     * @param string $t
362
     * @return string
363
     */
364
    public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '')
365
    {
366
        $ret = "{$t}for(\${$var} = {$initVal}; {$var} {$operator} \${$value}; \${$var}++) {\n";
367
        $ret .= "{$t}{$content}";
368
        $ret .= "{$t}}\n";
369
370
        return $ret;
371
    }
372
373
    /**
374
     * @public function getPhpCodeWhile
375
     * @param $var
376
     * @param $content
377
     * @param $value
378
     * @param $operator
379
     *  @param $t
380
     *
381
     * @return string
382
     */
383
    public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '')
384
    {
385
        $ret = "{$t}while(\${$var} {$operator} {$value}) {\n";
386
        $ret .= "{$t}{$content}";
387
        $ret .= "{$t}}\n";
388
389
        return $ret;
390
    }
391
392
    /**
393
     *  @public function getPhpCodeSwitch
394
     *
395
     *  @param $op
396
     *  @param $content
397
     *  @param $t
398
     *
399
     *  @return string
400
     */
401
    public function getPhpCodeSwitch($op = null, $content = null, $t = '')
402
    {
403
        //$ret = "{$t}switch(\${$op}) {\n"; test goffy
404
        $ret = "switch(\${$op}) {\n";
405
        $ret .= $content;
406
        $ret .= "}\n";
407
408
        return $ret;
409
    }
410
411
    /**
412
     *  @public function getPhpCodeCaseSwitch
413
     *
414
     *  @param $cases
415
     *  @param $defaultAfterCase
416
     *  @param $default
417
     *  @param $t
418
     *
419
     *  @return string
420
     */
421
    public function getPhpCodeCaseSwitch($cases = [], $defaultAfterCase = false, $default = false, $t = '')
422
    {
423
        $ret = '';
424
        $def = "{$t}default:\n";
425
        foreach ($cases as $case => $value) {
426
            $case = is_string($case) ? "'{$case}'" : $case;
427
            if (!empty($case)) {
428
                $ret .= "{$t}case {$case}:\n";
429
                if (false !== $defaultAfterCase) {
430
                    $ret .= $def;
431
                }
432
                if (is_array($value)) {
433
                    foreach ($value as $content) {
434
                        $ret .= "{$t}{$t}{$content}\n";
435
                    }
436
                }
437
                $ret .= "{$t}break;\n";
438
            }
439
            $defaultAfterCase = false;
440
        }
441
        if (false !== $default) {
442
            $ret .= $def;
443
            $ret .= "{$t}{$default}\n";
444
            $ret .= "{$t}break;\n";
445
        }
446
447
        return $ret;
448
    }
449
450
    /**
451
     *  @public function getPhpCodeIsset
452
     *  @param $var
453
     *  @return string
454
     */
455
    public function getPhpCodeIsset($var)
456
    {
457
        return "isset(\${$var})";
458
    }
459
460
    /**
461
     * @public function getPhpCodeUnset
462
     * @param string $var
463
     * @param string $t
464
     * @return string
465
     */
466
    public function getPhpCodeUnset($var = '', $t = '')
467
    {
468
        return "{$t}unset(\${$var});\n";
469
    }
470
471
    /**
472
     *  @public function getPhpCodeIsDir
473
     *  @param $var
474
     *  @return string
475
     */
476
    public function getPhpCodeIsDir($var)
477
    {
478
        return "is_dir({$var})";
479
    }
480
481
    /**
482
     *  @public function getPhpCodeImplode
483
     *  @param $left
484
     *  @param $right
485
     *  @return string
486
     */
487
    public function getPhpCodeImplode($left, $right)
488
    {
489
        return "implode('{$left}', {$right})";
490
    }
491
492
    /**
493
     *  @public function getPhpCodeExplode
494
     *  @param $left
495
     *  @param $right
496
     *  @return string
497
     */
498
    public function getPhpCodeExplode($left, $right)
499
    {
500
        return "explode('{$left}', {$right})";
501
    }
502
503
    /**
504
     * @public function getPhpCodeChmod
505
     * @param        $var
506
     * @param string $perm
507
     * @param string $t
508
     * @return string
509
     */
510
    public function getPhpCodeChmod($var, $perm = '0777', $t = '')
511
    {
512
        return "{$t}chmod(\${$var}, {$perm});\n";
513
    }
514
515
    /**
516
     * @public function getPhpCodeMkdir
517
     * @param        $var
518
     * @param string $perm
519
     * @param string $t
520
     * @return string
521
     */
522
    public function getPhpCodeMkdir($var, $perm = '0777', $t = '')
523
    {
524
        return "{$t}mkdir(\${$var}, {$perm});\n";
525
    }
526
527
    /**
528
     * @public function getPhpCodeCopy
529
     * @param        $file
530
     * @param string $newfile
531
     * @param string $t
532
     * @return string
533
     */
534
    public function getPhpCodeCopy($file, $newfile = '', $t = '')
535
    {
536
        return "{$t}copy({$file}, {$newfile});\n";
537
    }
538
539
    /**
540
     * @public function getPhpCodeArray
541
     * @param        $var
542
     * @param        $array
543
     * @param bool   $isParam
544
     *
545
     * @param string $t
546
     * @return string
547
     */
548
    public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t")
549
    {
550
        $retArray = [];
551
        if (is_array($array) && !empty($array)) {
552
            foreach ($array as $k => $v) {
553
                if (is_numeric($k)) {
554
                    $retArray[] = $v;
555
                } else {
556
                    $retArray[] = "{$k} => {$v}";
557
                }
558
            }
559
            $arrayContent = implode(', ', $retArray);
560
        } else {
561
            $arrayContent = '';
562
        }
563
        unset($retArray);
564
565
        if (!$isParam) {
566
            $ret = "{$t}\${$var} = array({$arrayContent});\n";
567
        } else {
568
            $ret = "array({$array})";
569
        }
570
571
        return $ret;
572
    }
573
574
    /**
575
     * @public function getPhpCodeArrayType
576
     * @param        $var
577
     * @param        $type
578
     * @param        $left
579
     * @param        $right
580
     * @param bool   $isParam
581
     *
582
     * @param string $t
583
     * @return string
584
     */
585
    public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t")
586
    {
587
        $vars = (null != $right) ? "\${$left}, {$right}" : "\${$left}";
588
        if (!$isParam) {
589
            $ret = "{$t}\${$var}[] = array_{$type}({$vars});\n";
590
        } else {
591
            $ret = "array_{$type}({$vars})";
592
        }
593
594
        return $ret;
595
    }
596
597
    /**
598
     *  @public function getPhpCodeSprintf
599
     *  @param $left
600
     *  @param $right
601
     *  @return string
602
     */
603
    public function getPhpCodeSprintf($left, $right)
604
    {
605
        return "sprintf({$left}, {$right})";
606
    }
607
608
    /**
609
     *  @public function getPhpCodeEmpty
610
     *  @param $var
611
     *  @return string
612
     */
613
    public function getPhpCodeEmpty($var)
614
    {
615
        return "empty({$var})";
616
    }
617
618
    /**
619
     *  @public function getPhpCodeHeader
620
     *  @param $var
621
     *  @return string
622
     */
623
    public function getPhpCodeHeader($var)
624
    {
625
        return "header({$var})";
626
    }
627
628
    /**
629
     *  @public function getPhpCodeRawurlencode
630
     *  @param $var
631
     *  @return string
632
     */
633
    public function getPhpCodeRawurlencode($var)
634
    {
635
        return "rawurlencode({$var})";
636
    }
637
638
    /**
639
     * @public function getPhpCodePregFunzions
640
     * @param        $var
641
     * @param        $exp
642
     * @param        $str
643
     * @param        $val
644
     * @param string $type
645
     * @param bool   $isParam
646
     *
647
     * @param string $t
648
     * @return string
649
     */
650
    public function getPhpCodePregFunzions($var, $exp, $str, $val, $type = 'match', $isParam = false, $t = "\t")
651
    {
652
        $pregFunz = "preg_{$type}('";
653
        if (!$isParam) {
654
            $ret = "{$t}\${$var} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
655
        } else {
656
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
657
        }
658
659
        return $ret;
660
    }
661
662
    /**
663
     * @public function getPhpCodeStrType
664
     * @param        $left
665
     * @param        $var
666
     * @param        $str
667
     * @param        $value
668
     * @param string $type
669
     * @param bool   $isParam
670
     *
671
     * @param string $t
672
     * @return string
673
     */
674
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t")
675
    {
676
        $strType = "str_{$type}('";
677
        if (!$isParam) {
678
            $ret = "{$t}\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
679
        } else {
680
            $ret = "{$strType}{$var}', '{$str}', {$value})";
681
        }
682
683
        return $ret;
684
    }
685
686
    /**
687
     * @public function getPhpCodeStripTags
688
     * @param        $left
689
     * @param        $value
690
     * @param bool   $isParam
691
     *
692
     * @param string $t
693
     * @return string
694
     */
695
    public function getPhpCodeStripTags($left, $value, $isParam = false, $t = '')
696
    {
697
        if (!$isParam) {
698
            $ret = "{$t}\${$left} = strip_tags({$value});\n";
699
        } else {
700
            $ret = "strip_tags({$value})";
701
        }
702
703
        return $ret;
704
    }
705
706
    /**
707
     *  @public function getPhpCodeHtmlentities
708
     *  @param $entitiesVar
709
     *  @param $entitiesQuote
710
     *  @return string
711
     */
712
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
713
    {
714
        $entitiesVar = (false !== $entitiesQuote) ? $entitiesVar . ', ' . $entitiesQuote : $entitiesVar;
715
        $entities = "htmlentities({$entitiesVar})";
716
717
        return $entities;
718
    }
719
720
    /**
721
     *  @public function getPhpCodeHtmlspecialchars
722
     *  @param $specialVar
723
     *  @param $specialQuote
724
     *  @return string
725
     */
726
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
727
    {
728
        $specialVar = (false !== $specialQuote) ? $specialVar . ', ' . $specialQuote : $specialVar;
729
        $specialchars = "htmlspecialchars({$specialVar})";
730
731
        return $specialchars;
732
    }
733
}
734