Passed
Push — master ( 900633...b5002d )
by Sebastian
03:40
created

ConvertHelper::explodeTrim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * File containing the {@see AppUtils\ConvertHelper} class.
4
 * 
5
 * @package Application Utils
6
 * @subpackage ConvertHelper
7
 * @see AppUtils\ConvertHelper
8
 */
9
10
namespace AppUtils;
11
12
use DateTime;
13
14
/**
15
 * Static conversion helper class: offers a number of utility methods
16
 * to convert variable types, as well as specialized methods for working
17
 * with specific types like dates.
18
 * 
19
 * @package Application Utils
20
 * @subpackage ConvertHelper
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class ConvertHelper
24
{
25
    const ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER = 23303;
26
    const ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE = 23304;
27
    const ERROR_JSON_ENCODE_FAILED = 23305;
28
    const ERROR_INVALID_BOOLEAN_STRING = 23306;
29
    
30
    /**
31
     * Normalizes tabs in the specified string by indenting everything
32
     * back to the minimum tab distance. With the second parameter,
33
     * tabs can optionally be converted to spaces as well (recommended
34
     * for HTML output).
35
     *
36
     * @param string $string
37
     * @param boolean $tabs2spaces
38
     * @return string
39
     */
40
    public static function normalizeTabs(string $string, bool $tabs2spaces = false) : string
41
    {
42
        return ConvertHelper_String::normalizeTabs($string, $tabs2spaces);
43
    }
44
45
    /**
46
     * Converts tabs to spaces in the specified string.
47
     * 
48
     * @param string $string
49
     * @param int $tabSize The amount of spaces per tab.
50
     * @return string
51
     */
52
    public static function tabs2spaces(string $string, int $tabSize=4) : string
53
    {
54
        return ConvertHelper_String::tabs2spaces($string, $tabSize);
55
    }
56
    
57
   /**
58
    * Converts spaces to tabs in the specified string.
59
    * 
60
    * @param string $string
61
    * @param int $tabSize The amount of spaces per tab in the source string.
62
    * @return string
63
    */
64
    public static function spaces2tabs(string $string, int $tabSize=4) : string
65
    {
66
        return ConvertHelper_String::spaces2tabs($string, $tabSize);
67
    }
68
69
    /**
70
     * Makes all hidden characters visible in the target string,
71
     * from spaces to control characters.
72
     *
73
     * @param string $string
74
     * @return string
75
     */
76
    public static function hidden2visible(string $string) : string
77
    {
78
        return ConvertHelper_String::hidden2visible($string);
79
    }
80
    
81
   /**
82
    * Converts the specified amount of seconds into
83
    * a human readable string split in months, weeks,
84
    * days, hours, minutes and seconds.
85
    *
86
    * @param float $seconds
87
    * @return string
88
    */
89
    public static function time2string($seconds) : string
90
    {
91
        $converter = new ConvertHelper_TimeConverter($seconds);
92
        return $converter->toString();
93
    }
94
95
   /**
96
    * Converts a timestamp into an easily understandable
97
    * format, e.g. "2 hours", "1 day", "3 months"
98
    *
99
    * If you set the date to parameter, the difference
100
    * will be calculated between the two dates and not
101
    * the current time.
102
    *
103
    * @param integer|DateTime $datefrom
104
    * @param integer|DateTime $dateto
105
    * @return string
106
    */
107
    public static function duration2string($datefrom, $dateto = -1) : string
108
    {
109
         $converter = new ConvertHelper_DurationConverter();
110
         
111
         if($datefrom instanceof DateTime)
112
         {
113
             $converter->setDateFrom($datefrom);
114
         }
115
         else
116
         {
117
             $converter->setDateFrom(self::timestamp2date($datefrom)); 
118
         }
119
120
         if($dateto instanceof DateTime)
121
         {
122
             $converter->setDateTo($dateto);
123
         }
124
         else if($dateto > 0)
125
         {
126
             $converter->setDateTo(self::timestamp2date($dateto));
127
         }
128
129
         return $converter->convert();
130
    }
131
132
   /**
133
    * Adds HTML syntax highlighting to the specified SQL string.
134
    * 
135
    * @param string $sql
136
    * @return string
137
    * @deprecated Use the Highlighter class directly instead.
138
    * @see Highlighter::sql()
139
    */
140
    public static function highlight_sql(string $sql) : string
141
    {
142
        return Highlighter::sql($sql);
143
    }
144
145
   /**
146
    * Adds HTML syntax highlighting to the specified XML code.
147
    * 
148
    * @param string $xml The XML to highlight.
149
    * @param bool $formatSource Whether to format the source with indentation to make it readable.
150
    * @return string
151
    * @deprecated Use the Highlighter class directly instead.
152
    * @see Highlighter::xml()
153
    */
154
    public static function highlight_xml(string $xml, bool $formatSource=false) : string
155
    {
156
        return Highlighter::xml($xml, $formatSource);
157
    }
158
159
   /**
160
    * @param string $phpCode
161
    * @return string
162
    * @deprecated Use the Highlighter class directly instead.
163
    * @see Highlighter::php()
164
    */
165
    public static function highlight_php(string $phpCode) : string
166
    {
167
        return Highlighter::php($phpCode);
168
    }
169
    
170
   /**
171
    * Converts a number of bytes to a human readable form,
172
    * e.g. xx Kb / xx Mb / xx Gb
173
    *
174
    * @param int $bytes The amount of bytes to convert.
175
    * @param int $precision The amount of decimals
176
    * @param int $base The base to calculate with: Base 10 is default (=1000 Bytes in a KB), Base 2 is mainly used for Windows memory (=1024 Bytes in a KB).
177
    * @return string
178
    * 
179
    * @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions
180
    */
181
    public static function bytes2readable(int $bytes, int $precision = 1, int $base = ConvertHelper_StorageSizeEnum::BASE_10) : string
182
    {
183
        return self::parseBytes($bytes)->toString($precision, $base);
184
    }
185
    
186
   /**
187
    * Parses a number of bytes, and creates a converter instance which
188
    * allows doing common operations with it.
189
    * 
190
    * @param int $bytes
191
    * @return ConvertHelper_ByteConverter
192
    */
193
    public static function parseBytes(int $bytes) : ConvertHelper_ByteConverter
194
    {
195
        return new ConvertHelper_ByteConverter($bytes);
196
    }
197
198
   /**
199
    * Cuts a text to the specified length if it is longer than the
200
    * target length. Appends a text to signify it has been cut at 
201
    * the end of the string.
202
    * 
203
    * @param string $text
204
    * @param int $targetLength
205
    * @param string $append
206
    * @return string
207
    */
208
    public static function text_cut(string $text, int $targetLength, string $append = '...') : string
209
    {
210
        return ConvertHelper_String::cutText($text, $targetLength, $append);
211
    }
212
213
    public static function var_dump($var, $html=true) : string
214
    {
215
        $info = parseVariable($var);
216
        
217
        if($html) {
218
            return $info->toHTML();
219
        }
220
        
221
        return $info->toString();
222
    }
223
    
224
   /**
225
    * Pretty print_r.
226
    * 
227
    * @param mixed $var The variable to dump.
228
    * @param bool $return Whether to return the dumped code.
229
    * @param bool $html Whether to style the dump as HTML.
230
    * @return string
231
    */
232
    public static function print_r($var, bool $return=false, bool $html=true) : string
233
    {
234
        $result = parseVariable($var)->enableType()->toString();
235
        
236
        if($html) 
237
        {
238
            $result = 
239
            '<pre style="background:#fff;color:#333;padding:16px;border:solid 1px #bbb;border-radius:4px">'.
240
                $result.
241
            '</pre>';
242
        }
243
        
244
        if(!$return) 
245
        {
246
            echo $result;
247
        }
248
        
249
        return $result;
250
    }
251
    
252
   /**
253
    * Converts a string, number or boolean value to a boolean value.
254
    *
255
    * @param mixed $string
256
    * @throws ConvertHelper_Exception
257
    * @return bool
258
    *
259
    * @see ConvertHelper::ERROR_INVALID_BOOLEAN_STRING
260
    */
261
    public static function string2bool($string) : bool
262
    {
263
        return ConvertHelper_Bool::fromString($string);
264
    }
265
266
   /**
267
    * Whether the specified string is a boolean string or boolean value.
268
    * Alias for {@link ConvertHelper::isBoolean()}.
269
    *
270
    * @param mixed $string
271
    * @return bool
272
    * @deprecated
273
    * @see ConvertHelper::isBoolean()
274
    */
275
    public static function isBooleanString($string) : bool
276
    {
277
        return self::isBoolean($string);
278
    }
279
280
   /**
281
    * Alias for the {@\AppUtils\XMLHelper::string2xml()} method.
282
    * 
283
    * @param string $text
284
    * @return string
285
    * @deprecated
286
    */
287
    public static function text_makeXMLCompliant($text)
288
    {
289
        return XMLHelper::string2xml($text);
290
    }
291
292
    /**
293
     * Transforms a date into a generic human readable date, optionally with time.
294
     * If the year is the same as the current one, it is omitted.
295
     *
296
     * - 6 Jan 2012
297
     * - 12 Dec 2012 17:45
298
     * - 5 Aug
299
     *
300
     * @param DateTime $date
301
     * @return string
302
     */
303
    public static function date2listLabel(DateTime $date, $includeTime = false, $shortMonth = false)
304
    {
305
        $today = new DateTime();
306
        if($date->format('d.m.Y') == $today->format('d.m.Y')) {
307
            $label = t('Today');
308
        } else {
309
            $label = $date->format('d') . '. ' . self::month2string((int)$date->format('m'), $shortMonth) . ' ';
310
            if ($date->format('Y') != date('Y')) {
311
                $label .= $date->format('Y');
312
            }
313
        }
314
        
315
        if ($includeTime) {
316
            $label .= $date->format(' H:i');
317
        }
318
319
        return trim($label);
320
    }
321
322
    protected static $months;
323
324
    /**
325
     * Returns a human readable month name given the month number. Can optionally
326
     * return the shorthand version of the month. Translated into the current
327
     * application locale.
328
     *
329
     * @param int|string $monthNr
330
     * @param boolean $short
331
     * @throws ConvertHelper_Exception
332
     * @return string
333
     */
334
    public static function month2string($monthNr, $short = false)
335
    {
336
        if (!isset(self::$months)) {
337
            self::$months = array(
338
                1 => array(t('January'), t('Jan')),
339
                2 => array(t('February'), t('Feb')),
340
                3 => array(t('March'), t('Mar')),
341
                4 => array(t('April'), t('Apr')),
342
                5 => array(t('May'), t('May')),
343
                6 => array(t('June'), t('Jun')),
344
                7 => array(t('July'), t('Jul')),
345
                8 => array(t('August'), t('Aug')),
346
                9 => array(t('September'), t('Sep')),
347
                10 => array(t('October'), t('Oct')),
348
                11 => array(t('November'), t('Nov')),
349
                12 => array(t('December'), t('Dec'))
350
            );
351
        }
352
353
        $monthNr = intval($monthNr);
354
        if (!isset(self::$months[$monthNr])) {
355
            throw new ConvertHelper_Exception(
356
                'Invalid month number',
357
                sprintf('%1$s is not a valid month number.', $monthNr),
358
                self::ERROR_MONTHTOSTRING_NOT_VALID_MONTH_NUMBER
359
            );
360
        }
361
362
        if ($short) {
363
            return self::$months[$monthNr][1];
364
        }
365
366
        return self::$months[$monthNr][0];
367
    }
368
369
    /**
370
     * Transliterates a string.
371
     *
372
     * @param string $string
373
     * @param string $spaceChar
374
     * @param bool $lowercase
375
     * @return string
376
     */
377
    public static function transliterate(string $string, string $spaceChar = '-', bool $lowercase = true) : string
378
    {
379
        return ConvertHelper_String::transliterate($string, $spaceChar, $lowercase);
380
    }
381
    
382
   /**
383
    * Retrieves the HEX character codes for all control
384
    * characters that the {@link stripControlCharacters()} 
385
    * method will remove.
386
    * 
387
    * @return string[]
388
    */
389
    public static function getControlCharactersAsHex()
390
    {
391
        return self::createControlCharacters()->getCharsAsHex();
392
    }
393
    
394
   /**
395
    * Retrieves an array of all control characters that
396
    * the {@link stripControlCharacters()} method will 
397
    * remove, as the actual UTF-8 characters.
398
    * 
399
    * @return string[]
400
    */
401
    public static function getControlCharactersAsUTF8()
402
    {
403
        return self::createControlCharacters()->getCharsAsUTF8();
404
    }
405
    
406
   /**
407
    * Retrieves all control characters as JSON encoded
408
    * characters, e.g. "\u200b".
409
    * 
410
    * @return string[]
411
    */
412
    public static function getControlCharactersAsJSON()
413
    {
414
        return self::createControlCharacters()->getCharsAsJSON();
415
    }
416
417
    /**
418
     * Removes all control characters from the specified string
419
     * that can cause problems in some cases, like creating
420
     * valid XML documents. This includes invisible non-breaking
421
     * spaces.
422
     *
423
     * @param string $string
424
     * @return string
425
     * @throws ConvertHelper_Exception
426
     */
427
    public static function stripControlCharacters(string $string) : string
428
    {
429
        return self::createControlCharacters()->stripControlCharacters($string);
430
    }
431
    
432
   /**
433
    * Creates the control characters class, used to 
434
    * work with control characters in strings.
435
    * 
436
    * @return ConvertHelper_ControlCharacters
437
    */
438
    public static function createControlCharacters() : ConvertHelper_ControlCharacters
439
    {
440
        return new ConvertHelper_ControlCharacters();
441
    }
442
443
   /**
444
    * Converts a unicode character to the PHPO notation.
445
    * 
446
    * Example:
447
    * 
448
    * <pre>unicodeChar2php('"\u0000"')</pre>
449
    * 
450
    * Returns
451
    * 
452
    * <pre>\x0</pre>
453
    * 
454
    * @param string $unicodeChar
455
    * @return string
456
    */
457
    public static function unicodeChar2php(string $unicodeChar) : string 
458
    {
459
        $unicodeChar = json_decode($unicodeChar);
460
        
461
        $output = '';
462
        $split = str_split($unicodeChar);
463
        
464
        foreach($split as $octet) 
465
        {
466
            $ordInt = ord($octet);
467
            // Convert from int (base 10) to hex (base 16), for PHP \x syntax
468
            $ordHex = base_convert((string)$ordInt, 10, 16);
469
            $output .= '\x' . $ordHex;
470
        }
471
        
472
        return $output;
473
    }
474
    
475
    /**
476
     * Removes the extension from the specified filename
477
     * and returns the name without the extension.
478
     *
479
     * Example:
480
     * filename.html > filename
481
     * passed.test.jpg > passed.test
482
     * path/to/file/document.txt > document
483
     *
484
     * @param string $filename
485
     * @return string
486
     */
487
    public static function filenameRemoveExtension($filename)
488
    {
489
        return FileHelper::removeExtension($filename);
490
    }
491
    
492
    public static function areVariablesEqual($a, $b) : bool
493
    {
494
        $a = self::convertScalarForComparison($a);
495
        $b = self::convertScalarForComparison($b);
496
497
        return $a === $b;
498
    }
499
    
500
    protected static function convertScalarForComparison($scalar)
501
    {
502
        if($scalar === '' || is_null($scalar)) {
503
            return null;
504
        }
505
        
506
        if(is_bool($scalar)) {
507
            return self::bool2string($scalar);
508
        }
509
        
510
        if(is_array($scalar)) {
511
            $scalar = md5(serialize($scalar));
512
        }
513
        
514
        if($scalar !== null && !is_scalar($scalar)) {
515
            throw new ConvertHelper_Exception(
516
                'Not a scalar value in comparison',
517
                null,
518
                self::ERROR_CANNOT_NORMALIZE_NON_SCALAR_VALUE
519
            );
520
        }
521
        
522
        return strval($scalar);
523
    }
524
525
    /**
526
     * Compares two strings to check whether they are equal.
527
     * null and empty strings are considered equal.
528
     *
529
     * @param string $a
530
     * @param string $b
531
     * @return boolean
532
     */
533
    public static function areStringsEqual($a, $b) : bool
534
    {
535
        return self::areVariablesEqual($a, $b);
536
    }
537
538
    /**
539
     * Checks whether the two specified numbers are equal.
540
     * null and empty strings are considered as 0 values.
541
     *
542
     * @param number|string $a
543
     * @param number|string $b
544
     * @return boolean
545
     */
546
    public static function areNumbersEqual($a, $b) : bool
547
    {
548
        return self::areVariablesEqual($a, $b);
549
    }
550
551
    /**
552
     * Converts a boolean value to a string. Defaults to returning
553
     * 'true' or 'false', with the additional parameter it can also
554
     * return the 'yes' and 'no' variants.
555
     *
556
     * @param boolean|string $boolean
557
     * @param boolean $yesno
558
     * @return string
559
     * @throws ConvertHelper_Exception
560
     */
561
    public static function bool2string($boolean, bool $yesno = false) : string
562
    {
563
        return ConvertHelper_Bool::toString($boolean, $yesno);
564
    }
565
    
566
   /**
567
    * Converts an associative array with attribute name > value pairs
568
    * to an attribute string that can be used in an HTML tag. Empty 
569
    * attribute values are ignored.
570
    * 
571
    * Example:
572
    * 
573
    * array2attributeString(array(
574
    *     'id' => 45,
575
    *     'href' => 'http://www.mistralys.com'
576
    * ));
577
    * 
578
    * Result:
579
    * 
580
    * id="45" href="http://www.mistralys.com"
581
    * 
582
    * @param array<string,mixed> $array
583
    * @return string
584
    */
585
    public static function array2attributeString(array $array) : string
586
    {
587
        return ConvertHelper_Array::toAttributeString($array);
588
    }
589
    
590
   /**
591
    * Converts a string so it can safely be used in a javascript
592
    * statement in an HTML tag: uses single quotes around the string
593
    * and encodes all special characters as needed.
594
    * 
595
    * @param string $string
596
    * @return string
597
    * @deprecated Use the JSHelper class instead.
598
    * @see JSHelper::phpVariable2AttributeJS()
599
    */
600
    public static function string2attributeJS(string $string) : string
601
    {
602
        return JSHelper::phpVariable2AttributeJS($string);
603
    }
604
    
605
   /**
606
    * Checks if the specified string is a boolean value, which
607
    * includes string representations of boolean values, like 
608
    * <code>yes</code> or <code>no</code>, and <code>true</code>
609
    * or <code>false</code>.
610
    * 
611
    * @param mixed $value
612
    * @return boolean
613
    */
614
    public static function isBoolean($value) : bool
615
    {
616
        return ConvertHelper_Bool::isBoolean($value);
617
    }
618
    
619
   /**
620
    * Converts an associative array to an HTML style attribute value string.
621
    * 
622
    * @param array<string,mixed> $subject
623
    * @return string
624
    */
625
    public static function array2styleString(array $subject) : string
626
    {
627
        return ConvertHelper_Array::toStyleString($subject);
628
    }
629
    
630
   /**
631
    * Converts a DateTime object to a timestamp, which
632
    * is PHP 5.2 compatible.
633
    * 
634
    * @param DateTime $date
635
    * @return integer
636
    */
637
    public static function date2timestamp(DateTime $date) : int
638
    {
639
        return (int)$date->format('U');
640
    }
641
    
642
   /**
643
    * Converts a timestamp into a DateTime instance.
644
    * @param int $timestamp
645
    * @return DateTime
646
    */
647
    public static function timestamp2date(int $timestamp) : DateTime
648
    {
649
        $date = new DateTime();
650
        $date->setTimestamp($timestamp);
651
        return $date;
652
    }
653
    
654
   /**
655
    * Strips an absolute path to a file within the application
656
    * to make the path relative to the application root path.
657
    * 
658
    * @param string $path
659
    * @return string
660
    * 
661
    * @see FileHelper::relativizePath()
662
    * @see FileHelper::relativizePathByDepth()
663
    */
664
    public static function fileRelativize(string $path) : string
665
    {
666
        return FileHelper::relativizePathByDepth($path);
667
    }
668
    
669
    /**
670
    * Converts a PHP regex to a javascript RegExp object statement.
671
    * 
672
    * NOTE: This is an alias for the JSHelper's `convertRegex` method. 
673
    * More details are available on its usage there.
674
    *
675
    * @param string $regex A PHP preg regex
676
    * @param string $statementType The type of statement to return: Defaults to a statement to create a RegExp object.
677
    * @return array|string Depending on the specified return type.
678
    * 
679
    * @see JSHelper::buildRegexStatement()
680
    */
681
    public static function regex2js(string $regex, string $statementType=JSHelper::JS_REGEX_OBJECT)
682
    {
683
        return JSHelper::buildRegexStatement($regex, $statementType);
684
    }
685
    
686
   /**
687
    * Converts the specified variable to JSON. Works just
688
    * like the native `json_encode` method, except that it
689
    * will trigger an exception on failure, which has the 
690
    * json error details included in its developer details.
691
    * 
692
    * @param mixed $variable
693
    * @param int $options JSON encode options.
694
    * @param int $depth 
695
    * @throws ConvertHelper_Exception
696
    * @return string
697
    */
698
    public static function var2json($variable, int $options=0, int $depth=512) : string
699
    {
700
        $result = json_encode($variable, $options, $depth);
701
        
702
        if($result !== false) {
703
            return $result;
704
        }
705
        
706
        throw new ConvertHelper_Exception(
707
            'Could not create json array'.json_last_error_msg(),
708
            sprintf(
709
                'The call to json_encode failed for the variable [%s]. JSON error details: #%s, %s',
710
                parseVariable($variable)->toString(),
711
                json_last_error(),
712
                json_last_error_msg()
713
            ),
714
            self::ERROR_JSON_ENCODE_FAILED
715
        );
716
    }
717
718
    /**
719
     * Converts any PHP variable to a human readable
720
     * string representation, like "object ClassName"
721
     *
722
     * @param mixed $variable
723
     * @return string
724
     */
725
    public function var2string($variable) : string
726
    {
727
        return parseVariable($variable)
728
            ->enableType()
729
            ->toString();
730
    }
731
    
732
   /**
733
    * Strips all known UTF byte order marks from the specified string.
734
    * 
735
    * @param string $string
736
    * @return string
737
    */
738
    public static function stripUTFBom($string)
739
    {
740
        $boms = FileHelper::getUTFBOMs();
741
        foreach($boms as $bomChars) {
742
            $length = mb_strlen($bomChars);
743
            $text = mb_substr($string, 0, $length);
744
            if($text==$bomChars) {
745
                return mb_substr($string, $length);
746
            }
747
        }
748
        
749
        return $string;
750
    }
751
752
   /**
753
    * Converts a string to valid utf8, regardless
754
    * of the string's encoding(s).
755
    * 
756
    * @param string $string
757
    * @return string
758
    */
759
    public static function string2utf8(string $string) : string
760
    {
761
        return ConvertHelper_String::toUtf8($string);
762
    }
763
    
764
   /**
765
    * Checks whether the specified string is an ASCII
766
    * string, without any special or UTF8 characters.
767
    * Note: empty strings and NULL are considered ASCII.
768
    * Any variable types other than strings are not.
769
    * 
770
    * @param string|float|int|NULL $string
771
    * @return boolean
772
    */
773
    public static function isStringASCII($string) : bool
774
    {
775
        return ConvertHelper_String::isASCII(strval($string));
776
    }
777
    
778
   /**
779
    * Adds HTML syntax highlighting to an URL.
780
    * 
781
    * NOTE: Includes the necessary CSS styles. When
782
    * highlighting several URLs in the same page,
783
    * prefer using the `parseURL` function instead.
784
    * 
785
    * @param string $url
786
    * @return string
787
    * @deprecated Use the Highlighter class directly instead.
788
    * @see Highlighter
789
    */
790
    public static function highlight_url(string $url) : string
791
    {
792
        return Highlighter::url($url);
793
    }
794
795
   /**
796
    * Calculates a percentage match of the source string with the target string.
797
    * 
798
    * Options are:
799
    * 
800
    * - maxLevenshtein, default: 10
801
    *   Any levenshtein results above this value are ignored.
802
    *   
803
    * - precision, default: 1
804
    *   The precision of the percentage float value
805
    * 
806
    * @param string $source
807
    * @param string $target
808
    * @param array $options
809
    * @return float
810
    */
811
    public static function matchString($source, $target, $options=array())
812
    {
813
        $defaults = array(
814
            'maxLevenshtein' => 10,
815
            'precision' => 1
816
        );
817
        
818
        $options = array_merge($defaults, $options);
819
        
820
        // avoid doing this via levenshtein
821
        if($source == $target) {
822
            return 100;
823
        }
824
        
825
        $diff = levenshtein($source, $target);
826
        if($diff > $options['maxLevenshtein']) {
827
            return 0;
828
        }
829
        
830
        $percent = $diff * 100 / ($options['maxLevenshtein'] + 1);
831
        return round(100 - $percent, $options['precision']);
832
    }
833
    
834
   /**
835
    * Converts a date interval to a human readable string with
836
    * all necessary time components, e.g. "1 year, 2 months and 4 days".
837
    * 
838
    * @param \DateInterval $interval
839
    * @return string
840
    * @see ConvertHelper_IntervalConverter
841
    */
842
    public static function interval2string(\DateInterval $interval) : string
843
    {
844
        $converter = new ConvertHelper_IntervalConverter();
845
        return $converter->toString($interval);
846
    }
847
    
848
    const INTERVAL_DAYS = 'days';
849
    
850
    const INTERVAL_HOURS = 'hours';
851
    
852
    const INTERVAL_MINUTES = 'minutes';
853
    
854
    const INTERVAL_SECONDS = 'seconds';
855
    
856
   /**
857
    * Converts an interval to its total amount of days.
858
    * @param \DateInterval $interval
859
    * @return int
860
    */
861
    public static function interval2days(\DateInterval $interval) : int
862
    {
863
        return self::interval2total($interval, self::INTERVAL_DAYS);
864
    }
865
866
   /**
867
    * Converts an interval to its total amount of hours.
868
    * @param \DateInterval $interval
869
    * @return int
870
    */
871
    public static function interval2hours(\DateInterval $interval) : int
872
    {
873
        return self::interval2total($interval, self::INTERVAL_HOURS);
874
    }
875
    
876
   /**
877
    * Converts an interval to its total amount of minutes. 
878
    * @param \DateInterval $interval
879
    * @return int
880
    */
881
    public static function interval2minutes(\DateInterval $interval) : int
882
    {
883
        return self::interval2total($interval, self::INTERVAL_MINUTES);
884
    }
885
    
886
   /**
887
    * Converts an interval to its total amount of seconds.
888
    * @param \DateInterval $interval
889
    * @return int
890
    */    
891
    public static function interval2seconds(\DateInterval $interval) : int
892
    {
893
        return self::interval2total($interval, self::INTERVAL_SECONDS);
894
    }
895
    
896
   /**
897
    * Calculates the total amount of days / hours / minutes or seconds
898
    * of a date interval object (depending in the specified units), and 
899
    * returns the total amount.
900
    * 
901
    * @param \DateInterval $interval
902
    * @param string $unit What total value to calculate.
903
    * @return integer
904
    * 
905
    * @see ConvertHelper::INTERVAL_SECONDS
906
    * @see ConvertHelper::INTERVAL_MINUTES
907
    * @see ConvertHelper::INTERVAL_HOURS
908
    * @see ConvertHelper::INTERVAL_DAYS
909
    */
910
    public static function interval2total(\DateInterval $interval, $unit=self::INTERVAL_SECONDS) : int
911
    {
912
        $total = (int)$interval->format('%a');
913
        if ($unit == self::INTERVAL_DAYS) {
914
            return $total;
915
        }
916
        
917
        $total = ($total * 24) + ((int)$interval->h );
918
        if ($unit == self::INTERVAL_HOURS) {
919
            return $total;
920
        }
921
    
922
        $total = ($total * 60) + ((int)$interval->i );
923
        if ($unit == self::INTERVAL_MINUTES) {
924
            return $total;
925
        }
926
927
        $total = ($total * 60) + ((int)$interval->s );
928
        if ($unit == self::INTERVAL_SECONDS) {
929
            return $total;
930
        }
931
        
932
        return 0;
933
    }
934
935
    protected static $days;
936
    
937
    protected static $daysShort;
938
939
    protected static $daysInvariant = array(
940
        'Monday',
941
        'Tuesday',
942
        'Wednesday',
943
        'Thursday',
944
        'Friday',
945
        'Saturday',
946
        'Sunday'
947
    );
948
    
949
   /**
950
    * Converts a date to the corresponding day name.
951
    * 
952
    * @param DateTime $date
953
    * @param bool $short
954
    * @return string|NULL
955
    */
956
    public static function date2dayName(DateTime $date, bool $short=false)
957
    {
958
        $day = $date->format('l');
959
        $invariant = self::getDayNamesInvariant();
960
        
961
        $idx = array_search($day, $invariant);
962
        if($idx !== false) {
963
            $localized = self::getDayNames($short);
964
            return $localized[$idx];
965
        }
966
        
967
        return null;
968
    }
969
    
970
   /**
971
    * Retrieves a list of english day names.
972
    * @return string[]
973
    */
974
    public static function getDayNamesInvariant()
975
    {
976
        return self::$daysInvariant;
977
    }
978
    
979
   /**
980
    * Retrieves the day names list for the current locale.
981
    * 
982
    * @param bool $short
983
    * @return array
984
    */
985
    public static function getDayNames(bool $short=false) : array
986
    {
987
        if($short) {
988
            if(!isset(self::$daysShort)) {
989
                self::$daysShort = array(
990
                    t('Mon'),
991
                    t('Tue'),
992
                    t('Wed'),
993
                    t('Thu'),
994
                    t('Fri'),
995
                    t('Sat'),
996
                    t('Sun')
997
                );
998
            }
999
            
1000
            return self::$daysShort;
1001
        }
1002
        
1003
        if(!isset(self::$days)) {
1004
            self::$days = array(
1005
                t('Monday'),
1006
                t('Tuesday'),
1007
                t('Wednesday'),
1008
                t('Thursday'),
1009
                t('Friday'),
1010
                t('Saturday'),
1011
                t('Sunday')
1012
            );
1013
        }
1014
        
1015
        return self::$days;
1016
    }
1017
1018
    /**
1019
     * Implodes an array with a separator character, and the last item with "add".
1020
     * 
1021
     * @param array $list The indexed array with items to implode.
1022
     * @param string $sep The separator character to use.
1023
     * @param string $conjunction The word to use as conjunction with the last item in the list. NOTE: include spaces as needed.
1024
     * @return string
1025
     */
1026
    public static function implodeWithAnd(array $list, string $sep = ', ', string $conjunction = '')
1027
    {
1028
        return ConvertHelper_Array::implodeWithAnd($list, $sep, $conjunction);
1029
    }
1030
    
1031
   /**
1032
    * Splits a string into an array of all characters it is composed of.
1033
    * Unicode character safe.
1034
    * 
1035
    * NOTE: Spaces and newlines (both \r and \n) are also considered single
1036
    * characters.
1037
    * 
1038
    * @param string $string
1039
    * @return string[]
1040
    */
1041
    public static function string2array(string $string) : array
1042
    {
1043
        return ConvertHelper_String::toArray($string);
0 ignored issues
show
Bug Best Practice introduced by
The expression return AppUtils\ConvertH...tring::toArray($string) returns an array which contains values of type array which are incompatible with the documented value type string.
Loading history...
1044
    }
1045
    
1046
   /**
1047
    * Checks whether the specified string contains HTML code.
1048
    * 
1049
    * @param string $string
1050
    * @return boolean
1051
    */
1052
    public static function isStringHTML(string $string) : bool
1053
    {
1054
        return ConvertHelper_String::isHTML($string);
1055
    }
1056
    
1057
   /**
1058
    * UTF8-safe wordwrap method: works like the regular wordwrap
1059
    * PHP function but compatible with UTF8. Otherwise the lengths
1060
    * are not calculated correctly.
1061
    * 
1062
    * @param string $str
1063
    * @param int $width
1064
    * @param string $break
1065
    * @param bool $cut
1066
    * @return string
1067
    */
1068
    public static function wordwrap(string $str, int $width = 75, string $break = "\n", bool $cut = false) : string 
1069
    {
1070
        return ConvertHelper_String::wordwrap($str, $width, $break, $cut);
1071
    }
1072
    
1073
   /**
1074
    * Calculates the byte length of a string, taking into 
1075
    * account any unicode characters.
1076
    * 
1077
    * @param string $string
1078
    * @return int
1079
    */
1080
    public static function string2bytes(string $string): int
1081
    {
1082
        return ConvertHelper_String::toBytes($string);
1083
    }
1084
    
1085
   /**
1086
    * Creates a short, 8-character long hash for the specified string.
1087
    * 
1088
    * WARNING: Not cryptographically safe.
1089
    * 
1090
    * @param string $string
1091
    * @return string
1092
    */
1093
    public static function string2shortHash(string $string) : string
1094
    {
1095
        return ConvertHelper_String::toShortHash($string);
1096
    }
1097
1098
    /**
1099
     * Converts a string into an MD5 hash.
1100
     *
1101
     * @param string $string
1102
     * @return string
1103
     */
1104
    public static function string2hash(string $string): string
1105
    {
1106
        return ConvertHelper_String::toHash($string);
1107
    }
1108
1109
    /**
1110
     * Converts the specified callable to string.
1111
     *
1112
     * NOTE: Will work even if the callable is not
1113
     * actually callable, as compared to
1114
     * `parseVariable($callback)->toString()`.
1115
     *
1116
     * @param callable $callback
1117
     * @return string
1118
     */
1119
    public static function callback2string($callback) : string
1120
    {
1121
        // We are creating the renderer manually, to allow rendering
1122
        // callbacks to string even if they are not actually callable.
1123
        $renderer = new VariableInfo_Renderer_String_Callable(parseVariable($callback));
1124
1125
        return $renderer->render();
1126
    }
1127
1128
    public static function exception2info(\Throwable $e) : ConvertHelper_ThrowableInfo
1129
    {
1130
        return self::throwable2info($e);
1131
    }
1132
    
1133
    public static function throwable2info(\Throwable $e) : ConvertHelper_ThrowableInfo
1134
    {
1135
        return ConvertHelper_ThrowableInfo::fromThrowable($e);
1136
    }
1137
    
1138
   /**
1139
    * Parses the specified query string like the native 
1140
    * function <code>parse_str</code>, without the key
1141
    * naming limitations.
1142
    * 
1143
    * Using parse_str, dots or spaces in key names are 
1144
    * replaced by underscores. This method keeps all names
1145
    * intact.
1146
    * 
1147
    * It still uses the parse_str implementation as it 
1148
    * is tested and tried, but fixes the parameter names
1149
    * after parsing, as needed.
1150
    * 
1151
    * @param string $queryString
1152
    * @return array
1153
    * @see ConvertHelper_QueryParser
1154
    */
1155
    public static function parseQueryString(string $queryString) : array
1156
    {
1157
        $parser = new ConvertHelper_QueryParser();
1158
        return $parser->parse($queryString);
1159
    }
1160
1161
   /**
1162
    * Searches for needle in the specified string, and returns a list
1163
    * of all occurrences, including the matched string. The matched 
1164
    * string is useful when doing a case insensitive search, as it 
1165
    * shows the exact matched case of needle.
1166
    *   
1167
    * @param string $needle
1168
    * @param string $haystack
1169
    * @param bool $caseInsensitive
1170
    * @return ConvertHelper_StringMatch[]
1171
    */
1172
    public static function findString(string $needle, string $haystack, bool $caseInsensitive=false): array
1173
    {
1174
        return ConvertHelper_String::findString($needle, $haystack, $caseInsensitive);
1175
    }
1176
    
1177
   /**
1178
    * Like explode, but trims all entries, and removes 
1179
    * empty entries from the resulting array.
1180
    * 
1181
    * @param string $delimiter
1182
    * @param string $string
1183
    * @return string[]
1184
    */
1185
    public static function explodeTrim(string $delimiter, string $string) : array
1186
    {
1187
        return ConvertHelper_String::explodeTrim($delimiter, $string);
1188
    }
1189
    
1190
   /**
1191
    * Detects the most used end-of-line character in the subject string.
1192
    * 
1193
    * @param string $subjectString The string to check.
1194
    * @return NULL|ConvertHelper_EOL The detected EOL instance, or NULL if none has been detected.
1195
    */
1196
    public static function detectEOLCharacter(string $subjectString) : ?ConvertHelper_EOL
1197
    {
1198
        return ConvertHelper_EOL::detect($subjectString);
1199
    }
1200
1201
   /**
1202
    * Removes the specified keys from the target array,
1203
    * if they exist.
1204
    * 
1205
    * @param array $array
1206
    * @param array $keys
1207
    */
1208
    public static function arrayRemoveKeys(array &$array, array $keys) : void
1209
    {
1210
        ConvertHelper_Array::removeKeys($array, $keys);
1211
    }
1212
    
1213
   /**
1214
    * Checks if the specified variable is an integer or a string containing an integer.
1215
    * Accepts both positive and negative integers.
1216
    * 
1217
    * @param mixed $value
1218
    * @return bool
1219
    */
1220
    public static function isInteger($value) : bool
1221
    {
1222
        if(is_int($value)) {
1223
            return true;
1224
        }
1225
        
1226
        // booleans get converted to numbers, so they would
1227
        // actually match the regex.
1228
        if(is_bool($value)) {
1229
            return false;
1230
        }
1231
        
1232
        if(is_string($value) && $value !== '') {
1233
            return preg_match('/\A-?\d+\z/', $value) === 1;
1234
        }
1235
        
1236
        return false;    
1237
    }
1238
    
1239
   /**
1240
    * Converts an amount of seconds to a DateInterval object.
1241
    * 
1242
    * @param int $seconds
1243
    * @return \DateInterval
1244
    * @throws ConvertHelper_Exception If the date interval cannot be created.
1245
    * 
1246
    * @see ConvertHelper::ERROR_CANNOT_GET_DATE_DIFF
1247
    */
1248
    public static function seconds2interval(int $seconds) : \DateInterval
1249
    {
1250
        return ConvertHelper_DateInterval::fromSeconds($seconds)->getInterval();
1251
    }
1252
    
1253
   /**
1254
    * Converts a size string like "50 MB" to the corresponding byte size.
1255
    * It is case insensitive, ignores spaces, and supports both traditional
1256
    * "MB" and "MiB" notations.
1257
    * 
1258
    * @param string $size
1259
    * @return int
1260
    */
1261
    public static function size2bytes(string $size) : int
1262
    {
1263
        return self::parseSize($size)->toBytes();
1264
    }
1265
    
1266
   /**
1267
    * Parses a size string like "50 MB" and returns a size notation instance
1268
    * that has utility methods to access information on it, and convert it.
1269
    * 
1270
    * @param string $size
1271
    * @return ConvertHelper_SizeNotation
1272
    */
1273
    public static function parseSize(string $size) : ConvertHelper_SizeNotation
1274
    {
1275
        return new ConvertHelper_SizeNotation($size);
1276
    }
1277
    
1278
   /**
1279
    * Creates a URL finder instance, which can be used to find
1280
    * URLs in a string - be it plain text, or HTML.
1281
    * 
1282
    * @param string $subject
1283
    * @return ConvertHelper_URLFinder
1284
    */
1285
    public static function createURLFinder(string $subject) : ConvertHelper_URLFinder
1286
    {
1287
        return new ConvertHelper_URLFinder($subject);
1288
    }
1289
1290
    public static function arrayRemoveValues(array $sourceArray, array $values, bool $keepKeys=false) : array
1291
    {
1292
        return ConvertHelper_Array::removeValues($sourceArray, $values, $keepKeys);
1293
    }
1294
}
1295