Completed
Push — develop ( aae47e...e01b9a )
by Tino
08:22
created

DataProcessor::unescape()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 6
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) by the ACP3 Developers.
5
 * See the LICENSE file at the top-level module directory for licensing details.
6
 */
7
8
namespace ACP3\Core\Model;
9
10
use ACP3\Core\Model\DataProcessor\ColumnTypeStrategyFactory;
11
12
class DataProcessor
13
{
14
    /**
15
     * @var ColumnTypeStrategyFactory
16
     */
17
    protected $factory;
18
19
    /**
20
     * DataProcessor constructor.
21
     *
22
     * @param ColumnTypeStrategyFactory $factory
23
     */
24
    public function __construct(ColumnTypeStrategyFactory $factory)
25
    {
26
        $this->factory = $factory;
27
    }
28
29
    /**
30
     * @param array $columnData
31
     * @param array $columnConstraints
32
     *
33
     * @return array
34
     */
35
    public function escape(array $columnData, array $columnConstraints)
36
    {
37
        $data = [];
38
        foreach ($columnData as $column => $value) {
39
            if (\array_key_exists($column, $columnConstraints)) {
40
                $data[$column] = $this->factory->getStrategy($columnConstraints[$column])->doEscape($value);
41
            }
42
        }
43
44
        foreach ($this->findMissingColumns($columnData, $columnConstraints) as $columnName) {
45
            $data[$columnName] = $this->factory->getStrategy($columnConstraints[$columnName])->getDefaultValue();
46
        }
47
48
        return $data;
49
    }
50
51
    /**
52
     * @param array $columnData
53
     * @param array $columnConstraints
54
     *
55
     * @return array
56
     */
57
    public function unescape(array $columnData, array $columnConstraints)
58
    {
59
        $data = [];
60
        foreach ($columnData as $column => $value) {
61
            if (\array_key_exists($column, $columnConstraints)) {
62
                $data[$column] = $this->factory->getStrategy($columnConstraints[$column])->doUnescape($value);
63
            }
64
        }
65
66
        foreach ($this->findMissingColumns($columnData, $columnConstraints) as $columnName) {
67
            $data[$columnName] = $this->factory->getStrategy($columnConstraints[$columnName])->getDefaultValue();
68
        }
69
70
        return $data;
71
    }
72
73
    /**
74
     * @param array $columnData
75
     * @param array $columnConstraints
76
     *
77
     * @return array
78
     */
79
    private function findMissingColumns(array $columnData, array $columnConstraints): array
80
    {
81
        $missingColumns = \array_diff(
82
            \array_keys($columnConstraints),
83
            \array_intersect(
84
                \array_keys($columnData),
85
                \array_keys($columnConstraints)
86
            )
87
        );
88
89
        return $missingColumns;
90
    }
91
}
92