FrameComponent   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
dl 0
loc 84
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 1
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A jsonSerialize() 0 18 1
A addScalar() 0 4 2
A addArrayIfNotEmpty() 0 4 3
A addNonDefault() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Component;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Gradient;
9
10
class FrameComponent extends APLBaseComponent implements \JsonSerializable
11
{
12
    public const TYPE = APLComponentType::FRAME;
13
14
    /**
15
     * @param Gradient|string|null $background Background fill that allows either a color or gradient
16
     * @param string|null $backgroundColor Background color (ignored if background is provided)
17
     * @param string|null $borderBottomLeftRadius Radius of the bottom-left corner
18
     * @param string|null $borderBottomRightRadius Radius of the bottom-right corner
19
     * @param string|null $borderColor Color of the border
20
     * @param string $borderRadius Corner radius for rounded-rectangle variant
21
     * @param string|null $borderStrokeWidth Width of the border stroke
22
     * @param string|null $borderTopLeftRadius Radius of the top-left corner
23
     * @param string|null $borderTopRightRadius Radius of the top-right corner
24
     * @param string $borderWidth Width of the border
25
     * @param APLBaseComponent|null $item Single child component to display inside the Frame
26
     * @param APLBaseComponent[]|null $items Array of child components to display inside the Frame
27
     */
28 10
    public function __construct(
29
        public Gradient|string|null $background = null,
30
        public ?string $backgroundColor = null,
31
        public ?string $borderBottomLeftRadius = null,
32
        public ?string $borderBottomRightRadius = null,
33
        public ?string $borderColor = null,
34
        public string $borderRadius = '0',
35
        public ?string $borderStrokeWidth = null,
36
        public ?string $borderTopLeftRadius = null,
37
        public ?string $borderTopRightRadius = null,
38
        public string $borderWidth = '0',
39
        public ?APLBaseComponent $item = null,
40
        public ?array $items = null,
41
    ) {
42 10
        parent::__construct(self::TYPE);
43
    }
44
45 5
    public function jsonSerialize(): array
46
    {
47 5
        $data = parent::jsonSerialize();
48
49 5
        $this->addScalar($data, 'background', $this->background);
50 5
        $this->addScalar($data, 'backgroundColor', $this->backgroundColor);
51 5
        $this->addScalar($data, 'borderBottomLeftRadius', $this->borderBottomLeftRadius);
52 5
        $this->addScalar($data, 'borderBottomRightRadius', $this->borderBottomRightRadius);
53 5
        $this->addScalar($data, 'borderColor', $this->borderColor);
54 5
        $this->addNonDefault($data, 'borderRadius', $this->borderRadius, '0');
55 5
        $this->addScalar($data, 'borderStrokeWidth', $this->borderStrokeWidth);
56 5
        $this->addScalar($data, 'borderTopLeftRadius', $this->borderTopLeftRadius);
57 5
        $this->addScalar($data, 'borderTopRightRadius', $this->borderTopRightRadius);
58 5
        $this->addNonDefault($data, 'borderWidth', $this->borderWidth, '0');
59 5
        $this->addScalar($data, 'item', $this->item);
60 5
        $this->addArrayIfNotEmpty($data, 'items', $this->items);
61
62 5
        return $data;
63
    }
64
65
    /**
66
     * @param array<string,mixed> $data
67
     */
68 5
    private function addScalar(array &$data, string $key, mixed $value): void
69
    {
70 5
        if ($value !== null) {
71 1
            $data[$key] = $value;
72
        }
73
    }
74
75
    /**
76
     * @param array<string,mixed> $data
77
     * @param string $default
78
     */
79 5
    private function addNonDefault(array &$data, string $key, string $value, string $default): void
80
    {
81 5
        if ($value !== $default) {
82 2
            $data[$key] = $value;
83
        }
84
    }
85
86
    /**
87
     * @param array<string,mixed> $data
88
     * @param array<mixed>|null $value
89
     */
90 5
    private function addArrayIfNotEmpty(array &$data, string $key, ?array $value): void
91
    {
92 5
        if ($value !== null && $value !== []) {
93 1
            $data[$key] = $value;
94
        }
95
    }
96
}
97