applyStringManipulationsSingle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 4
eloc 14
c 4
b 0
f 2
nc 4
nop 2
dl 0
loc 18
rs 9.7998
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
        $outStr = $inString;
55
        switch ($strManipulationRule) {
56
            case 'replace numeric sequence followed by single space':
57
                $outStr = preg_replace('#([0-9]+ )#', '', $outStr);
58
                break;
59
            case 'trim':
60
                $outStr = trim($outStr);
61
                break;
62
            default:
63
                $aryOut = $this->knownStringPatterns($strManipulationRule);
64
                if ($aryOut['Key Exists']) {
65
                    $outStr = str_ireplace($aryOut['Attributes']['Original'], $aryOut['Attributes']['Final'], $outStr);
66
                }
67
                break;
68
        }
69
        return $outStr;
70
    }
71
72
    protected function cleanString($strInput)
73
    {
74
        return str_replace([' ', "\n", "\r"], '', $strInput);
75
    }
76
77
    private function knownStringPatterns($strIdentifier)
78
    {
79
        $arrayStandard = [
80
            'remove EOL Unix'                        => ['Original' => chr(13), 'Final' => '',],
81
            'remove EOL Windows'                     => ['Original' => chr(10) . chr(13), 'Final' => '',],
82
            'remove colon'                           => ['Original' => ':', 'Final' => '',],
83
            'remove comma followed by double quotes' => ['Original' => ',"', 'Final' => '',],
84
            'remove dot'                             => ['Original' => '.', 'Final' => '',],
85
            'remove double quotes'                   => ['Original' => '"', 'Final' => '',],
86
            'remove double quotes followed by comma' => ['Original' => '",', 'Final' => '',],
87
            'remove pipeline'                        => ['Original' => '|', 'Final' => '',],
88
            'remove semicolon'                       => ['Original' => ';', 'Final' => '',],
89
            'remove slash'                           => ['Original' => '/', 'Final' => '',],
90
            'replace dash with space'                => ['Original' => '-', 'Final' => ' ',],
91
            'replace comma with dot'                 => ['Original' => ',', 'Final' => '.',],
92
            'replace circumflex accent with dot'     => ['Original' => '^', 'Final' => '.',],
93
        ];
94
        $bolKeyExists  = array_key_exists($strIdentifier, $arrayStandard);
95
        return [
96
            'Key Exists' => $bolKeyExists,
97
            'Attributes' => ($bolKeyExists ? $arrayStandard[$strIdentifier] : null),
98
        ];
99
    }
100
101
    protected function setSeparator($strCharacter)
102
    {
103
        return str_repeat($strCharacter, 90);
104
    }
105
106
    protected function setTab($intMultiplier = 1)
107
    {
108
        return str_repeat(' ', (4 * $intMultiplier));
109
    }
110
}
111