Completed
Pull Request — master (#119)
by Franco
02:40
created

JsonField::hasData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class JsonField combines form inputs into a key-value pair
4
 */
5
6
class JsonField extends CompositeField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public function __construct($name, $children = null)
9
    {
10
        $this->setName($name);
11
12
        if ($children instanceof FieldList) {
13
            foreach ($children as $child) {
14
                $child->setName($this->getName() . "[" . $child->getName() . "]");
15
            }
16
        } elseif (is_array($children)) {
17
            foreach ($children as $child) {
18
                $child->setName($this->getName() . "[" . $child->getName() . "]");
19
            }
20
        } else {
21
            $children = is_array(func_get_args()) ? func_get_args() : array();
22
            if (!empty($children)) {
23
                array_shift($children);
24
            }
25
            foreach ($children as $child) {
26
                $child->setName($this->getName() . "[" . $child->getName() . "]");
27
            }
28
        }
29
        parent::__construct($children);
30
    }
31
32
    public function hasData()
33
    {
34
        return true;
35
    }
36
37
    //have to has it to overwritten CompositeField behavior
38
    public function collateDataFields(&$list, $saveableOnly = false)
39
    {
40
    }
41
42
    function array_filter_empty_recursive($haystack)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
    {
44
        foreach ($haystack as $key => $value) {
45
            if (is_array($value)) {
46
                $haystack[$key] = $this->array_filter_empty_recursive($haystack[$key]);
47
            }
48
            if (empty($haystack[$key])) {
49
                unset($haystack[$key]);
50
            }
51
        }
52
53
        return $haystack;
54
    }
55
56
    public function dataValue()
57
    {
58
        if (is_array($this->value)) {
59
            $this->value = $this->array_filter_empty_recursive($this->value);
60
            if (!empty($this->value)) {
61
                return Convert::array2json($this->value);
62
            } else {
63
                return null;
64
            }
65
        } else {
66
            return parent::dataValue();
67
        }
68
    }
69
70
    /**
71
     * Load a value into this CheckboxSetField
72
     */
73
    public function setValue($value)
74
    {
75
        $this->value = $value;
76
        if (is_string($value) && !empty($value)) {
77
            $value = Convert::json2array($value);
78
        } // @codeCoverageIgnoreStart
79
        elseif (!is_array($value)) {
80
            $value = array($value);
81
        }
82
        // @codeCoverageIgnoreEnd
83
        $patten = "/^" . $this->getName() . "\[(.*)\]$/";
84
        foreach ($this->children as $c) {
85
            $title = $c->getName();
86
            preg_match($patten, $title, $matches);
87
            if (!empty($matches[1]) && isset($value[$matches[1]])) {
88
                $c->setValue($value[$matches[1]]);
89
            }
90
        }
91
92
        return $this;
93
    }
94
}
95