| Total Complexity | 11 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | class Implode extends AbstractRule |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | 3 | * @inheritdoc |
|
| 18 | */ |
||
| 19 | 3 | public function apply(array $rowData, array &$transformedData, array $options = []) |
|
| 20 | 3 | { |
|
| 21 | 3 | $subOptions = $options; |
|
| 22 | unset($subOptions['values']); |
||
| 23 | 3 | unset($subOptions['with']); |
|
| 24 | 3 | ||
| 25 | 3 | $data = []; |
|
| 26 | foreach ($options['values'] as $ruleData) { |
||
| 27 | 3 | $value = $this->ruleApplier->apply($rowData, $transformedData, $ruleData, $subOptions); |
|
| 28 | 3 | ||
| 29 | 2 | if (!empty($value)) { |
|
| 30 | 2 | if (is_array($value)) { |
|
| 31 | foreach ($this->flatten($value) as $v) { |
||
| 32 | $data[] = $v; |
||
| 33 | 1 | } |
|
| 34 | } else { |
||
| 35 | $data[] = $value; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | 3 | } |
|
| 39 | |||
| 40 | return implode($options['with'], $data); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Flatten a multidimensional array. |
||
| 45 | * |
||
| 46 | * @param array $array |
||
| 47 | * |
||
| 48 | 2 | * @return \Generator |
|
| 49 | */ |
||
| 50 | 2 | protected function flatten(array $array): \Generator |
|
| 51 | 2 | { |
|
| 52 | 2 | foreach ($array as $v) { |
|
| 53 | 2 | if (is_array($v)) { |
|
| 54 | foreach ($this->flatten($v) as $value) { |
||
| 55 | yield $value; |
||
| 56 | 2 | }; |
|
| 57 | } else { |
||
| 58 | yield $v; |
||
| 59 | 2 | } |
|
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | 3 | * @inheritdoc |
|
| 65 | */ |
||
| 66 | 3 | public function validate(array $options): void |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | 1 | * @inheritdoc |
|
| 74 | */ |
||
| 75 | 1 | public function getRuleCode(): string |
|
| 78 | } |
||
| 79 | } |