CastProperties   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 54
ccs 15
cts 15
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeCastType() 0 3 2
A isDateAttribute() 0 3 2
A castAttribute() 0 18 3
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