EntityAttributes::isEntityAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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