DMSJsonField   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 113
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 7
A setChildName() 0 4 1
A hasData() 0 4 1
A collateDataFields() 0 3 1
A arrayFilterEmptyRecursive() 0 13 4
A dataValue() 0 12 3
B setValue() 0 20 7
1
<?php
2
3
/**
4
 * Combines form inputs into a key-value pair
5
 */
6
class DMSJsonField extends CompositeField
7
{
8
    public function __construct($name, $children = null)
9
    {
10
        $this->setName($name);
11
12
        if ($children instanceof FieldList || is_array($children)) {
13
            foreach ($children as $child) {
14
                $this->setChildName($child);
15
            }
16
        } else {
17
            $children = is_array(func_get_args()) ? func_get_args() : array();
18
            if (!empty($children)) {
19
                array_shift($children);
20
            }
21
            foreach ($children as $child) {
22
                $this->setChildName($child);
23
            }
24
        }
25
        parent::__construct($children);
26
    }
27
28
    /**
29
     * Sets the name of the child object
30
     *
31
     * @param FormField $child
32
     */
33
    private function setChildName($child)
34
    {
35
        $child->setName("{$this->getName()}[{$child->getName()}]");
36
    }
37
38
    public function hasData()
39
    {
40
        return true;
41
    }
42
43
    /**
44
     * Override parent's behaviour as it's no longer required
45
     *
46
     * @param array $list
47
     * @param bool  $saveableOnly
48
     */
49
    public function collateDataFields(&$list, $saveableOnly = false)
50
    {
51
    }
52
53
    /**
54
     * Recursively removed empty key-value pairs from $haystack
55
     *
56
     * @param $haystack
57
     *
58
     * @return mixed
59
     */
60
    public function arrayFilterEmptyRecursive($haystack)
61
    {
62
        foreach ($haystack as $key => $value) {
63
            if (is_array($value)) {
64
                $haystack[$key] = $this->arrayFilterEmptyRecursive($haystack[$key]);
65
            }
66
            if (empty($haystack[$key])) {
67
                unset($haystack[$key]);
68
            }
69
        }
70
71
        return $haystack;
72
    }
73
74
    /**
75
     * Overrides parent behaviour to remove empty elements
76
     *
77
     * @return mixed|null|string
78
     */
79
    public function dataValue()
80
    {
81
        $result = null;
82
        if (is_array($this->value)) {
83
            $this->value = $this->arrayFilterEmptyRecursive($this->value);
84
            $result = (!empty($this->value)) ? Convert::array2json($this->value) : $result;
85
        } else {
86
            $result = parent::dataValue();
87
        }
88
89
        return $result;
90
    }
91
92
    /**
93
     * Sets the value
94
     * @param mixed $value
95
     *
96
     * @return $this
97
     */
98
    public function setValue($value)
99
    {
100
        $this->value = $value;
101
        if (is_string($value) && !empty($value)) {
102
            $value = Convert::json2array($value);
103
        } elseif (!is_array($value)) {
104
            $value = array($value);
105
        }
106
107
        $pattern = "/^{$this->getName()}\[(.*)\]$/";
108
        foreach ($this->children as $c) {
109
            $title = $c->getName();
110
            preg_match($pattern, $title, $matches);
111
            if (!empty($matches[1]) && isset($value[$matches[1]])) {
112
                $c->setValue($value[$matches[1]]);
113
            }
114
        }
115
116
        return $this;
117
    }
118
}
119