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

TranslatableAttribute::determineCast()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 4
nop 2
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