Completed
Push — master ( 8f5f7c...7faed5 )
by Basenko
04:23
created

Field   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 64
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parseAttributesWithKeys() 0 18 4
A parseAttributes() 0 13 3
A getValue() 0 4 1
parseValue() 0 1 ?
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
    public function __construct($value, $model)
14
    {
15
        $this->model = $model;
16
        $this->value = $this->parseValue($value);
17
    }
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
    protected function parseAttributes($attributes)
44
    {
45
        $result = [];
46
        if (is_array($attributes)) {
47
            foreach ($attributes as $key => $field) {
48
                $result[] = $this->model->getAttribute($field);
49
            }
50
        } else {
51
            return $this->model->getAttribute($attributes);
52
        }
53
54
        return $result;
55
    }
56
57
    /** @return mixed */
58
    public function getValue()
59
    {
60
        return $this->value;
61
    }
62
63
    /**
64
     * @param array|string $value
65
     * @return mixed
66
     */
67
    abstract protected function parseValue($value);
68
}
69