InputOutputArrays::setArrayToArrayKbr()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
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 InputOutputArrays
30
{
31
32
    /**
33
     * Replace space with break line for each key element
34
     *
35
     * @param array $aElements
36
     * @return array
37
     */
38
    protected function setArrayToArrayKbr(array $aElements)
39
    {
40
        $aReturn = [];
41
        foreach ($aElements as $key => $value) {
42
            $aReturn[str_replace(' ', '<br/>', $key)] = $value;
43
        }
44
        return $aReturn;
45
    }
46
47
    /**
48
     * Converts a single-child array into an parent-child one
49
     *
50
     * @param array $inArray
51
     * @return array
52
     */
53
    public function setArrayValuesAsKey(array $inArray)
54
    {
55
        $outArray = array_combine($inArray, $inArray);
56
        ksort($outArray);
57
        return $outArray;
58
    }
59
60
    protected function setArrayToJsonSafely($inputParameters, $preety = false)
61
    {
62
        $encodingFlags = (JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
63
        if ($preety) {
64
            $encodingFlags = (JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
65
        }
66
        $contentToWrite = json_encode($inputParameters, $encodingFlags);
67
        if ($contentToWrite === false) {
68
            throw new \RuntimeException(sprintf('Unable to encode string into JSON (code %s with message %s)', ''
69
                . json_last_error(), json_last_error_msg()));
70
        }
71
        return utf8_encode($contentToWrite);
72
    }
73
}
74