Completed
Push — master ( 080b9b...8f9441 )
by Freek
01:54
created

TranslatableAttribute   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A determineName() 0 4 2
A determineCast() 0 18 3
1
<?php
2
3
namespace Spatie\Translatable;
4
5
use Spatie\Translatable\Exceptions\InvalidCast;
6
7
class TranslatableAttribute
8
{
9
    public $name;
10
    public $cast;
11
12
    public function __construct($key, $value)
13
    {
14
        $this->name = $this->determineName($key, $value);
15
        $this->cast = $this->determineCast($key, $value);
16
    }
17
18
    protected function determineName($key, $value) : string
19
    {
20
        return is_string($key) ? $key : $value;
21
    }
22
23
    protected function determineCast($key, $value) : string
24
    {
25
        $cast = is_string($key) ? $value : 'string';
26
27
        $availableCasts = [
28
            'string',
29
            'bool',
30
            'integer',
31
            'float',
32
            'array',
33
        ];
34
35
        if (!in_array($cast, $availableCasts)) {
36
            throw InvalidCast::make($cast, $availableCasts);
37
        }
38
39
        return $cast;
40
    }
41
}
42