Passed
Push — master ( 778ac9...8dd292 )
by Daniel
01:32
created

applyStringManipulationsSingle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 2
dl 0
loc 27
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2018 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\io_operations;
28
29
trait InputOutputStrings
30
{
31
32
    protected function applyStringManipulations($inString, $ruleDetails)
33
    {
34
        $outString = $inString;
35
        if (is_array($ruleDetails)) {
36
            $outString = $this->applyStringManipulationsArray($outString, $ruleDetails);
37
        } else {
38
            $outString = $this->applyStringManipulationsSingle($outString, $ruleDetails);
39
        }
40
        return $outString;
41
    }
42
43
    private function applyStringManipulationsArray($inString, $arrayRuleDetails)
44
    {
45
        $outString = $inString;
46
        foreach ($arrayRuleDetails as $strRuleCurrentDetail) {
47
            $outString = $this->applyStringManipulationsSingle($outString, $strRuleCurrentDetail);
48
        }
49
        return $outString;
50
    }
51
52
    private function applyStringManipulationsSingle($inString, $strManipulationRule)
53
    {
54
        $outString = $inString;
55
        switch ($strManipulationRule) {
56
            case 'replace numeric sequence followed by single space':
57
                $outString     = preg_replace('#([0-9]+ )#', '', $outString);
58
                break;
59
            case 'trim':
60
                $outString     = trim($outString);
61
                break;
62
            default:
63
                $arrayStandard = [
64
                    'remove comma followed by double quotes' => [',"', '',],
65
                    'remove dot'                             => ['.', '',],
66
                    'remove double quotes'                   => ['"', '',],
67
                    'remove double quotes followed by comma' => ['",', '',],
68
                    'remove pipeline'                        => ['|', '',],
69
                    'remove slash'                           => ['/', '',],
70
                    'replace dash with space'                => ['-', ' ',],
71
                    'replace comma with dot'                 => [',', '.',],
72
                ];
73
                if (array_key_exists($strManipulationRule, $arrayStandard)) {
74
                    $outString = str_replace($arrayStandard[0], $arrayStandard[1], $outString);
75
                }
76
                break;
77
        }
78
        return $outString;
79
    }
80
81
    protected function cleanString($strInput)
82
    {
83
        return str_replace([' ', "\n", "\r"], '', $strInput);
84
    }
85
86
    protected function setSeparator($strCharacter)
87
    {
88
        return str_repeat($strCharacter, 90);
89
    }
90
91
    protected function setTab($intMultiplier = 1)
92
    {
93
        return str_repeat(' ', (4 * $intMultiplier));
94
    }
95
96
}
97