Passed
Push — master ( 606715...ac81bc )
by Robin
04:41
created

AbstractRepository::ensureArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Konsulting\Laravel\RuleRepository;
4
5
use Konsulting\Laravel\RuleRepository\Contracts\RuleRepository;
6
7
abstract class AbstractRepository implements RuleRepository
8
{
9
    /**
10
     * Prepend a rule to all fields. Rules expressed as pipe-delimited strings will be converted to arrays.
11
     *
12
     * @param string $rule
13
     * @param array  $baseRules
14
     * @return array[]
15
     */
16
    protected function prependRule($rule, $baseRules)
17
    {
18 1
        return array_map(function ($ruleLine) use ($rule) {
19 1
            return array_merge([$rule], $this->ensureArray($ruleLine));
20 1
        }, $baseRules);
21
    }
22
23
    /**
24
     * Append a rule to all fields. Rules expressed as pipe-delimited strings will be converted to arrays.
25
     *
26
     * @param string $rule
27
     * @param array  $baseRules
28
     * @return array[]
29
     */
30
    protected function appendRule($rule, $baseRules)
31
    {
32 1
        return array_map(function ($ruleLine) use ($rule) {
33 1
            return array_merge($this->ensureArray($ruleLine), [$rule]);
34 1
        }, $baseRules);
35
    }
36
37
    /**
38
     * Make sure the rules are expressed in array format.
39
     *
40
     * @param array|string|mixed $ruleLine
41
     * @return array
42
     */
43 2
    private function ensureArray($ruleLine)
44
    {
45 2
        if (empty($ruleLine)) {
46 2
            return [];
47 2
        } elseif (is_string($ruleLine)) {
48 2
            return explode('|', $ruleLine);
49
        }
50
51 2
        return is_array($ruleLine) ? $ruleLine : [$ruleLine];
52
    }
53
}
54