CastProperties::isDateAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
namespace PagOnline\XmlEntities\Traits;
4
5
use PagOnline\IgfsUtils;
6
7
/**
8
 * Trait CastProperties.
9
 */
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
62
    {
63 10
        return \array_key_exists($attribute, $this->casts) ? $this->casts[$attribute] : 'string';
64
    }
65
}
66