Total Complexity | 7 |
Total Lines | 54 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | trait CastProperties |
||
11 | { |
||
12 | /** @var array */ |
||
13 | protected $casts = []; |
||
14 | |||
15 | /** |
||
16 | * Check if is date attribute. |
||
17 | * |
||
18 | * @param string $attribute Public attribute name |
||
19 | * |
||
20 | * @return bool |
||
21 | */ |
||
22 | 5 | public function isDateAttribute($attribute): bool |
|
23 | { |
||
24 | 5 | return \array_key_exists($attribute, $this->casts) && $this->casts[$attribute] === 'date'; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * Cast attribute value. |
||
29 | * |
||
30 | * @param string $attribute |
||
31 | * |
||
32 | * @return array|string |
||
33 | */ |
||
34 | 7 | public function castAttribute($attribute) |
|
35 | { |
||
36 | 7 | switch ($this->getAttributeCastType($attribute)) { |
|
37 | 7 | case 'date': |
|
38 | 5 | $value = (string) IgfsUtils::formatXMLGregorianCalendar($this->{$attribute}); |
|
39 | |||
40 | 5 | break; |
|
41 | 7 | case 'array': |
|
42 | 1 | $value = (array) $this->{$attribute}; |
|
43 | |||
44 | 1 | break; |
|
45 | default: |
||
46 | 7 | $value = (string) $this->{$attribute}; |
|
47 | |||
48 | 7 | break; |
|
49 | } |
||
50 | |||
51 | 7 | return $value; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get attribute cast type for simple properties. |
||
56 | * |
||
57 | * @param string $attribute |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 10 | protected function getAttributeCastType(string $attribute): string |
|
64 | } |
||
65 | } |
||
66 |