Completed
Push — master ( 610136...82575c )
by De Cramer
06:15 queued 03:00
created

Implode::getRuleCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\RuleEngine\Rules;
4
5
/**
6
 * Class Implode
7
 *
8
 * @author    de Cramer Oliver<[email protected]>
9
 * @copyright 2018 Oliverde8
10
 * @package Oliverde8\Component\RuleEngine\Rules
11
 */
12
class Implode extends AbstractRule
13
{
14
    /**
15
     * @inheritdoc
16
     */
17 3
    public function apply($rowData, &$transformedData, $options = [])
18
    {
19 3
        $subOptions = $options;
20 3
        unset($subOptions['values']);
21 3
        unset($subOptions['with']);
22
23 3
        $data = [];
24 3
        foreach ($options['values'] as $ruleData) {
25 3
            $value = $this->ruleApplier->apply($rowData, $transformedData, $ruleData, $subOptions);
26
27 3
            if (!empty($value)) {
28 3
                if (is_array($value)) {
29 2
                    foreach ($this->flatten($value) as $v) {
30 2
                        $data[] = $v;
31
                    }
32
                } else {
33 3
                    $data[] = $value;
34
                }
35
            }
36
        }
37
38 3
        return implode($options['with'], array_unique($data));
39
    }
40
41
    /**
42
     * Flatten a multidimensional array.
43
     *
44
     * @param $array
45
     *
46
     * @return \Generator
47
     */
48 2
    protected function flatten($array)
49
    {
50 2
        foreach ($array as $v) {
51 2
            if (is_array($v)) {
52 2
                foreach ($this->flatten($v) as $value) {
53 2
                    yield $value;
54
                };
55
            } else {
56 2
                yield $v;
57
            }
58
        }
59 2
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 3
    public function validate($options)
65
    {
66 3
        $this->requireOption('values', $options);
67 3
        $this->requireOption('with', $options);
68 3
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 1
    public function getRuleCode()
74
    {
75 1
        return 'implode';
76
    }
77
}