try to handle nightly debug port resets
[auerswald-callnotifier.git] / src / callnotifier / Functions.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Some helper functions
6  */
7 class Functions
8 {
9     /**
10      * Multibyte-version of str_pad
11      *
12      * @link http://stackoverflow.com/a/14773638/282601
13      */
14     public static function mb_str_pad(
15         $input, $pad_length, $pad_string = ' ',
16         $pad_type = STR_PAD_RIGHT, $encoding = 'UTF-8'
17     ) {
18         if (!function_exists('mb_substr')) {
19             return str_pad($input, $pad_length, $pad_string, $pad_type);
20         }
21
22         $input_length = mb_strlen($input, $encoding);
23         $pad_string_length = mb_strlen($pad_string, $encoding);
24
25         if ($pad_length <= 0 || ($pad_length - $input_length) <= 0) {
26             return $input;
27         }
28
29         $num_pad_chars = $pad_length - $input_length;
30
31         switch ($pad_type) {
32         case STR_PAD_RIGHT:
33             $left_pad = 0;
34             $right_pad = $num_pad_chars;
35             break;
36
37         case STR_PAD_LEFT:
38             $left_pad = $num_pad_chars;
39             $right_pad = 0;
40             break;
41
42         case STR_PAD_BOTH:
43             $left_pad = floor($num_pad_chars / 2);
44             $right_pad = $num_pad_chars - $left_pad;
45             break;
46         }
47
48         $result = '';
49         for ($i = 0; $i < $left_pad; ++$i) {
50             $result .= mb_substr($pad_string, $i % $pad_string_length, 1, $encoding);
51         }
52         $result .= $input;
53         for ($i = 0; $i < $right_pad; ++$i) {
54             $result .= mb_substr($pad_string, $i % $pad_string_length, 1, $encoding);
55         }
56
57         return $result;
58     }
59 }
60 ?>