Completed
Push — master ( 25f8d8...b9ecd3 )
by tac
02:20
created

JoinedArrayAttribute::castToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
nc 2
cc 2
eloc 3
nop 1
1
<?php
2
3
namespace Tacone\Bees\Attribute;
4
5
class JoinedArrayAttribute extends ArrAttribute
6
{
7
    protected $separator = '|';
8
9
    protected function castToArray($array)
10
    {
11
        $array = is_string($array) ? explode('|', $array) : $array;
12
13
        return parent::castToArray($array);
14
    }
15
16
    /**
17
     * Flatten a multi dimensional array and split all the strings
18
     * using the separator.
19
     *
20
     * @param $value
21
     *
22
     * @return array
23
     */
24
    protected function flattenValue($value)
25
    {
26
        $value = (array) $value;
27
        $return = array();
28
        $separator = $this->separator;
29
        array_walk_recursive($value, function ($item) use (&$return, $separator) {
30
            if (is_string($item)) {
31
                $item = explode($separator, $item);
32
            }
33
            if (!is_array($item) && $item instanceof \Traversable) {
34
                $item = iterator_to_array($item);
35
            }
36
            if (is_array($item)) {
37
                $return = array_merge($return, $item);
38
            } else {
39
                $return[] = $item;
40
            }
41
        });
42
        $return = array_filter($return);
43
44
        return $return;
45
    }
46
}
47