1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Utils; |
4
|
|
|
|
5
|
|
|
class TextFormater |
6
|
|
|
{ |
7
|
|
|
public function toCamelCase($str) |
8
|
|
|
{ |
9
|
|
|
return preg_replace('/ /', '', ucwords($str)); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function toUnderscoreCase($str) |
13
|
|
|
{ |
14
|
|
|
$str = strtolower(preg_replace("[A-Z]", "_\$1", $str)); |
|
|
|
|
15
|
|
|
|
16
|
|
|
return preg_replace("/([^a-zA-Z])/", '_', $str); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function toSpaceCase($str) |
20
|
|
|
{ |
21
|
|
|
$str = strtolower(preg_replace("[A-Z]", "_\$1", $str)); |
|
|
|
|
22
|
|
|
|
23
|
|
|
return preg_replace("/([^a-zA-Z])/", ' ', $str); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function tableToString(array $array) |
27
|
|
|
{ |
28
|
|
|
if (1 === $this->getDimentions($array)) { |
29
|
|
|
|
30
|
|
|
return sprintf('|%s|', implode('|', array_map(function ($e) { return sprintf(' %s ', trim($e)); }, $array))); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$sizes = array(); |
34
|
|
|
foreach ($array as $row) { |
35
|
|
|
foreach ($row as $index => $cell) { |
36
|
|
|
if (empty($sizes[$index])) { |
37
|
|
|
$sizes[$index] = 0; |
38
|
|
|
} |
39
|
|
|
$sizes[$index] = max(array($sizes[$index], mb_strlen(trim($cell), 'UTF-8'))); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$lines = array(); |
44
|
|
|
foreach ($array as $row) { |
45
|
|
|
$cells = array(); |
46
|
|
|
foreach ($row as $index => $cell) { |
47
|
|
|
$cells[] = sprintf(' %s ', $this->mbStrPad(trim($cell), $sizes[$index])); |
48
|
|
|
} |
49
|
|
|
$lines[] = sprintf('|%s|', implode('|', $cells)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return implode("\n", $lines). "\n"; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function listToArray($list, $delimiters = [', ', ' and '], $parser = "#||#") |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$list = str_replace('"', '', $list); |
|
|
|
|
58
|
|
|
|
59
|
|
|
foreach ($delimiters as $delimiter) { |
60
|
|
|
$list = str_replace($delimiter, $parser, $list); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!is_string($list)) { |
64
|
|
|
throw new \Exception(sprintf('The list must be a string, not a "%s" element', gettype($list))); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$parts = explode($parser, $list); |
68
|
|
|
|
69
|
|
|
$parts = array_map('trim', $parts); |
70
|
|
|
$parts = array_filter($parts, 'strlen'); |
71
|
|
|
|
72
|
|
|
return $parts; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getDimentions(array $array) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return $this->goDeeper($array, 0); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function goDeeper(array $array, $deep) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$deep++; |
83
|
|
|
foreach ($array as $elem) { |
84
|
|
|
if (is_array($elem)) { |
85
|
|
|
$deep = max([ $this->goDeeper($elem, $deep), $deep ]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $deep; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function mbStrPad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$diff = strlen($input) - mb_strlen($input, 'UTF8'); |
95
|
|
|
|
96
|
|
|
return str_pad($input, $pad_length + $diff, $pad_string, $pad_type); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.