Completed
Push — master ( e25986...ffddd1 )
by Craig
07:06
created

removeEmptyEntries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Routes.
4
 *
5
 * @copyright Zikula contributors (Zikula)
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 * @author Zikula contributors <[email protected]>.
8
 * @link http://www.zikula.org
9
 * @link http://zikula.org
10
 * @version Generated by ModuleStudio 0.7.4 (http://modulestudio.de).
11
 */
12
13
namespace Zikula\RoutesModule\Form\DataTransformer\Base;
14
15
use Symfony\Component\Form\DataTransformerInterface;
16
use Symfony\Component\Form\FormBuilderInterface;
17
18
/**
19
 * Array field transformer base class.
20
 */
21
abstract class AbstractArrayFieldTransformer implements DataTransformerInterface
22
{
23
    /**
24
     * Transforms the object array to the normalised value.
25
     *
26
     * @param array|null $values
27
     *
28
     * @return string
29
     */
30
    public function transform($values)
31
    {
32
        if (null === $values) {
33
            return '';
34
        }
35
36
        if (!is_array($values)) {
37
            return $values;
38
        }
39
40
        if (!count($values)) {
41
            return '';
42
        }
43
44
        $value = $this->removeEmptyEntries($values);
45
46
        return implode("\n", $value);
47
    }
48
49
    /**
50
     * Transforms a textual value back to the array.
51
     *
52
     * @param string $value
53
     *
54
     * @return array
55
     */
56
    public function reverseTransform($value)
57
    {
58
        if (!$value) {
59
            return [];
60
        }
61
62
        $items = explode("\n", $value);
63
64
        return $this->removeEmptyEntries($items);
65
    }
66
67
    /**
68
     * Iterates over the given array and removes all empty entries.
69
     *
70
     * @param array array The given input array.
71
     *
72
     * @return array The cleaned array.
73
     */
74
    protected function removeEmptyEntries($array)
75
    {
76
        $items = $array;
77
78
        foreach ($items as $k => $v) {
79
            if (!empty($v)) {
80
                continue;
81
            }
82
            unset($items[$k]);
83
        }
84
85
        return $items;
86
    }
87
}
88