EntityAttributes   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 42
ccs 9
cts 11
cp 0.8182
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCustomAttributeXml() 0 16 6
A isEntityAttribute() 0 3 1
1
<?php
2
3
namespace PagOnline\XmlEntities\Traits;
4
5
use PagOnline\XmlEntities\XmlEntityInterface;
6
7
/**
8
 * Trait EntityAttributes.
9
 */
10
trait EntityAttributes
11
{
12
    /**
13
     * Attributes that are classes
14
     * array['attributeName']['type'] = 'scalar|array'
15
     * array['attributeName']['namespace'] = \PagOnline\XmlEntities\Class::class.
16
     *
17
     * @var array
18
     */
19
    protected $entityAttributes = [];
20
21
    /**
22
     * @param string $attribute
23
     *
24
     * @return bool
25
     */
26 10
    public function isEntityAttribute(string $attribute): bool
27
    {
28 10
        return \array_key_exists($attribute, $this->entityAttributes);
29
    }
30
31
    /**
32
     * @param string $attribute
33
     *
34
     * @return null|string
35
     */
36 3
    public function getCustomAttributeXml(string $attribute): ?string
37
    {
38 3
        if (empty($this->{$attribute})) {
39
            return null;
40
        }
41 3
        $xmlContent = '';
42 3
        if ($this->entityAttributes[$attribute]['type'] == 'array') {
43 3
            foreach ($this->{$attribute} as $item) {
44
                /* @var \PagOnline\XmlEntities\XmlEntityInterface $item */
45 3
                $xmlContent .= $item instanceof XmlEntityInterface ? $item->toXml($attribute) : '';
46
            }
47
        } else {
48
            $xmlContent .= $this->{$attribute} instanceof XmlEntityInterface ? $this->{$attribute}->toXml($attribute) : '';
49
        }
50
51 3
        return $xmlContent;
52
    }
53
}
54