Completed
Pull Request — master (#72)
by Gino
04:14
created

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