TextFormater::goDeeper()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 2
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