Passed
Pull Request — master (#83)
by Romain
03:05
created

OpenGraphElement::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template\Element;
6
7
use Kerox\Messenger\Helper\ValidatorTrait;
8
use Kerox\Messenger\Model\Common\Button\AbstractButton;
9
10
class OpenGraphElement implements \JsonSerializable
11
{
12
    use ValidatorTrait;
13
14
    /**
15
     * @var string
16
     */
17
    protected $url;
18
19
    /**
20
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
21
     */
22
    protected $buttons = [];
23
24
    /**
25
     * OpenGraphElement constructor.
26
     *
27 2
     * @param string $url
28
     *
29 2
     * @throws \InvalidArgumentException
30
     */
31 2
    public function __construct(string $url)
32 2
    {
33
        $this->isValidUrl($url);
34
35
        $this->url = $url;
36
    }
37
38
    /**
39 2
     * @param string $url
40
     *
41 2
     * @throws \InvalidArgumentException
42 2
     *
43
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\OpenGraphElement
44 1
     */
45
    public static function create(string $url): self
46 1
    {
47
        return new self($url);
48
    }
49
50
    /**
51
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
52 2
     *
53
     * @throws \InvalidArgumentException
54
     *
55 2
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\OpenGraphElement
56
     */
57
    public function setButtons(array $buttons): self
58
    {
59
        $this->isValidArray($buttons, 1);
60
        $this->isValidButtons($buttons, $this->getAllowedButtonsType());
61
62 1
        $this->buttons = $buttons;
63
64
        return $this;
65 1
    }
66 1
67
    /**
68
     * @return array
69 1
     */
70
    protected function getAllowedButtonsType(): array
71
    {
72
        return [
73
            AbstractButton::TYPE_WEB_URL,
74
        ];
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function toArray(): array
81
    {
82
        $array = [
83
            'url'     => $this->url,
84
            'buttons' => $this->buttons,
85
        ];
86
87
        return array_filter($array);
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function jsonSerialize(): array
94
    {
95
        return $this->toArray();
96
    }
97
}
98