Completed
Push — master ( ec2e66...eaa18f )
by Basenko
07:05
created

Field::parseAttributesWithKeys()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 10
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 2
nop 1
crap 20
1
<?php
2
3
namespace MadWeb\Seoable\Fields;
4
5
abstract class Field
6
{
7
    /** @var \Illuminate\Database\Eloquent\Model */
8
    protected $model;
9
10
    /** @var string|array */
11
    protected $value;
12
13 6
    public function __construct($value, $model)
14
    {
15 6
        $this->model = $model;
16 6
        $this->value = $this->parseValue($value);
17 6
    }
18
19
    /** @param array|string $attributes */
20
    protected function parseAttributesWithKeys($attributes): array
21
    {
22
        $result = [];
23
        if (is_array($attributes)) {
24
            foreach ($attributes as $key => $field) {
25
                $value = $this->model->getAttribute($field);
26
                if (is_numeric($key)) {
27
                    $result[$field] = $value;
28
                } else {
29
                    $result[$key] = $value;
30
                }
31
            }
32
        } else {
33
            $result[$attributes] = $this->model->getAttribute($attributes);
34
        }
35
36
        return $result;
37
    }
38
39
    /**
40
     * @param array|string $attributes
41
     * @return array|string
42
     */
43 6
    protected function parseAttributes($attributes)
44
    {
45 6
        $result = [];
46 6
        if (is_array($attributes)) {
47 3
            foreach ($attributes as $key => $field) {
48 3
                $result[] = $this->model->getAttribute($field);
49
            }
50
        } else {
51 3
            return $this->model->getAttribute($attributes);
52
        }
53
54 3
        return $result;
55
    }
56
57
    /** @return mixed */
58 6
    public function getValue()
59
    {
60 6
        return $this->value;
61
    }
62
63
    /**
64
     * @param array|string $value
65
     * @return mixed
66
     */
67
    abstract protected function parseValue($value);
68
}
69