Issues (138)

src/Traits/DataProcessingTrait.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
/**
6
 * Trait DataProcessingTrait
7
 * @package Nip\Form\Traits
8
 */
9
trait DataProcessingTrait
10
{
11
    /**
12
     * @param $request
13
     */
14
    protected function getDataFromRequest($request)
15
    {
16
        $elements = $this->getElements();
0 ignored issues
show
It seems like getElements() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        /** @scrutinizer ignore-call */ 
17
        $elements = $this->getElements();
Loading history...
17
        if (is_array($elements)) {
18
            foreach ($elements as $name => $element) {
19
                if ($element->isGroup() && $element->isRequestArray()) {
20
                    $name = str_replace('[]', '', $name);
21
22
                    if (isset($request[$name])) {
23
                        $data = is_array($request[$name]) ? $request[$name] : [$request[$name]];
24
                        $element->getData($data, 'request');
25
                    }
26
                } else {
27
                    $value = isset($request[$name]) ? $request[$name] : null;
28
                    if (strpos($name, '[') && strpos($name, ']')) {
29
                        $arrayPrimary = substr($name, 0, strpos($name, '['));
30
                        $arrayKeys = str_replace($arrayPrimary, '', $name);
31
32
                        preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $arr_matches, PREG_PATTERN_ORDER);
33
                        $value = $request[$arrayPrimary];
34
                        foreach ($arr_matches[1] as $dimension) {
35
                            $value = isset($value[$dimension]) ? $value[$dimension] : null;
36
                        }
37
                    }
38
                    $element->getData($value, 'request');
39
                }
40
            }
41
        }
42
    }
43
44
    /**
45 1
     * @return array
46
     */
47 1
    public function getData()
48 1
    {
49 1
        $data = [];
50 1
        $elements = $this->getElements();
51 1
        if (is_array($elements)) {
52 1
            foreach ($elements as $element) {
53
                $name = $element->getName();
54
                if ($element->isGroup() && $element->isRequestArray()) {
55
                    $name = str_replace('[]', '', $name);
56
                    $data[$name][] = $element->getValue();
57
                    continue;
58 1
                }
59 1
60 1
                if (strpos($name, '[]')) {
61 1
                    $arrayPrimary = substr($name, 0, strpos($name, '['));
62
                    $data[$arrayPrimary][] = $element->getValue();
63
                    continue;
64 1
                }
65 1
66 1
                if (strpos($name, '[') && strpos($name, ']')) {
67
                    $arrayPrimary = substr($name, 0, strpos($name, '['));
68 1
                    $arrayKeys = str_replace($arrayPrimary, '', $name);
69
70 1
                    preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $arr_matches, PREG_PATTERN_ORDER);
71 1
72 1
                    $dataArray = $element->getValue();
73
                    foreach ($arr_matches[1] as $dimension) {
74
                        $dataArray = [$dimension => $dataArray];
75 1
                    }
76 1
77 1
                    $data[$arrayPrimary] = (isset($data[$arrayPrimary]))
78 1
                        ? array_merge($data[$arrayPrimary], $dataArray)
79
                        : $dataArray;
80 1
                    continue;
81
                }
82
                $data[$name] = $element->getValue();
83
            }
84 1
        }
85
86
        return $data;
87
    }
88
}
89