Completed
Pull Request — master (#53)
by Romain
03:15
created

WebUrl::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Kerox\Messenger\Model\Common\Button;
4
5
class WebUrl extends AbstractButton
6
{
7
8
    const RATIO_TYPE_COMPACT = 'compact';
9
    const RATIO_TYPE_TALL = 'tall';
10
    const RATIO_TYPE_FULL = 'full';
11
12
    /**
13
     * @var string
14
     */
15
    protected $title;
16
17
    /**
18
     * @var string
19
     */
20
    protected $url;
21
22
    /**
23
     * @var string
24
     */
25
    protected $webviewHeightRatio;
26
27
    /**
28
     * @var null|bool
29
     */
30
    protected $messengerExtension;
31
32
    /**
33
     * @var null|string
34
     */
35
    protected $fallbackUrl;
36
37
    /**
38
     * @var null|string
39
     */
40
    protected $webviewShareButton;
41
42
    /**
43
     * WebUrl constructor.
44
     *
45
     * @param string $title
46
     * @param string $url
47
     */
48
    public function __construct(string $title, string $url)
49
    {
50
        parent::__construct(self::TYPE_WEB_URL);
51
52
        $this->isValidString($title, 20);
53
        $this->isValidUrl($url);
54
55
        $this->title = $title;
56
        $this->url = $url;
57
    }
58
59
    /**
60
     * @param string $webviewHeightRatio
61
     * @return WebUrl
62
     */
63
    public function setWebviewHeightRatio(string $webviewHeightRatio): WebUrl
64
    {
65
        $this->isValidWebviewHeightRatio($webviewHeightRatio);
66
67
        $this->webviewHeightRatio = $webviewHeightRatio;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param bool $messengerExtension
74
     * @return WebUrl
75
     */
76
    public function setMessengerExtension(bool $messengerExtension): WebUrl
77
    {
78
        $this->messengerExtension = $messengerExtension;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param string $fallbackUrl
85
     * @return WebUrl
86
     */
87
    public function setFallbackUrl(string $fallbackUrl): WebUrl
88
    {
89
        $this->isValidUrl($fallbackUrl);
90
91
        $this->fallbackUrl = $fallbackUrl;
92
93
        return $this;
94
    }
95
96
    public function setWebviewShareButton(bool $webviewShareButton): WebUrl
97
    {
98
        if (!$webviewShareButton) {
99
            $this->webviewShareButton = 'hide';
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $webviewHeightRatio
107
     *
108
     * @throws \InvalidArgumentException
109
     */
110
    private function isValidWebviewHeightRatio(string $webviewHeightRatio)
111
    {
112
        $allowedRatioType = $this->getAllowedRatioType();
113
        if (!in_array($webviewHeightRatio, $allowedRatioType)) {
114
            throw new \InvalidArgumentException('$webviewHeightRatio must be either ' . implode(', ', $allowedRatioType));
115
        }
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    private function getAllowedRatioType(): array
122
    {
123
        return [
124
            self::RATIO_TYPE_COMPACT,
125
            self::RATIO_TYPE_TALL,
126
            self::RATIO_TYPE_FULL,
127
        ];
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function jsonSerialize(): array
134
    {
135
        $json = parent::jsonSerialize();
136
        $json += [
137
            'url' => $this->url,
138
            'title' => $this->title,
139
            'webview_height_ratio' => $this->webviewHeightRatio,
140
            'messenger_extensions' => $this->messengerExtension,
141
            'fallback_url' => $this->fallbackUrl,
142
            'webview_share_button' => $this->webviewShareButton,
143
        ];
144
145
        return array_filter($json);
146
    }
147
}
148